a.k
a.k

Reputation: 1203

dynamically searching in multi/all elasticsearch nested field

I'm using spring-data-elasticsearch 2.0.8 and elasticsearch.2.2.0 want to search nested objects dynamically.

Basically my nested object could have few nested fields, but I would like to search in all of those fields - dynamically. for example an animal document can have 3x fields that describe him: name/size/description.

I would like to search in all of those, as my search end point just has a 'Description' free-text option. so when a user types '15' or 'dog' in his entry point, search will check 'name', 'size' and 'description' field and will return something from it.

My mapping looks like:

{
    "mappings": {
        "animal_doc": {
            "properties": {
                "animal_description": {
                    "type": "nested",
                    "include_in_parent": true,
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "size": {
                            "type": "string"
                        },
                        "description": {
                            "type": "string"
                        }
                    }
                },
           }
      }
  }

I have read both: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html and https://www.elastic.co/blog/managing-relations-inside-elasticsearch

as this can be solved by creating a query with (spring-data-es):

boolQuery.must(QueryBuilders.nestedQuery("animal_description", 
addMatchQuery("animal_description."+field, value)));

Thus each time I will query for Object.nestedField : value But I do not want to specify the field each time, and want it to be dynamically - inside the nested 'animal_description' field - all inner fields will be searched as well.

A desired solution would look like:

boolQuery.must(QueryBuilders.nestedQueryGetAllFields("animal_description",value)));

Thanks!

Upvotes: 1

Views: 1107

Answers (1)

Green
Green

Reputation: 2565

From your description it seem that you might want to use a general query string. When you query few specific fields. For example, lets say you want to search a general

animal description

Your query would look like:

GET /_search
{
    "query": {
        "query_string" : {
            "fields" : ["properties.size", "properties.name", "properties.description"],
            "query" : "animal description"
        }
    }
}

The options you have using this query are very vast and I recommend you to check them out here

Upvotes: 1

Related Questions