Elasticsearch将查询与布尔查询相结合

时间:2014-06-19 23:00:32

标签: java elasticsearch

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

如果我有以下文件(其中包括):

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
}

如果我运行以下查询(使用关键字&#34获取所有文档和方面; 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中实现的(如果有这个帮助)。 谢谢! (这个巨大的帖子的索里)

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误。 (说实话新手的错误)

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

谢谢你的一切!!