I have just started using Elasticsearch, version 7.5.1.
I want to query results which start with a particular word fragment. For example tho* should return data containing:
thought, Thomson, those, etc.
I tried with -
[{'regexp':{'f1':'tho.*'}},{'regexp':{'f2':'tho.*'}}]
[{'wildcard':{'f1':'tho*'}},{'wildcard':{'f2':'tho*'}}]
[{'prefix':{'f1':'tho'}},{'prefix':{'f2':'tho'}}]
'multi_match': {'query': 'tho', 'fields':[f1,f2,f3], 'type':phrase}
# also tried with type phrase_prefix
All those are returning correct results, but they all also return the word method.
Similarly cat* is returning the word communication.
What I am doing wrong? Is this something related to analyzer?
'f1': {
'full_name': 'f1',
'mapping': {
'f1': {
'type': 'text',
'analyzer': 'some_analyzer',
'index_phrases': true
}
}
},
Since you have not provided any index mapping of yours and as mentioned you are getting method
also in the search result. I think that there is some issue with the analyzer that you have set.
One possibility is that you have set ngram tokenizer, that tokenizes the words, and produce token of tho
(since all the words have tho
present in them)
Adding a working example with index data, mapping, search query, and search result
Index Mapping:
{
"mappings": {
"properties": {
"f1": {
"type": "text"
}
}
}
}
Index Data:
{
"f1": "method"
}
{
"f1": "thought"
}
{
"f1": "Thomson"
}
{
"f1": "those"
}
Search Query using Wildcard Query:
{
"query": {
"wildcard": {
"f1": {
"value": "tho*"
}
}
}
}
Search Query using Prefix Query:
{
"query": {
"prefix": {
"f1": {
"value": "tho"
}
}
}
}
Search Query using Regexp query:
{
"query": {
"regexp": {
"f1": {
"value": "tho.*"
}
}
}
}
Search QUery using match phrase prefix query:
{
"query": {
"match_phrase_prefix": {
"f1": {
"query": "tho"
}
}
}
}
Search Result for all the above 4 queries are
"hits": [
{
"_index": "67673694",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"f1": "thought"
}
},
{
"_index": "67673694",
"_type": "_doc",
"_id": "2",
"_score": 1.2039728,
"_source": {
"f1": "Thomson"
}
},
{
"_index": "67673694",
"_type": "_doc",
"_id": "3",
"_score": 1.2039728,
"_source": {
"f1": "those"
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With