Search among field names in Elasticsearch

Is it possible to search on some fields with increased weights if a field name is present in search query?
For example if an index has the following fields:

PUT /my_index
{
"mappings": {
    "_doc": {
        "properties": {
            "name": {
                "type": "keyword"
            },
            "pistons": {
                "type": "keyword"
            },
            "cylinders": { "type": "integer" }
        }
    }
}
}

and the search query is

10 cylinders

is there a way in Elasticsearch to focus this search to the "cylinders" field, since that field was mentioned on the query?

Or if the search is for

metal pistons

can we narrow this search to the pistons field, based on the contents of the search query?
Thanks in advance.

Do you mean that you only want the fields that you query as result ?

you can do that by :

{  
   "_source": "pistons",
   "query":{  
    "term":{"pistons":"metal"}
   }
  
}

This query will return only pistons which match with the value "metal" , it will not return the whole document .

1 Like

@Tosh I want to search both fields. But I want to narrow the search based on he name of the field in the search query. So if the query says "10 pistons", it should effectively consider "_source": "pistons", but if the query has "8 cylinders", it should search like this. "_source": "pistons".

I can do this manually by changing the "_source" based on the query, however I was looking for a built in approach.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.