Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch query_string nested query

I am trying to write a query using query_string to retrieve data querying by nested objects.

An example of query I would like to do is this one:

{
  "query": {
    "query_string": {
      "query": "a.id:2"
    }
  }
}

Where "a" is a nested object, and "id" is a field of "a".

I know I can successfully perform this task using using a nested query, writing a query like:

{
  "nested": {
    "path": "a"
    "query_string": {
      "query": "a.id:2"
    }
  }
}

However, I would like to avoid it. I don't want to figure out by myself that the user is searching for a nested field and modify the query. I tried to use the "fields" parameter, but it looks like it doesn't work with nested objects.

Is it possible to write this query directly using "query_string" queries? What semantic is it possible to obtain? (for instance, if I write "a.id:2 AND a.b:10" I am matching the two fields in the same object or in different objects?)

like image 370
Cale Avatar asked Feb 09 '15 17:02

Cale


1 Answers

I was doing more research and found this can be achieved by setting the include_in_parent setting to true in the mapping.

Now you should be able to do a query like

{
  "query": {
    "query_string": {
      "query": "fields.fieldvalue:sometext"
    }
  }
}

Eg:-

"mappings": {
         "documentmetadatavalue": {
            "properties": {
              "id": {
                  "type": "string"
               },
               "fields": {
                 "type": "nested",
                 "include_in_parent": true, 
                 "properties": {
                   "fieldId": {"type": "string"},
                   "fieldvalue": {"type": "string"}
                 }
               }
           }
        }
     }

Let me know if that helps.

like image 127
Vineet Avatar answered Sep 30 '22 13:09

Vineet