I just run a simple wildcard query on my elasticsearch server.
My query is;
GET calls-*/_search
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"default_field": "foo.number",
"query": "*16922*"
}
}
]
}
}
]
}
}
}
The error response looks like this;
Can only use wildcard queries on keyword, text, and wildcard fields - not on [foo.number] which is of type [long]
when I check the index info I saw number type is long
. If I switch to text
from long
everything worked as expected. This answer says exact same thing.
"number": {
"type": "long",
"ignore_malformed": false,
"coerce": true
},
But I can't change the number type because the code will ship to the prod server and I can't modify any prod elastic index.
So how can I run my wildcard queries without any errors? If the only way is the modifying the index (switch text
from the long
), can I replace wildcard queries with another method?
CodePudding user response:
You cannot perform wildcard search directly on the numeric data type fields
If you want to perform a wildcard search on long-type fields then 1 way is to update your index mapping, reindex the data, and then use the wildcard search.
While updating the index mapping, you can keep the type of foo.number
field as long
, and add one sub-field to it of type text
, using Update Mapping API as shown below :
Step 1: Update Mapping
PUT /_mapping
{
"properties": {
"foo": {
"properties": {
"number": {
"type": "long",
"fields": {
"key": {
"type": "text"
}
}
}
}
}
}
}
Step 2: Reindex the data
Step 3: Search Query
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"default_field": "foo.number.key",
"query": "*123*"
}
}
]
}
}
]
}
}
}
After hitting the above search and following Step 1 and Step 2, you will get your desired result.