How to make exact values โ€‹โ€‹and respond to requests in one field in elasticsearch?

So, I have a field that stores the value in the format: number/year , something like 23/2014, 24/2014, 12/2015, etc.

therefore, if this field is displayed as not_analyzed , I can search for exact values โ€‹โ€‹using a term filter, if I look for a value in this exact structure (something like 1/2014, 15/2014, ...) it works like sql equals(=) .

 { "query": { "filtered": { "filter": { "term": { "processNumber": "11/2014" } } } } } 

So, a search with something else, like 11 /, or / 2014, will not return hits. This is normal.

But if I define the field as not_analyzed , I cannot do a sql LIKE search with match_phrase query.

 { "query": { "match_phrase": { "processNumber": "11/201" } } } 

In this case, a search of 11.11 /, / 2014 or 2014 should return hits, but they do not. The fact is that this request works if the field is not displayed as not_analyzed . Therefore, it seems to me that I should use one or the other, the problem is that the field should support both options for different requests, am I not seeing something here?

+8
filter elasticsearch
source share
1 answer

You can analyze the same processNumber field differently using the fields property in the display:

For example, if you want the analyzed and non-analyzed version of ProcessNumber to be displayed as:

  { "type_name": { "properties": { "processNumber": { "type": "string", "index": "not_analyzed", "fields": { "analyzed": { "type": "string", "index": "analyzed" } } } } } } 

If an unanalyzed field is referred to in the request as processNumber .

To refer to the analyzed field type, use processNumber.analyzed

Queries for terms 11/201, 11, etc. will be as follows:

Filter example:

  { "query" : { "filtered" : { "filter" : { "term" : { "processNumber" : "11/2014" } } } } } 

The thermal filter does not analyze the search string, so the input will be matched, as with the inverted field index in this case: 11/2014 against the field.

Example Match_Phrase_prefix:

 { "query": { "match_phrase_prefix": { "processNumber": "11/201" } } } 

match_phrase_prefix tries to check if the last term in a phrase is a prefix of terms in the index. It parses the search string if an parser is specified. It is for this reason that you need to use an unanalyzable version of the field here. If we use processNumber. The analyzed search queries, such as 11-201, 11 | 201 will also match

match example:

  { "query": { "match": { "processNumber.analyzed": "11" } } } 

This is a direct match expression, since the default parser (usually a standard parser) will tokenize 11/2014 to condition 11, 2014.

You can use api analysis to see how specific text is analyzed by default.

 curl -XPOST "http://<machine>/_analyze?text=11/2014" 
+14
source share

All Articles