Reputation: 9384
I have a piece of data in elasticsearch like this:
{"_index": "my_index",
"_type": "my_type",
"_id": "test_id",
"_version": 1,
"_score": 1,
"_source": {
"d2": "*",
"d1": "test_data"
}}
So I can search this data by this clause:
{"query": {"filtered":{"filter":{"term":{"d1":"test"}}}}}
However, because the field "d2" has a specical character "*", I am confused that how to search this data by d2. These two methods below are both incorrect.
{"query": {"filtered":{"filter":{"term":{"d2":"*"}}}}}
or
{"query": {"filtered":{"filter":{"term":{"d2":"\*"}}}}}
Upvotes: 3
Views: 4493
Reputation: 8165
Your problem is that your index doesn't contain anything!
If you don't have setup a specific mapping, the dynamic mapping that has been used has setup a field with type string
, which is analyzed by default with standard
analyzer.
If you check the result of analyzing *
with the standard analyzer, you will see that you don't have any token as a result :
GET _analyze?analyzer=standard&text=*
ElasticSearch response :
{
"tokens": []
}
Change your mapping to have the d2
field indexed as not_analyzed
and the document will match your queries.
Example :
POST /special/
{
"mappings":{
"character":{
"properties":{
"d2":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}
POST /special/character/
{
"d2":"*"
}
POST /special/_search
{
"query": {
"term": {
"d2": "*"
}
}
}
And you finally have your result :
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "special",
"_type": "character",
"_id": "AU_gcWOBn6s1TzYZvmTv",
"_score": 0.30685282,
"_source": {
"d2": "*"
}
}
]
}
Analysis is a very important concept of ElasticSearch : take the time to read about it and you'll save yourself a large amount of time trying to understand what's going on.
To answer more generally to how to query on these special characters, you must control your analysis not to delete them or query on a dedicated field which is not analyzed.
Upvotes: 3