Elasticsearch exact result not return on a first

112 views Asked by At

I have added some products like 'lcd apple iphone 11' 'lcd apple iphone x' 'lcd apple iphone xs' 'lcd apple iphone xr' 'lcd samsung s8' 'lcd samsung s8+' 'lcd apple iphone xs max' 'lcd apple iphone xr battery' we had added iphone xr product in last so

I have created elasticsearch index products_idx1 and type product.

When I search product like apple iphone xr so it returns iphone xr but not come at top result.

What I want Exact result should be first then partial result should be after exact result .I want a sort the result based on accuracy result.

Here is my code in php elasticsearch

<?php

    use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

   $client = ClientBuilder::create()->build();
 $values =['name','name.prefix','name.suffix','sku'];
$params =
[
'client'=>['verify'=>1,'connect_timeout'=>5],
'from'=> 0,
'size'=>25,
 'body'  =>[
'query' => [
 'bool'=>
            [
            'should'=> [[
                'multi_match'=> ['query'=>'apple iphone xr','type'=>'cross_fields','fields'=>$values,'operator'=>'AND']
                ],
                ['match'=>['all'=>['query'=>'apple iphone xr','operator'=>'AND','fuzziness'=>'AUTO'] ]]
                ]
            ]

],
'sort'=>['_score'=>['order'=>'desc']],
],

'index'=>'products_idx1'
];

 $response = $client->search($params);
echo "<pre>";print_r($response);
2

There are 2 answers

0
Amit On BEST ANSWER

while bhavya's answer is working but its more complicated as its using the match_phrase query which is more complicated and might be more costly based on the dataset you might have, I have created a simpler version of this query which works.

Index sample docs

{
    "title" : "lcd apple iphone xr"
}
{
    "title" : "lcd apple iphone 11"
}
{
    "title" : "lcd apple iphone x"
}
{
    "title" : "lcd apple iphone xs"
}
{
    "title" : "iphone xr"
}

Search query which uses additional simpler match clause

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "title" : "apple iphone xr"
                    }
                },
                {
                    "match": {    --> simple additional match clause solves issue.
                        "title": "iphone xr"
                    }
                }
            ]
        }
    }
}

And search result showing iphone xr at top, with more score

 "hits": [
            {
                "_index": "64129903",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.8347404, // note score is higher than other results.
                "_source": {
                    "title": "iphone xr"
                }
            },
            {
                "_index": "64129903",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.8268716,
                "_source": {
                    "title": "lcd apple iphone xr"
                }
            },
            {
                "_index": "64129903",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.54542315,
                "_source": {
                    "title": "lcd apple iphone 11"
                }
            },
            {
                "_index": "64129903",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.54542315,
                "_source": {
                    "title": "lcd apple iphone x"
                }
            },
            {
                "_index": "64129903",
                "_type": "_doc",
                "_id": "5",
                "_score": 0.54542315,
                "_source": {
                    "title": "lcd apple iphone xs"
                }
            }
        ]
0
ESCoder On

You can use bool query with match phrase query that analyzes the text and creates a phrase query out of the analyzed text.

Adding a working example with search query and search result

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "apple iphone xr"
          }
        },
        {
          "match_phrase":{
            "title":"iphone xr"
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "5",
            "_score": 1.8850331,
            "_source": {
                "title": "iphone xr"
            }
        },
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "4",
            "_score": 1.7120029,
            "_source": {
                "title": "lcd apple iphone xr"
            }
        },
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.30396554,
            "_source": {
                "title": "lcd apple iphone 11"
            }
        },
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.30396554,
            "_source": {
                "title": "lcd apple iphone x"
            }
        },
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.30396554,
            "_source": {
                "title": "lcd apple iphone xs"
            }
        }
    ]

You can also use search Query using Multi match

{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "apple iphone xr",
                        "fields": [
                            "title"
                        ]
                    }
                },
                {
                    "match_phrase": {
                        "title": "iphone xr"
                    }
                }
            ]
        }
    }
}