Elasticsearch: get for a substring in the value of a document field?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-19 03:20:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!