0
votes

I'm trying to get ElasticSearch to work on my box. 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'.

Checking that sneakers exist, running curl -XGET 'http://localhost:9200/sneakers/sneaker/1?pretty', I get:

{
  "_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:

{
  "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:

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. I am running ES 2.3.1.

Any idea's why Query DSL isn't working? What am I doing wrong?

1

1 Answers

1
votes

You've mapped the brand field as a nested type, so you need to query it with a nested query, like this:

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.