Show search result based on keywords

I am using Elasticsearch version 8. When I try to query a combination of two words that are stored in two different attributes, I get the search result but the record match with post_title are not coming first
For example I have two names saved in ES
Document 1: "post_tile" : "virat kohli king of centuries"
"post_subheading" : "virat kohli king of centuries"
"post_content" : "virat kohli king of centuries ....."
Document 1: "post_tile" : "xyz"
"post_subheading" : "xyz"
"post_content" : "xyz.."
If my search word is "Virat kohli", then i want my search result the search word matching with post_title keyword comes first after that post_subheading and after that post_content and order by latest post come first
Can I get assistance on how to resolve the issue?

Elasticsearch version 7.2 is EOL and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

sorry the version is 8

I am facing the same issue, anyone can please give some pointers ?

Hi,

Try should clause of boolean query and set any boost parameter for each term or match query.

'query' => [
'bool' => [
'should' => [
[ 'match_phrase' => [ 'post_title' => ''.$searchtext.'' ] ],
[ 'match_phrase' => [ 'post_excerpt' => ''.$searchtext.'' ] ],
[ 'match_phrase' => [ 'post_content' => ''.$searchtext.'' ] ],
],
'minimum_should_match' => 1,
]
],
i am using this but its not working as expected

Use constant score query to wrap each match_phrase result.

How about this?

{
  "query":{
    "bool": {
      "should":[
        {
          "constant_score":{
            "filter": {
              "match_phrase":{
                "post_title":"searchtext"
              }
            },
            "boost": 3
          }
        },
        {
          "constant_score":{
            "filter": {
              "match_phrase":{
                "post_subheading":"searchtext"
              }
            },
            "boost": 2
          }
        },
        {
          "constant_score":{
            "filter": {
              "match_phrase":{
                "post_content":"searchtext"
              }
            },
            "boost": 1
          }
        }
        ],
        'minimum_should_match' :1
    }
  },
  "sort": [
    "_score",
    {
      "@timestamp": "desc"
    }]
}

$params = [
'index' => 'elasticpostdata',
'body' => [
'from' => $pageLimit,
'size' => $setLimit,
'query' => [
"multi_match" => [
"query" => ''.$searchtext.'',
"fields" => ['post_title^3', 'post_excerpt^2', 'post_content'],
"type" => "phrase",
]
],
'sort' => [
["post_title.keyword" => ["order" => "desc"]],
["post_excerpt.keyword" => ["order" => "desc"]],
["post_content.keyword" => ["order" => "desc"]],
["post_indate" => ["order" => "desc"]],
["_score" => ["order" => "desc"]],
],
'collapse' => [
"field" => "postid"
]
],
];

above query working perfectly but sorting of result not working as expected
i want search text matching with post_title field comes first in desc order with date
and after that search text matching with post_excerpt comes in desc order with date
and in last search text matchinf with post_content comes in desc order with date

query finds result correct but sorting not working as i want.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.