问题
In mysql
I can use the sql
SELECT LEFT(field,4) from table
to search 4 bytes of a field.
Elasticsearch
has similar grammar?
回答1:
With ES, you can use script_fields in order to perform any script computation on existing fields.
In your case, this would translate to making a query like this:
{
"query" : {
"match_all": {}
},
"script_fields" : {
"left_field" : {
"script" : {
"inline": "doc.field.value.substring(0, length)"
"params": {
"length": 4
}
}
}
}
}
Then in your response you'll get a field named left_field
which will contain the 4 left-most characters of the field
value.
Also make sure to enable dynamic scripting in order for this to work.
来源:https://stackoverflow.com/questions/35644126/elasticsearch-get-for-a-substring-in-the-value-of-a-document-field