繁体   English   中英

查询DSL Elasticsearch不起作用

[英]Query DSL elasticsearch doesn't work

我正在尝试让ElasticSearch在我的盒子上工作。 我有以下映射:

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

因此,我有一个“运动鞋”类型的“运动鞋”索引,其“品牌”属性具有一个“ id”和一个“ title”。

检查运动鞋是否存在,运行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"
    }
  }
}

现在,运行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"
        }
      }
    }, ...
}

但是,一旦我开始像这样使用Query DSL:

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

我懂了

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

以某种方式,即使运行最简单的查询,查询DSL也不会返回任何内容。 我正在运行ES 2.3.1。

知道为什么Query DSL无法正常工作吗? 我究竟做错了什么?

您已将brand字段映射为nested类型,因此需要使用nested查询对其进行查询 ,如下所示:

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

注意:如果从映射中删除"type": "nested" ,则查询将起作用。

暂无
暂无

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

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