Elasticsearch exact match or query
elasticsearch
Solution
You could use a multi-field mapping. This allows a field to be sent once, but analyzed in two different ways.
"properties": {
"field" {
"type": "multi_field",
"fields" : {
"field" : {"type" : "string", "index" : "analyzed"},
"raw" : {"type" : "string", "index" : "not_analyzed"}
}
}
}
Send the document to elasticsearch as normal (it will get indexed in two places, `field` (or `field.field`) and `field.raw`
Now your query will look like:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"or": [
{
"term": {
"field.raw": "a, b"
}
},
{
"and": [
{
"term": {
"field": "c"
}
},
{
"term": {
"field": "d"
}
}
]
}
]
}
}
}
}
It's not the most elegant solution. I would much prefer changing the way you store the data. It seems like "a, b" represents something different, maybe have a boolean field "a_b_only" on the doc to filter by.
Good luck, and please feel free to ask for more help!
Problem
I have documents like this in my index: ``` { "field" : "a, b, c, d, e" } ``` The field value is a string made by a array to string function. So not every document has the same string but every document has at least `"a, b"` as value. Now I would like to have a query which is matching 2 kinds of documents: Documents which have only(exactly) `"a, b"` as field value or documents which contain at least two searchterms in the field. Basically my problem is that I can't fullfill the first condition if the field gets analyzed and I can"t fulfill the second condition if the fields is not getting analyzed. Is there a solution without cloning the field as not_alanyzed? If I clone the field to a not analyzed field(in the code example field1) I can use this query. I feel like this query is too complicated for the achievement...: ``` { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "or": [ { "term": { "field1": "a, b" } }, { "and": [ { "term": { "field": "c" } }, { "term": { "field1": "d" } } ] } ] } } } } ```