-1

Am searching elastic search with the below match query, which is not giving me the exact match instead its giving some more irrevalant match also.

am using elastic search 6.3

Please find my query below

GET /_search
{
   "must":{
      "query_string":{
         "query":"review:*test product*"
      }
   }
}

Search Result:

"hits": [ { "_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "title": "testing" } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "product good" } }, { "_index": "67107104", "_type": "_doc", "_id": "3", "_score": 0.6931471, "_source": { "title": "sample" } },{ "_index": "67107104", "_type": "_doc", "_id": "4", "_score": 0.7897571, "_source": { "title": "superr" } } ]

Expected Search Result:

"hits": [ { "_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "title": "testing" } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "product good" } } ]

revathi
  • 1
  • 1
  • 2

2 Answers2

1

If you have not explicitly defined any mapping then you need to add .keyword to the title field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after title field).

Adding a working example with index data, search query and search result

Index Data:

{
  "title": "This is test product"
}
{
  "title": "test product"
}

Search Query:

{
  "query": {
    "query_string": {
      "fields": [
        "title.keyword"
      ],
      "query": "test product"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67107104",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "title": "test product"
        }
      }
    ]

Search Query using Match query:

{
  "query": {
    "match": {
      "title.keyword": "test product"
    }
  }
}

Search Query using term query

    {
      "query": {
        "term": {
          "title.keyword": "test product"
        }
      }
    }
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • @revathi did you get a chance to go through the answer, looking forward to get feedback from you :-) – ESCoder Apr 16 '21 at 02:23
  • I want to perform both exact word match and partial word/sub string match. For example if I search for "test product" then I should be able to find "test" and "product" related text in the result. is giving irrevalant match For Example it's give "sample" related title in the result. – revathi Apr 16 '21 at 05:05
  • @revathi did you try with the search query shown above in my answer? Can you please share your sample index data, so that I can provide a working example of that ? – ESCoder Apr 16 '21 at 05:09
  • Result:"hits": ["_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "title": "testing" }}, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "product good" }},{ "_index": "67107104", "_type": "_doc", "_id": "3", "_score": 0.6931471, "_source":{ "title": "sample" }}] Expected Result:"hits": [ { "_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source":{ "title": "testing" } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source":{ "title": "product good" }}] – revathi Apr 21 '21 at 04:53
  • @revathi there is no document having `{ "title": "sample" }` in your sample index data, so how can you get this data in your search result ? – ESCoder Apr 21 '21 at 05:17
  • @revathi please hit the query match_all API --> https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html, and then share the result of this query. This will help me know your index data – ESCoder Apr 21 '21 at 05:19
0

You can use boolean queries for the exact match with the filter by using the term. As the term is used for exact match and you need to add keyword for the text fields

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "review_title.keyword": "test product"
          }
        }
      ]
    }
  }
}