Getting an error when using boost for multiple fields

Hello,

When i am trying to add boost for multiple queries :
POST /new_master_query/_search
{
"query": {
"match" : {
"title": {
"query": "test",
"boost": 2
},
"object_summary":{
"query":"Test",
"boost": 1
}
}
}
}

but i am getting an error:
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[match] query doesn't support multiple fields, found [title] and [object_summary]",
"line": 8,
"col": 30
}

Kindly guide me on this.

Thanks,
Priyanka

If the text you are searching for is the same, use multi_match query, if not use a bool query with a must array with all your match queries inside.

Hello @dadoonet,

Thanks for reply!!

Is it possible to apply boost on multiple fields without adding query parameter?
if yes then how?

Regards,
Priyanka

Yes. The documentation says it. In case you did not read it, here it is:

GET /_search
{
  "query": {
    "multi_match" : {
      "query" : "this is a test",
      "fields" : [ "subject^3", "message" ] 
    }
  }
}

Hello @dadoonet,

Thanks for reply!!!

I do not want to add query parameter. In above example query is present. I only want to add different boost to different fields. for example: for title, value for boost is 3, for object_summary it is 2 and so on. Can this is possible?

Regards,
Priyanka

I don't understand then. If you don't query for anything by not "adding the query", what do you want to search for????

A full example could help to understand.

Hello @dadoonet ,

My requirement is when user searches any term or word, depending upon boost number of fields it should display the results.

For example, if user searches for “Test” and title has boost value as 2 and object_summary has boost value as 1, then according to boost number more weightage should be given to title (as it has max boost value). And user will get the proper results. That means it should first search in title field and then in object_sumary.

Is this possible using boost? If not what exactly boost does?

Regards,

Priyanka

Yes. That's I think the example I gave.

If you still don't make it work, could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Hello @dadoonet,

I am getting what you are trying to say. Still I have one doubt, in below example, I have used 2 fields “title" and "object_summary and boost value is 2. So when user searches they will get results by applying boost value as 2 for both fields.

GET new_master_query/_search
{
"query": {
"multi_match" : {
"query": "this is a test",
"fields": [ "title", "object_summary" ] ,
"boost":2
}
}
}

What if I want to give boost value 2 to title and 1 to object_summary? Or is it assumed that if we have provided multiple fields then boost value for title is 2 and object_summary is 1?

Regards,
Priyanka

If you just follow the example I gave and the documentation I linked to you'll see that you can write:

GET new_master_query/_search
{
  "query": {
    "multi_match" : {
      "query": "this is a test",
      "fields": [ "title^2", "object_summary" ] 
    }
  }
}

Hello @dadoonet ,

Thanks for your reply!!!

As mentioned in above example, 2 is representing boost value for title. And that means when any user searches , first it will give preference to title and then to object _summary and will show the results. Correct me if I am wrong.

Regards,
Priyanka

This is correct. More or less.
It will multiply by 2 the score computed for title.

Hello @dadoonet,

Thanks for your reply!!!

What if I have used boost as 3? Please refer below example for the same. In that I have used segmentname^3.

GET new_master_query/_search
{
"query": {
"multi_match" : {
"query": "Geosolutions",
"fields": [ "segmentname^3", "object_summary^2","title"] }
}
}

Regards,
Priyanka

Yes.

Hello @dadoonet ,

Thanks for your reply!!!

I have used segmentname^3. After running it score is doubling. It should be thrice? Correct me if I am wrong.

Thanks,
Priyanka

Try this:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "Geosolutions",
      "type":       "most_fields",
      "fields":     [ "segmentname^3", "object_summary^2","title" ]
    }
  }
}

Note that the boost is computed per field.
If you need more details about how the score is computed add explain: true: https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-request-explain.html

Hello @dadoonet ,

Great!!! Thank you so much!!

Is there any relevancy used other than boost and Score in ES?

Thanks,
Priyanka

Not sure I got the question but may be you want this? https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html

Hello @dadoonet,

I don’t want to change the score of the document. Actually I want to apply relevancy to my indexes so that user will get better results.

Thanks,
Priyanka

So relevancy is based on score computation nowadays. But may be this as well can help: https://www.elastic.co/guide/en/elasticsearch/reference/7.1/query-dsl-rank-feature-query.html (I never used it yet).