【问题标题】:Elasticsearch Can I use "OR" or "AND" in the query_string phrase mode?Elasticsearch 我可以在 query_string 短语模式中使用“OR”或“AND”吗?
【发布时间】:2021-03-04 17:08:52
【问题描述】:

例如,我有两个文档,例如“美味的苹果很好”和“美味的香蕉很好”。

现在我想使用 query_string 来匹配“tasty apple”和“tasty banana”这两个短语。一般来说,我可以使用类似的查询

"\"美味的苹果\"或\"美味的香蕉\""

匹配这些文件。

但在这里我想要这样的查询

"\"美味(苹果或香蕉)\""

匹配。 es好像不支持短语模式下的括号和bool。

之所以需要上面这个,是因为随着搜索词数的增加,有效的query_string越来越复杂。

例如当我想搜索时

“\”(美味或讨厌或好)(苹果或香蕉或葡萄)\“”

我不想像这样划分这个查询字符串

"\"美味的苹果\" OR \"美味的香蕉\" OR \"美味的葡萄\" OR \"讨厌的苹果\" OR ..."

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    提取文档

    POST test_david_zhao/_doc
    {
      "text": "tasty apple is good"
    }
    
    POST test_david_zhao/_doc
    {
      "text": "tasty grape is good"
    }
    
    POST test_david_zhao/_doc
    {
      "text": "tasty banana is good"
    }
    
    POST test_david_zhao/_doc
    {
      "text": "nasty banana is bad"
    }
    
    POST test_david_zhao/_doc
    {
      "text": "dirty grape is bad"
    }
    

    查询

    POST test_david_zhao/_search
    {
      "query": {
        "query_string": {
          "default_field": "text",
          "query": "+(text:apple text:banana text: grape) +(text:tasty text:nasty)"
        }
      }
    }
    

    回应

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 2.261763,
        "hits" : [
          {
            "_index" : "test_david_zhao",
            "_type" : "_doc",
            "_id" : "d6t3_HcBDMyXCx985YKJ",
            "_score" : 2.261763,
            "_source" : {
              "text" : "nasty banana is bad"
            }
          },
          {
            "_index" : "test_david_zhao",
            "_type" : "_doc",
            "_id" : "dKt3_HcBDMyXCx9804Le",
            "_score" : 1.9252907,
            "_source" : {
              "text" : "tasty apple is good"
            }
          },
          {
            "_index" : "test_david_zhao",
            "_type" : "_doc",
            "_id" : "dat3_HcBDMyXCx982oIq",
            "_score" : 1.4144652,
            "_source" : {
              "text" : "tasty grape is good"
            }
          },
          {
            "_index" : "test_david_zhao",
            "_type" : "_doc",
            "_id" : "dqt3_HcBDMyXCx984IJD",
            "_score" : 1.4144652,
            "_source" : {
              "text" : "tasty banana is good"
            }
          }
        ]
      }
    }
    

    【讨论】:

    • 感谢您的回答!这里我想在query_string中使用phrase slop参数,所以需要使用phrase模式。这个查询似乎不能使用 slop 来定义美味和苹果之间的最小距离。
    猜你喜欢
    • 1970-01-01
    • 2019-12-17
    • 2017-08-20
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2013-10-12
    • 2016-07-28
    • 2015-01-08
    相关资源
    最近更新 更多