2
votes

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"
            }
        }
        }
    ]
    }
}
}
1
What have you tried so far?Tim
I just want to do my normal search but also want them to be listed by "likes" field. More likes = more relevancy.somethingyouwant

1 Answers

2
votes

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.