Pauli
Pauli

Reputation: 548

Elasticsearch nested object query_string

I have question about query_string query in ElasticSearch. I want create fulltext search over all types and fields in index. Is query_string string performed against nested objects ? For example I have this mapping

 {
  "my_index": {
    "mappings": {
      "my_type": {
        "properties": {
          "group": {
            "type": "string"
          },
          "user": {
            "type": "nested",
            "properties": {
              "first": {
                "type": "string"
              },
              "last": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

And the query

GET /my_index/_search
{
  "query": {
      "query_string" : {
          "query" : "paul"
      }
    }

} 

So when I call the query, will ES search across all fields including nested or only in my_type object and for nested search I will have to use nested query ?

Upvotes: 0

Views: 1971

Answers (2)

anthonybruni
anthonybruni

Reputation: 36

You cannot reference nested fields from a query_string at the root. i.e. this won't work:

{
    "query": {
         "query_string": {
          "query": "myNestedObj.myTextField:food"
         }
   }
}

To search in specific nested fields, you must use the nested clause:

{
    "query": {
      "nested": {
        "path": "myNestedObj",
        "query": {
         "query_string": {
          "query": "myNestedObj.myTextField:food"
         }
        }
      }
    }
  }
}

However, I've found that the pseudo-field "_all" does include nested fields, so this query would find documents containing 'food' in myNestedObj.myTextField (as well as anywhere else)

{
    "query": {
         "query_string": {
          "query": "_all:food"
         }
   }
}

Upvotes: 2

core
core

Reputation: 869

Try:

GET my_index/_search?q=paul

Upvotes: 0

Related Questions