Boost results matching terms query without filtering in elasticsearch
Asked Answered
S

0

2

Context : I'm relatively new to ElasticSearch, so maybe there's a concept I'm missing.

I'd like to boost the relevance score of documents that have one or more tags (the more tags, the better the score), without filtering out the documents that do not have any tag.

Attempted solutions :

I tried to use a terms query :

{
  "query": {
    "terms": {
      tags: ['some','tags','to','boost'],
      minimum_should_match: 0
    }
  }
}

But the minimum_should_match option seems to be ignored (the doc is not quite clear on this, but I think this option MUST be different from zero).

I also tried to "cheat" a bit using a bool should query with match_all:

{
  "query": {
    "bool": {
      "should" : [
        {"terms": {tags: ['some','tags','to','boost']}},
        {"match_all": {}}
      ]
    }
  }
}    

But for some reason the results not having any tag get filtered out anyway.

I also tried to use a function_score or a boosting_query but could not figure out a valid syntax that preserves the relevance score generated by the terms query.

Edit :

There seems to be a working solution in the spirit of the "bool cheat" :

{
  "query": {
    "dis_max": {
      "queries" : [
        {"terms": {tags: ['some','tags','to','boost']}},
        {"match_all": {}}
      ]
    }
  }
} 

But :

  • this feels hackish,
  • I don't know how to control precisely the relevance generated in this case

Question : I feel that this must be a common and simple use case, so is there a way to use a terms query without filtering out the documents that don't match ?

Swellhead answered 31/7, 2015 at 9:1 Comment(2)
gives a good explanation about minimum_should_match i.e a boolean query without a required clause should match at least one optional clause. however i'm not sure why your first so called "bool cheat" mentioned in OP doesn't work , seems to work for meNunhood
sorry for the delay, back from holidays ;) I think that this has to do with a gem that I use (I'm using Rails with Chewy, which is based on the "official" elasticsearch gem). Somehow the library builds a weird query that does not quite match what I did expect, will return later if I find out moreSwellhead

© 2022 - 2024 — McMap. All rights reserved.