Viplock
Viplock

Reputation: 3319

Search By Partial word of a sentence in elastic search

I am very new to Elastic Search , I want to search a result based on a partial word of a sentence , like the search string is

"val"

and it should search the result with string value

"value is grater than 100"

but if I am using a query

var searchDescriptor = new SearchDescriptor<ElasticsearchProject>()
searchDescriptor.Query(q =>
                    q.Wildcard(m => m.OnField(p => p.PNumber).Value(string.Format("*{0}*", searchString)))

                );

it will work only for one word string like

"ValueIsGraterThan100"

if I use something like this

var searchDescriptor = new SearchDescriptor<ElasticsearchProject>()
searchDescriptor.Query(q =>

                    q.QueryString(m => m.OnFields(p => p.PName).Query(searchString))

                );

This will work for entire word , like i have to provide search string as

"value"

to search

"value is grater than 100"

only providing val will not work.So how i can fulfill my requirement ?

Upvotes: 0

Views: 1221

Answers (1)

user3775217
user3775217

Reputation: 4803

Your field currently is not_analyzed, You can use edge n-gram analyzer made up of edge ngram filter to token your field before saving the fields on inverted index. You can use the following settings

PUT index_name1323
{
    "settings": {
        "analysis": {
            "analyzer": {
                "autocomplete_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "standard",
                        "lowercase",
                        "filter_edgengram"
                    ]
                }
            },
            "filter": {
                "filter_edgengram": {
                    "type": "edgeNGram",
                    "min_gram": 2,
                    "max_gram": 15
                }
            }
        }
    },
    "mappings": {
        "test_type": {
            "properties": {
                "props": {
                    "type": "string",
                    "analyzer": "autocomplete_analyzer"
                }
            }
        }
    }
}

Now you can simply use both query_string or term filter to match both your documents to val

POST index_name1323/_search
{
    "query": {
        "query_string": {
            "default_field": "props",
            "query": "val"
        }
    }
}

Hope this helps

Upvotes: 1

Related Questions