Hi there
Sorry, this is undoubtedly a bit of noob question but I am still trying to get to grips with compound queries in Elasticsearch.
In my example, each document has a name
field and we can assume that each name
field is unique. The mapping is as follows (there's also an id field that I've excluded for brevity):
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"exact": {
"type": "keyword"
}
}
}
}
}
}
I am wondering how to go about the following as a single query:
Perform a keyword match query on the
name
field to find one result. If that query doesn't find any results, perform a match query on thename
field to return a list of similar results.
In other words, find one exact result OR find a list of similar results.
My first attempt was pretty naive, this obviously returns the exact match if present AND similar results:
{
"query": {
"bool": {
"should": [
{
"term": {
"name.exact": "Acme Incorporated"
}
},
{
"match": {
"name": "Acme Incorporated"
}
}
]
}
}
}
Any advice would be greatly appreciated Thanks!