pirox22
pirox22

Reputation: 922

Query Elastic document field with and without characters

I have the following documents stored at my elasticsearch index (my_index):

{
    "name": "111666"
},
{
    "name": "111A666"
},
{
    "name": "111B666"
}

and I want to be able to query these documents using both the exact value of the name field as well as a character-trimmed version of the value.

Examples

GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "name": {
                "query": "111666"
            }
        }
    }
}

should return all of the (3) documents mentioned above.

On the other hand:

GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "name": {
                "query": "111a666"
            }
        }
    }
}

should return just one document (the one that matches exactly with the the provided value of the name field).

I didn't find a way to configure the settings of my_index in order to support such functionality (custom search/index analyzers etc..).

I should mention here that I am using ElasticSearch's Java API (QueryBuilders) in order to implement the above-mentioned queries, so I thought of doing it the Java-way.

Logic

1) Check if the provided query-string contains a letter
2) If yes (e.g 111A666), then search for 111A666 using a standard search analyzer
3) If not (e.g 111666), then use a custom search analyzer that trims the characters of the `name` field

Questions

1) Is it possible to implement this by somehow configuring how the data are stored/indexed at Elastic Search?

2) If not, is it possible to conditionally change the analyzer of a field at Runtime? (using Java)

Upvotes: 0

Views: 98

Answers (2)

Karsten R.
Karsten R.

Reputation: 1758

Your question is about different logic for the analyzer at index and query time.

The solution for your Q1 is to generate two tokens at index time (111a666 -> [111a666, 111666]) but only on token at query time (111a666 -> 111a666 and 111666 -> 111666).

I.m.h.o. your have to generate a new analyzer like https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern_replace-tokenfilter.html which supported "preserve_original" like https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-capture-tokenfilter.html does. Or you could use two fields (one with original and one without letters) and search over both.

Upvotes: 0

Emdadul Sawon
Emdadul Sawon

Reputation: 6077

You can easily use any build in analyzer or any custom analyzer to map your document in elasticsearch. More information on analyzer is here

The "term" query search for exact match. You can find more information about exact match here (Finding Exact Values)

But you can not change a index once it created. If you want to change any index, you have to create a new index and migrate all your data to new index.

Upvotes: 0

Related Questions