3

I am having a lot of issues with the nested type mapping in Elasticsearch, I have ran this to create my index:

curl -XPOST 'http://localhost:9200/thread_and_messages' -d 
'{"mappings" : {
    "message": {
        "properties": {
            "messages": {
                "type": "nested", 
                "include_in_parent": true, 
                "properties": {
                    "message_id": {"type": "string"}, 
                    "message_text": {"type": "string"}, 
                    "message_nick": {"type": "string"}
                }
            }
        }
    }
}}'

Then this is how I've indexed a document:

curl -XPUT 'http://localhost:9200/thread_and_messages/thread/1' -d 
'{
    "thread_id":"2", 
    "thread_name":"Windows", 
    "created":"Wed Mar 25 2015", 
    "first_nick":"Admin", 
    "messages":[
        {"message_id":"5", "message_text":" Pc with a mouse", "message_nick":"Admin"},
        {"message_id":"6", "message_text":"Keyboard", "message_nick":"Admin"},
        {"message_id":"7", "message_text":"iPhone", "message_nick":"Admin"},
        {"message_id":"8", "message_text":"Gym", "message_nick":"Admin"}]"
}'

This is my query:

curl -XGET 'http://localhost:9200/thread_and_messages/thread/_search' -d 
'{"query": {
    "bool": {
        "must": [
            {"match": {"thread_name": "windows"}}, 
            {"nested": {
                "path": "messages", "query": {
                "bool": {
                    "must": [{
                        "match": {"messages.message_text": "gym"}
                    }]
                }
                }
            }}
        ]}
    }
}'

I am receiving this error, even though I have clearly mapped messages as a nested type:

QueryParsingException[[thread_and_messages] [nested] nested object under path [messages] is not of nested type

1 Answer 1

5

It's because you defined your mapping for the type "message", but then indexed your document as the type "thread". So the "thread" type is dynamically created, but not with a nested sub-type. I ran your code as posted, then looked at the mapping:

GET /test_index/_mapping
...
{
   "test_index": {
      "mappings": {
         "message": {
            "properties": {
               "messages": {
                  "type": "nested",
                  "include_in_parent": true,
                  "properties": {
                     "message_id": {
                        "type": "string"
                     },
                     "message_nick": {
                        "type": "string"
                     },
                     "message_text": {
                        "type": "string"
                     }
                  }
               }
            }
         },
         "thread": {
            "properties": {
               "created": {
                  "type": "string"
               },
               "first_nick": {
                  "type": "string"
               },
               "messages": {
                  "properties": {
                     "message_id": {
                        "type": "string"
                     },
                     "message_nick": {
                        "type": "string"
                     },
                     "message_text": {
                        "type": "string"
                     }
                  }
               },
               "thread_id": {
                  "type": "string"
               },
               "thread_name": {
                  "type": "string"
               }
            }
         }
      }
   }
}

When I changed "message" to "thread" in the mapping and started over, your query worked fine.

Here is the code I used to test it:

http://sense.qbox.io/gist/8a06b7849cf49006afd464ed4ee5b4e770759d5a

Sign up to request clarification or add additional context in comments.

1 Comment

The sense.qbox.io link is dead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.