1

Suppose the doc text is This is a sample text to show how the search results works and my query string is mple tex. I want this query string to match the text since it partially match against sample text.

How do I do that in elastic search? is much search possible in ES?

What I have currently used is the match_phrase query

"query": {"match_phrase": {"description": "mple tex"}},
Rameez Rami
  • 5,322
  • 2
  • 29
  • 36
  • Its been stackoverflow long, let me know if you have anything else to ask, otherwise please mark the answer. – Amit Apr 02 '22 at 08:53

2 Answers2

2

What you are looking for is called infix search and can be easily accomplished using the ngram token filter, Please see below complete working example, which is better than doing the wildcard searches and doesn't use the query string which is not recommended for search boxes, as mentioned in official docs.

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

Index def

{
    "settings": {
        "analysis": {
            "filter": {
                "autocomplete_filter": {
                    "type": "ngram",
                    "min_gram": 1,
                    "max_gram": 10
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter"
                    ]
                }
            }
        },
        "index.max_ngram_diff": 10
    },
    "mappings": {
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "autocomplete",
                "search_analyzer": "standard"
            }
        }
    }
}

Index your sample doc

{
    "title":"This is a sample text to show how the search results works"
}

** Search for your text**

{
   "query": {
      "match": {
         "title": {
            "query": "mple tex"
         }
      }
   }
}

Search result with score

 "max_score": 0.9168506,
        "hits": [
            {
                "_index": "my-index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.9168506,
                "_source": {
                    "title": "This is a sample text to show how the search results works"
                }
            }
        ]

Note:Please refer my detailed answer on how to choose best autocomplete approach based on functional and non-functional requirements and with their trade-off

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Amit
  • 30,756
  • 6
  • 57
  • 88
0

Try with description.keyword instead of just description. The description.keyword field will have the text in raw format or in the string format. This will help you to get the exact match or the wildcard match.

If you have applied the custom analyzer to your field description. Then apply the keyword type as well in the mappings.

Similar to suggestion above, but this is simple.

{
  "query": {
    "wildcard": {
      "description": {
        "value": "*ronavir*"
      }
    }
  }
}

The above works for single word. Like it fetches the result where the words matches coronavirus. But For multiple words you need to do the ngram analysis of the text.

Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47
  • i tried `"description.keyword" ` but no luck. it is not even returning results for the exact word. I haven't used analyzers yet. is it needed for this to work? – Rameez Rami Jul 31 '20 at 13:17
  • @RameezRami: You should then try what Opster Elasticsearch Ninja has suggested I think that should work for you. – Abhijit Bashetti Jul 31 '20 at 13:37