我在Elasticsearch中有一条数据是这样的:
{"_index": "my_index",
"_type": "my_type",
"_id": "test_id",
"_version": 1,
"_score": 1,
"_source": {
"d2": "*",
"d1": "test_data"
}}
因此,我可以通过以下子句搜索此数据:
{"query": {"filtered":{"filter":{"term":{"d1":"test"}}}}}
但是,因为字段“ d2”具有特殊字符“ *”,所以我对如何通过d2搜索此数据感到困惑。下面的这两种方法都是不正确的。
{"query": {"filtered":{"filter":{"term":{"d2":"*"}}}}}
或者
{"query": {"filtered":{"filter":{"term":{"d2":"\*"}}}}}
您的问题是您的索引不包含任何内容!
如果您没有设置特定的映射做的,已经使用的动态映射具有设置与类型的字段string
,它是分析默认与standard
分析。
如果您*
使用标准分析仪检查分析结果,则会发现结果没有任何标记:
GET _analyze?analyzer=standard&text=*
ElasticSearch响应:
{
"tokens": []
}
更改您的映射以使d2
字段索引为not_analyzed
,并且文档将与您的查询匹配。
例子 :
POST /special/
{
"mappings":{
"character":{
"properties":{
"d2":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}
POST /special/character/
{
"d2":"*"
}
POST /special/_search
{
"query": {
"term": {
"d2": "*"
}
}
}
最后,您得到了结果:
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "special",
"_type": "character",
"_id": "AU_gcWOBn6s1TzYZvmTv",
"_score": 0.30685282,
"_source": {
"d2": "*"
}
}
]
}
分析是ElasticSearch的一个非常重要的概念:花时间阅读它,您将节省大量时间来了解发生的事情。
若要更一般地回答如何查询这些特殊字符,您必须控制您的分析,不要删除它们或在未分析的专用字段上进行查询。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句