简体   繁体   English

如何在Elasticsearch中从前缀搜索中排除匹配的术语?

[英]How can I exclude matched terms from prefix searching in Elasticsearch?

Is there any way how to tell Elastic to exclude matched terms for "prefix" searching? 有什么方法可以告诉Elastic为“前缀”搜索排除匹配的术语?

Consider this query: 考虑以下查询:

{
    "query": {
        "bool": {
            "must": [
                {"match": {"title": "word1"}},
                {"match": {"title": "word2"}},
                {"prefix": {"title": "w"}}
            ]
        }
    }
}

and stored documents in an index: 并将文档存储在索引中:

{"title": "word1 word2", "data": "..."}
{"title": "word2 word1", "data": "..."}
{"title": "word1 word2 www", "data": "..."}
{"title": "word1 www word2", "data": "..."}
{"title": "www word2 word1", "data": "..."}
{"title": "xxx www word2 word1", "data": "..."}

then all these documents will be in query response (because terms "word1", "word2" and "www" has "w" prefix). 那么所有这些文档将处于查询响应中(因为术语“ word1”,“ word2”和“ www”具有“ w”前缀)。

But that's not ok for me. 但这对我来说不行。 I need to obtain only these documents: 我只需要获取以下文件:

{"title": "word1 word2 www", "data": "..."}
{"title": "word1 www word2", "data": "..."}
{"title": "www word2 word1", "data": "..."}

Is there any way to do this? 有什么办法吗?

Based on the output you mentioned, you need word1 , word2 and www to be all present in the title. 根据您提到的输出,标题中必须包含word1word2www

For prefix matching, you need to match it with title.keyword and not title since the inverted index does contain atleast one work with w as prefix. 对于prefix匹配,您需要将其与title.keyword而不是title匹配,因为倒排索引确实包含至少一个以w为前缀的作品。 But as i understand, you want the prefix for the entire title so keyword should achieve that. 但是据我了解,您想要整个标题的前缀,因此keyword应该可以达到目的。

Below query works and gives expected results: 下面的查询有效,并给出了预期的结果:

{
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": "word1"
              }
            },
            {
              "match": {
                "title": "word2"
              }
            },
            {
              "match": {
                "title": "www"
              }
            },
            {
              "prefix": {
                "title.keyword": "w"
              }
            }
          ]
        }
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM