# Show search result based on keywords

**URL:** <https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617>\
**Category:** Elasticsearch\
**Created:** [May 25, 2022, 1:47pm UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617 "2022-05-25T13:47:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvermanoj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silvermanoj/32/102280_2.png) [@silvermanoj](https://discuss.elastic.co/u/silvermanoj)\
**Post date:** [May 25, 2022, 1:47pm UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/1 "2022-05-25T13:47:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 25, 2022, 1:47pm UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/2 "2022-05-25T13:47:23Z")

</div>

Elasticsearch version 7.2 is [EOL](https://www.elastic.co/support/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: )

---

<div class="post-metadata">

**Author:** ![silvermanoj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silvermanoj/32/102280_2.png) [@silvermanoj](https://discuss.elastic.co/u/silvermanoj)\
**Post date:** [May 26, 2022, 5:13am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/3 "2022-05-26T05:13:52Z")

</div>

sorry the version is 8

---

<div class="post-metadata">

**Author:** ![w0lfxpunk](https://avatars.discourse-cdn.com/v4/letter/w/73ab20/32.png) [@w0lfxpunk](https://discuss.elastic.co/u/w0lfxpunk)\
**Post date:** [May 27, 2022, 4:18am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/4 "2022-05-27T04:18:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 27, 2022, 5:09am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/5 "2022-05-27T05:09:26Z")

</div>

Hi,

Try `should` clause of [boolean query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) and set any [`boost` parameter](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html#term-field-params) for each term or match query.

---

<div class="post-metadata">

**Author:** ![silvermanoj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silvermanoj/32/102280_2.png) [@silvermanoj](https://discuss.elastic.co/u/silvermanoj)\
**Post date:** [June 7, 2022, 2:15pm UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/6 "2022-06-07T14:15:36Z")

</div>

'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

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 8, 2022, 7:56am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/7 "2022-06-08T07:56:28Z")

</div>

Use [constant score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) to wrap each match\_phrase result.

How about this?

```auto
{
  "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"
    }]
}

```

---

<div class="post-metadata">

**Author:** ![silvermanoj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silvermanoj/32/102280_2.png) [@silvermanoj](https://discuss.elastic.co/u/silvermanoj)\
**Post date:** [June 10, 2022, 8:48am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/8 "2022-06-10T08:48:36Z")

</div>

$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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 8, 2022, 8:48am UTC](https://discuss.elastic.co/t/show-search-result-based-on-keywords/305617/9 "2022-07-08T08:48:55Z")

</div>

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