0

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 ?

2 Answers 2

2

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"
         }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @anthonybruni , "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)" this answer was exactly what I was asking for
0

Try:

GET my_index/_search?q=paul

Comments

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.