0

Let's say I have three indices: cities, museums and sights.

Now I am querying on all indices (_all) for a term, for example "Vienna"

'query' :{
    'fuzzy' : {
        'name': 'vienna'
    }
}

and as results I get:

  1. Vienna: Vienna Art Museum (Museum)
  2. Vienna: Vienna History Museum (Museum)
  3. Vienna's Riesenrad (Sight)
  4. Vienna (cities)

Is there a way to prioritize the indices, so that I get first cities, than sights and last the museums, something like this:

  1. Vienna (cities)
  2. Vienna's Riesenrad (Sight)
  3. Vienna: Vienna Art Museum (Museum)
  4. Vienna: Vienna History Museum (Museum)

1 Answer 1

0

You can do it like this, by adding a boost for each index in the desired order:

{
  "query": {
    "bool": {
      "must": {
        "fuzzy": {
          "name": "vienna"
        }
      },
      "should": [
        {
          "term": {
            "_index": {
              "value": "cities",
              "boost": 3
            }
          }
        },
        {
          "term": {
            "_index": {
              "value": "sights",
              "boost": 2
            }
          }
        },
        {
          "term": {
            "_index": {
              "value": "museums",
              "boost": 1
            }
          }
        }
      ]
    }
  }
}
2
  • 1
    Keep in mind this might not always work. Documents are scored based on partially on term frequency (notice how the docs with Vienna in them twice came first). This could cause records from sights or museums to still come first. Furthermore, the score from the term queries above will be affected by the number of docs in each index. If, for example, there are lots of documents in the cities index, and only a few in sights and museum, it's possible that a match on the latter two indexes could result in a high enough score that results come out of order. Oct 23, 2018 at 19:41
  • @orangejulius Yes, of course, however without more information on data, shard count, etc, this is good enough for what the OP is asking. If he encounters further issues, he'll feel free to create a new question with more detailed information ;-)
    – Val
    Oct 23, 2018 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.