2

I want to use the example on official documents combined with normal match and boolean queries. How to do that?

GET /_search
{
    "query": {
        "function_score": {
            "field_value_factor": {
                "field": "likes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
            }
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor

Match query:

    "query": {
        "match" : {
            "name" : "star wars"
        }
    }

Boolean query:

{
"query": {
    "bool": {
    "must": [
        {
        "match": {
            "name": "Star Wars"
        }
        }
    ],
    "should": [
        {
        "term": {
            "name.keyword": {
            "value": "Star Wars"
            }
        }
        }
    ]
    }
}
}
2
  • What have you tried so far? Commented Oct 12, 2018 at 13:15
  • I just want to do my normal search but also want them to be listed by "likes" field. More likes = more relevancy. Commented Oct 12, 2018 at 15:57

1 Answer 1

3

Yes, this should be doable. If you read further down in the documentation that you linked to, there is an example:

GET /_search
{
    "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "price": {
                  "origin": "0",
                  "scale": "20"
                }
              }
            },
            {
              "gauss": {
                "location": {
                  "origin": "11, 12",
                  "scale": "2km"
                }
              }
            }
          ],
          "query": {
            "match": {
              "properties": "balcony"
            }
          },
          "score_mode": "multiply"
        }
    }
}

Modifying that slightly for your use case should look something like this:

GET /_search
{
    "query": {
        "function_score": {
          "functions": [
            {
              "field_value_factor": {
                "field": "likes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
              }
            }
          ],
          "query": {
            "match": {
              "name": "Star Wars"
            }
          },
          "score_mode": "multiply"
        }
    }
}

Disclaimer: I haven't tested this, just going off of the documentation.

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

5 Comments

not working. Getting this error: Fielddata is disabled on text fields by default. Set fielddata=true on [likes] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead
What is your "likes" field mapping?
"likes": { "type": "integer" }
Sorry forgot what i write (now deleted that comment. it is nothing like that) It just throws that error on integer type.
Likes is absolute for me now. The top search results must be though highest likes to the lowest ones. They are the utmost relevancy for me. How can i do that?

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.