Preserving order of terms in ElasticSearch query

前端 未结 3 1581
滥情空心
滥情空心 2020-12-18 04:43

Is it possible in ElasticSearch to form a query that would preserve the ordering of the terms?

A simple example would be having these documents indexed using standar

3条回答
  •  感情败类
    2020-12-18 05:18

    This is exactly what a match_phrase query (see here) does.

    It checks the position of the terms, on top of their presence.

    For example, these documents :

    POST test/values
    {
      "test": "Hello World"
    }
    
    POST test/values
    {
      "test": "Hello nice World"
    }
    
    POST test/values
    {
      "test": "World, I don't say hello"
    }
    

    will all be found with the basic match query :

    POST test/_search
    {
      "query": {
        "match": {
          "test": "Hello World"
        }
      }
    }
    

    But using a match_phrase, only the first document will be returned :

    POST test/_search
    {
      "query": {
        "match_phrase": {
          "test": "Hello World"
        }
      }
    }
    
    {
       ...
       "hits": {
          "total": 1,
          "max_score": 2.3953633,
          "hits": [
             {
                "_index": "test",
                "_type": "values",
                "_id": "qFZAKYOTQh2AuqplLQdHcA",
                "_score": 2.3953633,
                "_source": {
                   "test": "Hello World"
                }
             }
          ]
       }
    }
    

    In your case, you want to accept to have some distance between your terms. This can be achieved with the slop parameter, which indicate how far you allow your terms to be one from another :

    POST test/_search
    {
      "query": {
        "match": {
          "test": {
            "query": "Hello world",
            "slop":1,
            "type": "phrase"
          }
        }
      }
    }
    

    With this last request, you find the second document too :

    {
       ...
       "hits": {
          "total": 2,
          "max_score": 0.38356602,
          "hits": [
             {
                "_index": "test",
                "_type": "values",
                "_id": "7mhBJgm5QaO2_aXOrTB_BA",
                "_score": 0.38356602,
                "_source": {
                   "test": "Hello World"
                }
             },
             {
                "_index": "test",
                "_type": "values",
                "_id": "VKdUJSZFQNCFrxKk_hWz4A",
                "_score": 0.2169777,
                "_source": {
                   "test": "Hello nice World"
                }
             }
          ]
       }
    }
    

    You can find a whole chapter about this use case in the definitive guide.

提交回复
热议问题