Syed Rafi
Syed Rafi

Reputation: 915

Elastic Query Nested Query

Step 1:

Created an index on elastic search http://localhost:9200/shop with below mapping.json

{
  "cloth" : 
  {
      "properties" : 
      {
          "name" : { "type" : "string", "index" : "analyzed" },
          "variation" : 
          {
            "type" : "nested", 
            "properties" : 
            { 
                "size" : 
                { 
                    "type" : "string", "index" : "not_analyzed"
                },
                "color" : 
                {
                    "type" : "string", "index" : "not_analyzed"
                }
            }
        }
    }
  }
}

GET: http://localhost:9200/shop/_mapping/cloth

HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 518

{"shop":{"mappings":{"cloth":{"properties":{"cloth":{"properties":{"properties":{"properties":{"name":{"properties":{"index":{"type":"string"},"type":{"type":"string"}}},"variation":{"properties":{"properties":{"properties":{"color":{"properties":{"index":{"type":"string"},"type":{"type":"string"}}},"size":{"properties":{"index":{"type":"string"},"type":{"type":"string"}}}}},"type":{"type":"string"}}}}}}},"name":{"type":"string"},"variation":{"properties":{"color":{"type":"string"},"size":{"type":"string"}}}}}}}}

Step 2:

Inserted the data with given below data.json http://localhost:9200/shop/cloth/?_create

{
"name" : "Test shirt",
"variation" : [
{ "size" : "XXL", "color" : "red" },
{ "size" : "XL", "color" : "black" }
]
}

Step 3:

Tried searching with given query.json

http://localhost:9200/shop/cloth/_search

{
"query" : {
"nested" : {
"path" : "variation",
"query" : {
"bool" : {
"must" : [
{ "term" : { "variation.size" : "XXL" } },
{ "term" : { "variation.color" : "black" } }
]
}
}
}
}
}

Below error is followed

HTTP/1.1 400 Bad Request Content-Type: application/json; charset=UTF-8 Content-Length: 519

{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"shop","node":"6U9SA_SDRJKfw1bRxwH8ig","reason":{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}}]},"status":400}

What is the way to search with queries nested ? Is there any proper method to load mapping file into search cluster ?

Upvotes: 1

Views: 976

Answers (2)

smithyj
smithyj

Reputation: 113

{
    "query" : {
        "nested" : {
            "path" : "cloth.variation",
            "query" : {
                "bool" : {
                    "must" : [
                        { "term" : { "cloth.variation.size" : "XXL" } },
                        { "term" : { "cloth.variation.color" : "black" } }
                    ]
                }
            }
        }
    }
}

Upvotes: 0

Val
Val

Reputation: 217254

I think you're not properly creating your index with the cloth mapping. Do it this way:

# delete your index first
curl -XDELETE localhost:9200/shop

# create it properly
curl -XPUT localhost:9200/shop -d '{
  "mappings": {
    "cloth": {
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed"
        },
        "variation": {
          "type": "nested",
          "properties": {
            "size": {
              "type": "string",
              "index": "not_analyzed"
            },
            "color": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}'

Upvotes: 1

Related Questions