【问题标题】:Elasticsearch combining queries with Boolean queryElasticsearch 将查询与布尔查询相结合
【发布时间】:2023-03-10 04:25:01
【问题描述】:

我正在尝试使用布尔查询在弹性搜索中组合多个查询,但结果不是我所期望的。例如:

如果我有以下文件(除其他外):

DOC 1:
{
   "name":"Iphone 5",
   "product_suggestions":{
      "input":[
         "iphone 5",
         "apple"
      ]
   },
   "description":"Iphone 5 - The almost last version",
   "brand":"Apple",
   "brand_facet":"Apple",
   "state_id":"2",
   "user_state_description":"Almost New",
   "product_type_id":"1",
   "current_price":350,
   "finish_date":"2014/06/20 14:12",
   "finish_date_ms":1403273520
}

DOC 2:
{
   "name":"Apple II Lisa",
   "product_suggestions":{
      "input":[
         "apple ii lisa",
         "apple"
      ]
   },
   "description":"Make a offer and I Apple II Lisa!!",
   "brand":"Apple",
   "brand_facet":"Apple",
   "state_id":"2",
   "user_state_description":"Used",
   "product_type_id":"1",
   "current_price":150,
   "finish_date":"2014/06/15 16:12",
   "finish_date_ms":1402848720
}

DOC 3:
{
   "name":"Iphone 5s",
   "product_suggestions":{
      "input":[
         "iphone 5s",
         "apple"
      ]
   },
   "description":"Iphone 5s 32Gb like new with a few scratches bla bla bla",
   "brand":"Apple",
   "brand_facet":"Apple",
   "state_id":"1",
   "user_state_description":"New",
   "product_type_id":"2",
   "current_price":510.1,
   "finish_date":"2014/06/10 14:12",
   "finish_date_ms":1402409520
}

DOC 4:
{
   "name":"Iphone 4s",
   "product_suggestions":{
      "input":[
         "iphone 4s",
         "apple"
      ]
   },
   "description":"Iphone 4s 16Gb Mint conditions and unlocked to all network",
   "brand":"Apple",
   "brand_facet":"Apple",
   "state_id":"1",
   "user_state_description":"Almost New",
   "product_type_id":"2",
   "current_price":385,
   "finish_date":"2014/06/12 16:12",
   "finish_date_ms":1402589520
}

如果我运行以下查询(使用关键字“Apple”获取finish_date_ms大于1402869581的所有文档和构面)

{
  "from" : 1,
  "size" : 20,
  "query" : {
    "bool" : {
      "must" : {
        "query_string" : {
          "query" : "apple",
          "default_operator" : "and",
          "analyze_wildcard" : true
        }
      },
      "must_not" : {
        "range" : {
          "finish_date_ms" : {
            "from" : null,
            "to" : 1402869581,
            "include_lower" : true,
            "include_upper" : false
          }
        }
      }
    }
  },
  "facets" : {
    "brand" : {
      "terms" : {
        "field" : "brand_facet",
        "size" : 10
      }
    },
    "product_type_id" : {
      "terms" : {
        "field" : "product_type_id",
        "size" : 10
      }
    },
    "state_id" : {
      "terms" : {
        "field" : "state_id",
        "size" : 10
      }
    }
  }
}

这会返回:

{
   "took":5,
   "timed_out":false,
   "_shards":{
      "total":5,
      "successful":5,
      "failed":0
   },
   "hits":{
      "total":1,
      "max_score":0.18392482,
      "hits":[

      ]
   },
   "facets":{
      "brand":{
         "_type":"terms",
         "missing":0,
         "total":1,
         "other":0,
         "terms":[
            {
               "term":"Apple",
               "count":1
            }
         ]
      },
      "product_type_id":{
         "_type":"terms",
         "missing":0,
         "total":1,
         "other":0,
         "terms":[
            {
               "term":1,
               "count":1
            }
         ]
      },
      "state_id":{
         "_type":"terms",
         "missing":0,
         "total":1,
         "other":0,
         "terms":[
            {
               "term":2,
               "count":1
            }
         ]
      }
   }
}

并且应该只返回文档 DOC1。如果我删除范围查询,则返回所有包含 Apple 单词的文档。如果我删除“术语”查询,则返回 n 个文档,所以我认为问题出在范围查询中。

谁能指出我正确的方向?

还有一件重要的事情,所有这些查询都将在 java 中实现(如果有帮助的话)。 谢谢! (对不起这个巨大的帖子)

【问题讨论】:

  • 在 MUST 子句中您要查询的字段...我看不到
  • 我要查询所有字段,所以我没有放字段名。

标签: java elasticsearch


【解决方案1】:

我发现了我的错误。 (老实说新手错误)

问题不在于范围查询,而在于Json的乞求:from字段设置为1但结果只有一条记录,所以这应该是0!!

谢谢你的一切!!

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 2017-09-18
    • 2017-05-22
    • 2017-06-09
    • 2011-10-02
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多