0

I have a problem understanding the operation of the boost parameter in Elasticsearch 6.
I have an index with four fields: id, title, content, client. All fields are of type "text".
With the following query I try to give the title field a higher weight:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "europe",
            "analyzer": "standard",
            "default_operator": "AND",
            "fields": [
              "id", "title^2", "content"
            ]
          }
        },
        {
          "term": {
            "client": {
              "value": "test",
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "size": 10,
  "from": 0,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

What I would expect now would be that I get search results where the first hits are only records that contain the search term in the title, but not necessarily in the content. However, I only get hits that contain the search term in both the title and the content, ie a multi-field matching.
Can I somehow influence this, perhaps by increasing the boost value or reformulating the request? I also read something about a dismax query, but I do not know if that is useful for my purposes?

2
  • i see you are using AND Operator, thus it expects all the fields to have the value you are looking for. did you try changing the boolean operator to OR? Commented Jan 14, 2019 at 8:51
  • if you are looking for individual field boosting, try following this answer Commented Jan 14, 2019 at 8:53

1 Answer 1

0

You need to use a multi match query with the best_fields strategy.

In the multi match syntax you can use the boosting syntax "^X" and you will gain access to different strategies in order to tweak the outcome of your query.

The best_field strategy tell that between all the searched field, you will only keep the most relevant field for scoring (and not the sum of all the fields, that would favorize document where the query is found in title AND content).

Could you try :

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "europe",
            "analyzer": "standard",
            "operator": "AND",
            "fields": [
              "id", "title^2", "content"
            ],
            "type": "best_fields"
          }
        },
        {
          "term": {
            "client": {
              "value": "test",
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "size": 10,
  "from": 0,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

BTW the best_field strategy is the default one so you can omit the type parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

That would definitely work in the desired way, thanks. Now, however, two new questions arise for me: Elasticsearch informs me that the default operator is not allowed. But how do I formulate now a query with two keywords (AND search)? And secondly, with multi_match I have to specify which fields should be searched. Can I formulate this so that all fields in the index are taken, so as not to have to specify each one individually?
You cant select all fields with a wildcard in the field name elastic.co/guide/en/elasticsearch/guide/current/… and there was a typo in my query, its operator and not default_operator. Be carefull with the AND with best_field strategy. It will require all terms to be found in title or all terms in content in order to match

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.