简体   繁体   English

无法匹配包含查询中指定的所有单词的文档

[英]unable to match document containing all words specified in query

I am struggling to get only those documents which contains word1 and word2 both at least.我正在努力只获取至少包含word1word2的那些文档。 I tried multiple ways to form request but still fails to fetch required records.我尝试了多种方式来形成请求,但仍然无法获取所需的记录。 Below request returns document even if word1 is missing from document but it contains word2 .即使 document 中缺少word1但它包含word2 ,下面的请求也会返回 document 。

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "source": "source1"
                    }
                },
                {
                    "term": {
                        "qty": "1"
                    }
                }
            ],
            "must": [
                {
                    "match": {
                        "product_name": {
                            "query": "word1 word2",
                            "fuzziness": "AUTO"
                        }
                    }
                }
            ]
        }
    }
}

Tldr; Tldr;

There are many ways to achieve this.有很多方法可以实现这一点。 Either with a terms query for keywords type fields or query_string for text fields.使用关键字类型字段的terms查询或文本字段的query_string

Solution解决方案

Assuming the field product_name is of type text.假设字段product_name是文本类型。

POST /_bulk
{"index":{"_index":"74018346"}}
{"product_name":"This sentence has word1 and word2"}
{"index":{"_index":"74018346"}}
{"product_name":"this sentence only has word1"}
{"index":{"_index":"74018346"}}
{"product_name":"in this one, only word2 is present"}
{"index":{"_index":"74018346"}}
{"product_name":"None of the words are presents"}


GET 74018346/_search
{
  "query": {
    "query_string": {
      "query": "word1 AND word2",
      "default_field": "product_name"
    }
  }
}

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

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