简体   繁体   English

查询DSL Elasticsearch不起作用

[英]Query DSL elasticsearch doesn't work

I'm trying to get ElasticSearch to work on my box. 我正在尝试让ElasticSearch在我的盒子上工作。 I have the following mapping: 我有以下映射:

{
  "sneakers" : {
    "mappings" : {
      "sneaker" : {
        "properties" : {
          "brand" : {
            "type" : "nested",
            "properties" : {
              "id" : {
                "type" : "integer",
                "index" : "no"
              },
              "title" : {
                "type" : "string"
              }
            }
          }
        }
      }
    }
  }
}

So I have a 'sneakers' index with a 'sneaker' type, with a 'brand' property that has an 'id' and a 'title'. 因此,我有一个“运动鞋”类型的“运动鞋”索引,其“品牌”属性具有一个“ id”和一个“ title”。

Checking that sneakers exist, running curl -XGET ' http://localhost:9200/sneakers/sneaker/1?pretty ', I get: 检查运动鞋是否存在,运行curl -XGET'http :// localhost:9200 / sneakers / sneaker / 1?pretty ',我得到:

{
  "_index" : "sneakers",
  "_type" : "sneaker",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "brand" : {
      "id" : 1,
      "title" : "Nike"
    }
  }
}

Now, runningcurl -XGET ' http://localhost:9200/sneakers/_search?q=brand.title=adidas&pretty ' I get: 现在,运行curl -XGET'http :// localhost:9200 / sneakers / _search?q = brand.title = adidas&pretty '我得到:

{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1330,
    "max_score" : 0.42719018,
    "hits" : [ {
      "_index" : "sneakers",
      "_type" : "sneaker",
      "_id" : "19116",
      "_score" : 0.42719018,
      "_source" : {
        "brand" : {
          "id" : 2,
          "title" : "Adidas"
        }
      }
    }, ...
}

But as soon as I start using the Query DSL as such: 但是,一旦我开始像这样使用Query DSL:

curl -XGET 'http://localhost:9200/sneakers/_search?pretty' -d '{
    "query" : {
        "term" : { "brand.title" : "adidas" }
    }
}
'

I get 我懂了

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

Somehow Query DSL never returns anything, even running the most simple queries. 以某种方式,即使运行最简单的查询,查询DSL也不会返回任何内容。 I am running ES 2.3.1. 我正在运行ES 2.3.1。

Any idea's why Query DSL isn't working? 知道为什么Query DSL无法正常工作吗? What am I doing wrong? 我究竟做错了什么?

You've mapped the brand field as a nested type, so you need to query it with a nested query , like this: 您已将brand字段映射为nested类型,因此需要使用nested查询对其进行查询 ,如下所示:

curl -XGET 'http://localhost:9200/sneakers/_search?pretty' -d '{
  "query" : {
    "nested": {
        "path": "brand",
        "query": {
           "term" : { "brand.title" : "adidas" }
        }
    }
  }
}
'

Note: Your query would work if you remove the "type": "nested" from your mapping. 注意:如果从映射中删除"type": "nested" ,则查询将起作用。

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

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