2

I have a query with different query data for different fields and ORed results. I also want to favor hits with certain fields. Ideally this would only increase ranking but would not cause results that did not contain some of the terms in the other fields. This would skew results towards those that have certain fields.

I think this used to be called a boost but since boost has been removed from Lucene Elasticsearch has replaced them with function scores and I don't understand how to add them to my query.

The query looks like this:

POST /index/type/_search
{
    "query": {
        "bool": {
            "should": [{
                "terms": {
                    "field1": ["67", "93", "73", "78", "88", "77"]
                }
            }, {
                "terms": {
                    "field2": ["68", "94", "72", "76", "82", "96", "70", "86", "81", "92", "97", "74", "91", "85"]
                }
            }, {
                "terms": {
                    "category": ["cat2"]
                }
            }]
        }
    }
}

Of all possible hits, I'd like to skew ranking towards those with terms from the catagory field.

sehe
  • 374,641
  • 47
  • 450
  • 633
pferrel
  • 5,673
  • 5
  • 30
  • 41
  • I think you need to re-formulate the problem: what do you want to achieve given a certain mapping and some documents? You are mentioning boost (but boost exists in ES) and function_score, but are you sure this is the solution to the problem? What is the problem? Can you, please, reformulate the issue and say what you have, what you need to achieve and what have you tried so far and why you didn't like what you tried? – Andrei Stefan Jun 17 '15 at 16:34
  • For example, you could use boosting fields with query dsl: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html – Andrei Stefan Jun 17 '15 at 16:38

1 Answers1

2

The _boost field (document-level boost) was removed, but field-level boosts, and query boosts still work just fine. It looks like you are looking for query boosts.

So, if you wanted to boost matches on field1:

"bool": {
    "should": [{
        "terms": {
            "field1": ["67", "93", "73", "78", "88", "77"],
            "boost": 2.0
        }
    }, {
        "terms": {
            "field2": ["68", "94", "72", "76", "82", "96", "70", "86", "81", "92", "97", "74", "91", "85"]
        }
    }, {
        "terms": {
            "category": ["cat2"]
        }
    }]
}
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • Moved the boost to the correct field but tested and works perfectly. Is it possible to also filter based on containing a set of terms in a field, like boosting with a very large number I guess? – pferrel Jun 19 '15 at 00:25
  • @pferrel Not sure I follow. If you want to get *only* results matching that field, you would want to add them to the `"must"` clauses of the bool query, instead of should. Is that what you mean? A [terms filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html) would also work (result set would be the same, but scoring and performance may be a bit different). – femtoRgon Jun 19 '15 at 03:55