Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multi_match to Elastic Search Function Score Query

Currently, I am only querying the "content" of a web page stored in elastic search for a keyword match. I would like to add the title to that equation. I am using the top code (under "MARRY THIS") and would like to somehow insert the bottom code ("WITH THIS") but can't seem to find an explanation on how to do so.

MARRY THIS

{
    "query": {
        "function_score": {
            "query": {
                "match": {
                    "content": "keyword"
                }
            },
            "functions": [{
                "field_value_factor": {
                    "field": "obls",
                    "factor": 0.5,
                    "modifier": "ln2p"
                }
            }]
        }
    },
    "from": "0",
    "size": "100"
}

WITH THIS

{
  "multi_match" : {
    "query":    "keyword", 
    "fields": [ "title^3", "content" ] 
  }
}

Thanks in advance!

like image 394
Russ Avatar asked Mar 18 '15 14:03

Russ


People also ask

What is multi match query in Elasticsearch?

More Practice: Elasticsearch Multi Match Query – More Practice 1. Multi Match Query multi_match query allows us to search for a value across multiple fields. GET javasampleapproach/tutorial/_search { "query": { "multi_match" : { "query": "restapi", "fields": [ "title", "tags"] } } }

How does the multi_match query work internally?

The way the multi_match query is executed internally depends on the type parameter, which can be set to: ( default) Finds documents which match any field, but uses the _score from the best field. See best_fields . Finds documents which match any field and combines the _score from each field. See most_fields .

What does Elasticsearch use for scoring?

The Practical Scoring Function Elasticsearch runs Lucene under the hood so by default it uses Lucene's Practical Scoring Function. This is a similarity model based on Term Frequency (tf) and Inverse Document Frequency (idf) that also uses the Vector Space Model (vsm) for multi-term queries.

How do you score a match with multiple fields?

Take the single best score plus tie_breaker multiplied by each of the scores from other matching fields/ groups The fuzziness parameter cannot be used with the cross_fields type. The bool_prefix type’s scoring behaves like most_fields, but using a match_bool_prefix query instead of a match query.


1 Answers

I figured it out after a lot of trial and error, it looks like this:

{
    "query": {
        "function_score": {
            "query": {
                "multi_match": {
                    "query": "baseball",
                    "fields": ["content", "title"]
                }
            },
            "functions": [{
                "field_value_factor": {
                    "field": "obls",
                    "factor": 0.5,
                    "modifier": "ln2p"
                }
            }]
        }
    },
    "from": "10",
    "size": "100"
}
like image 57
Russ Avatar answered Sep 29 '22 11:09

Russ