2

I want to get results in a specific order first get the exact phrase (match_phrase) match and then match any word in phrase (match), for example, if I'm searching for "where can i find my account"I will get the documents containing the full phrase "where can I find my account" first and then documents that contain one or more of the words "where", "can", "find", "my", "account"

my query :

GET my_index/_search

{

"query": {

"bool": {
  "should": [
    {
      "match": {"body": "right usage of Localization"      }
    } ,
    {
      "match": {"title": "right usage of Localization"      }
    } 
    ],
    "should": [
    {
      "match_phrase": {"body": "translated"      }
    },
    {
      "match_phrase": {"title": "translated"      }
    } 
    ]
  }

} }

1 Answer 1

1

You can use boost query to boost the documents matching your match_phrase query. You can change the boost parameter accordingly.

Following query should work for you

POST phrase_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "title": "this is where it should work first"
                }
              },
              {
                "match": {
                  "body": "this is where it should work first"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "title": {
                    "query": "this is where it should work first",
                    "boost": 20
                  }
                }
              },
              {
                "match_phrase": {
                  "body": {
                    "query": "this is where it should work first",
                    "boost": 20
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi! What if I just wanted to use only the match_phrase for both fields? I tried your answer without any of the match but it did not work for me.
what ES version are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.