I know there is elastic search pipeline processor can do data transformation, but it's before index the document. Is there similar thing for after search result? My use case is that, I have a document, some fields have very long texts, I simply want to truncate those long texts into shorter texts. But I don't want to do it before index documents because those truncated text would not be searchable. I can certainly do this on my Angular UI code after elastic search returns the result. But I want to see if there is alternative way to let elastic search engine do it for me, before return to Angular.
Any tip appreciate.
CodePudding user response:
You can use the script field for this. First, lets create a sample document
POST sample-index/_doc
{
"id": 1,
"text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
}
Now lets do a query with scripted fields:
GET sample-index/_search
{
"_source": ["id"],
"query": {
"match": {
"text": "ipsum"
}
},
"script_fields": {
"text_summary": {
"script": "if (params['_source']['text'].length() > 40) { return params['_source']['text'].substring(0,40) '...'; } else { return params['_source']['text']; }"
}
}
}
The result will be something like this :
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.39556286,
"hits": [
{
"_index": "sample-index",
"_id": "ZAhIEoUBNMHtGSRnO4bt",
"_score": 0.39556286,
"_ignored": [
"text.keyword"
],
"_source": {
"id": 1
},
"fields": {
"text_summary": [
"Lorem Ipsum is simply dummy text of the ..."
]
}
}
]
}
}