Home > Software design >  Elasticsearch Match Query all words in analyzed text field
Elasticsearch Match Query all words in analyzed text field

Time:10-21

So I have an ES document upon which structure I have wno control, so I cannot change the mappings. I have a location field (mapping type text) that is "analyzed" by ES. My documents look like this:

[
 {
  title: "Something that happened in the UK",
  location: "United States, London"
 },
 {
  title: "Something that happened in the US",
  location: "United Kingdom, London"
 }
]

I am trying to write a query that would only filter the location field and return results that are either united states or united kingdom but not both.

{
 "query":
 {
   "match": {
    "location": {  "query": "united statess" }
 }
}

This does not work because the word united is present in both location names. The field is unfortunately analyzed and it will return both results. I have tried adding the "operator" : "and" to the "match" query but that does not return any results. What am I missing? Is there a way to achieve this with the "match" query?

CodePudding user response:

After trying various things I came up with this solution which seems to work. Though for some reason I feel that there is a better way to achieve this:

Answering my own question, would this make sense?

  • Related