0
votes

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 ?

2
Can you update your question with the output you get form curl -XGET localhost:9200/shop/_mapping/cloth ?Val
How we can insert the mapping, as am using as POST with mapping.json contentSyed Mazreena
my bad, sorry, please check my above comment again.Val
Please run this: curl -XGET localhost:9200/shop/_mapping/cloth I don't think it was the caseVal
@Val I have simple question: If the declare more Fields in the mapping file and post less columns on the index DB and Perform search on the Index Whether it throws same error as the above question ?Syed Mazreena

2 Answers

1
votes

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"
            }
          }
        }
      }
    }
  }
}'
0
votes
{
    "query" : {
        "nested" : {
            "path" : "cloth.variation",
            "query" : {
                "bool" : {
                    "must" : [
                        { "term" : { "cloth.variation.size" : "XXL" } },
                        { "term" : { "cloth.variation.color" : "black" } }
                    ]
                }
            }
        }
    }
}