I try to make an autocomplete function with angularjs and elasticsearch on a given field, for example countryname
. it can contain simple names like "France", "Spain" or "composed names" like "Sierra Leone".
In the mapping this field is not_analyzed
to prevent elastic to tokenize "composed names"
"COUNTRYNAME" : {"type" : "string", "store" : "yes","index": "not_analyzed" }
I need to query elasticsearch:
- to filter the document with something like "countryname:value" where value can contain wildcard
- and make an aggregation on the countryname returned by the filter, ( i do aggregation to get only distinct data, the count is useless for me her, maybe there is a better solution)
I can't use wildcard with the "not_analyzed" field :
this is my query but wildcard in "value" variable doesn't work and it's case sensitive :
The wildcard alone her work :
curl -XGET 'local_host:9200/botanic/specimens/_search?size=0' -d '{
"fields": [
"COUNTRYNAME"
],
"query": {
"query_string": {
"query": "COUNTRYNAME:*"
}
},
"aggs": {
"general": {
"terms": {
"field": "COUNTRYNAME",
"size": 0
}
}
}
}'
but this doesn't work (franc*) :
curl -XGET 'local_host:9200/botanic/specimens/_search?size=0' -d '{
"fields": [
"COUNTRYNAME"
],
"query": {
"query_string": {
"query": "COUNTRYNAME:Franc*"
}
},
"aggs": {
"general": {
"terms": {
"field": "COUNTRYNAME",
"size": 0
}
}
}
}'
I tried also with bool must query
but don't work with this not_analyzed field and wildcard :
curl -XGET 'local_host:9200/botanic/specimens/_search?size=0' -d '{
"fields": [
"COUNTRYNAME"
],
"query": {
"bool": {
"must": [
{
"match": {
"COUNTRYNAME": "Franc*"
}
}
]
}
},
"aggs": {
"general": {
"terms": {
"field": "COUNTRYNAME",
"size": 0
}
}
}
}'
What I'm missing or doing wrong? should I left the field analyzed
in the mapping and use another analyser who don't split composed name into token??