Spartan Troy
Spartan Troy

Reputation: 1039

elastic search multi-fields matching problem

I have a query for elastic search. I am writing a query like this.

$params = [
            'index' => config('elasticsearch.index'),
            'type' => $this->type,
            'body' => [
                'query' => [
                    'multi_match' => [
                        'query' => 'newyork john',
                        'type'=>'cross_fields',
                        'fields'=>['user','surname','location'],
                        'tie_breaker'=>'0.3',
                        'minimum_should_match'=>'10%',
                    ]
                ]
            ]
        ];

For example, this query returns the following result.

id=>1
score => 0.9808292
user => nja
surname => foo
'location'=> newyork

//1 field matching

id=>2
score => 0.87546873
user => alex
surname => stephen
'location'=> newyork

//1 field matching

id=>3
score => 0.18232156,
user => alex
surname => john
'location'=> newyork

//2 field matching

but I want to see it at the top if there is how many more fields match.This query does not do it. Matching data for 2 field is at the bottom.I actually want to see it at the top.

How can I do that?

Upvotes: 1

Views: 539

Answers (1)

Pierre Mallet
Pierre Mallet

Reputation: 7221

You have to use the most_fields type instead of cross_fields :

You can see the doc here : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#multi-match-types

most_fields

Finds documents which match any field and combines the _score from each field. See most_fields.

cross_fields

Treats fields with the same analyzer as though they were one big field. Looks for each word in any field. See cross_fields.

The most_fields type will add the score of the matching fields, and thus give a significant boost to documents matching the query in different fields.

Upvotes: 1

Related Questions