34

I've been trying to search on my document which contains a nested field. I created the nested mapping like this:

{
  "message": {
    "properties": {
      "messages": {
        "type": "nested",
        "properties": {
          "message_id": { "type": "string" },
          "message_text": { "type": "string" },
          "message_nick": { "type": "string" }
        }
      }
    }
  }
}

My search looks like this:

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

Yet I am receiving this error message:

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

EDIT

I am still receiving this error. I am doing this via Java so this is the document I am trying to create:

{
  "_id": {
    "path": "3",
    "thread_id": "3",
    "thread_name": "Banana",
    "created": "Wed Mar 25 2015",
    "first_nick": "AdminTech",
    "messages": [
      {
        "message_id": "9",
        "message_text": "Banana",
        "message_nick": "AdminTech"
      }
    ]
  }
}

Creating the index like so:

CreateIndexRequestBuilder indexRequest = client.admin().indices().prepareCreate(INDEX).addMapping("message", mapping);

I think I am possibly indexing the document incorrectly.

Emma Burrows
  • 4,734
  • 1
  • 20
  • 24
Daniel Buckle
  • 628
  • 1
  • 9
  • 19

3 Answers3

32

TLDR: Put "type": "nested", in your nested type.

Say we have a normal type, and another type nested in it:

{
   "some_index": {
      "mappings": {
         "normal_type": {
            "properties": {
               "nested_type": {
                  "type": "nested",
                  "properties": {
                     "address": {
                        "type": "string"
                     },
                     "country": {
                        "type": "string"
                     }
                  }
               },
               "first_name": {
                  "type": "string"
               },
               "last_name": {
                  "type": "string"
               }
            }
         }
      }
   }
}

The "type": "nested", line is required for the nested queries to work which have "path": assigned to nested_type, like this:

GET /some_index/normal_type/_search
{
  "query": {
    "nested": {
      "query": {
        "bool": {}
      },
      "path": "nested_type"
    }
  }
}

The "type": "nested", line seems to be required in newer Elasticsearch versions only (since 1.1.1 ?).

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
0

Syntax error in query DSL. Incorrect closing for must block query->bool->must

{
    "query": {
        "bool": {
                "must": [

            }// Should be ] 
        }
    }
}

Correct version query are :

curl -XGET 'localhost:9200/thread_and_messages/thread/_search' -d '{
   "query": {
      "bool": {
         "must": [
            {
               "match": {
                  "thread_name": "Banana"
               }
            },
            {
               "nested": {
                  "path": "messages",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "messages.message_text": "Banana"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}'
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
Roopendra
  • 7,674
  • 16
  • 65
  • 92
0

If your field is type object you will need to use the flatten name <object name>.<object key> has if it was a normal variable. For example:

Not this

{
  "nested":
  {
    "path":"album",
    "query":{
      "bool":{
        "boost":5,
        "should":[{"match":{"album.name":"lady"}}]
      }
    }
  }
},

Yes to this

{
  "match":{
    "genre":{
      "query":"lady",
      "boost":2
    }
  }
},

In meme form: enter image description here

Royer Adames
  • 868
  • 9
  • 13