Search code examples
elasticsearchelasticsearch-6

Multiple elasticsearch match queries


Say I have a document with 3 text fields: field_a , field_b and field_c. Is it possible to do a single query so that we have results in this order:

  1. 'match' in field_a
  2. 'match' in field_b
  3. 'match' in field_c

'mutli_match' results can have results from different fields mixed together in the order of the results, what I want is any and all results from field_a, then any and all results from field_b and so on.


Solution

  • Even though, I find this approach strange in general (I think the problem you have should be solved in a different way, e.g. multiple stages of search), I think you could solve it for now in a following manner.

    Multi match query have a perfect ability to provide boost to your fields. E.g.

    "query": {
        "multi_match" : {
          "query" : "this is a test",
          "fields" : [ "field_a^1000", "field_b^10", "field_c" ] 
        }
      }
    

    The sign ^ is a boost sign which will multiple score of the match in this field by the value - 1000 in case of field_a

    However, I would recommend to avoid this sort of behavior in general, since:

    • It's hard to control those boosting values
    • It could be in some cases behaving not as expected (imagine you get the score of 1000 in field_b)

    • If you would have many hits, this makes whole idea of having match of field_c kinda obsolete, since no user will scroll that far away in search results