我正在尝试使用Elasticsearch查找具有2个匹配条件的文档:
这里是正在使用的映射:
{
"mappings": {
"stores": {
"properties": {
"locality": {
"type": "text"
},
"city": {
"type": "text"
},
"type": {
"type": "integer"
}
}
}
}
}
这是我的过滤器:
{
"query": {
"constant_score": {
"filter": {
"bool" : {
"must" : [
{
"term" : { "locality": "Shivajinagar" }
}, {
"term" : { "city": "Bangalore" }
}
]
}
}
}
}
}
无论我尝试什么价值,我总能得到:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
即使数据存在(所有文档搜索):
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10742,
"max_score": 1.0,
"hits": [
{
"_index": "test_es",
"_type": "stores",
"_id": "942",
"_score": 1.0,
"_source": {
"type": 2,
"locality": "Palam Vihar",
"city": "Gurgaon"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "944",
"_score": 1.0,
"_source": {
"type": 2,
"locality": "Chirag Dilli",
"city": "Delhi"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "948",
"_score": 1.0,
"_source": {
"type": 1,
"locality": "Vashi",
"city": "Navi Mumbai"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "980",
"_score": 1.0,
"_source": {
"type": 3,
"locality": "Sector 48",
"city": "Faridabad"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "982",
"_score": 1.0,
"_source": {
"type": 2,
"locality": "Kammanahalli",
"city": "Bangalore"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "984",
"_score": 1.0,
"_source": {
"type": 3,
"locality": "Tilak Nagar",
"city": "Delhi"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "742",
"_score": 1.0,
"_source": {
"type": 3,
"locality": "Shivajinagar",
"city": "Bangalore"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "752",
"_score": 1.0,
"_source": {
"type": 1,
"locality": "DLF Phase 3",
"city": "Gurgaon"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "754",
"_score": 1.0,
"_source": {
"type": 3,
"locality": "Electronic City",
"city": "Bangalore"
}
},
{
"_index": "test_es",
"_type": "stores",
"_id": "778",
"_score": 1.0,
"_source": {
"type": 2,
"locality": "Bandra East",
"city": "Mumbai"
}
}
]
}
}
我尝试使用查询而不是过滤器,即使我并不真正关心乐谱,但是虚无...!
我可能在哪里出错?!
答案 0 :(得分:1)
简答:使用match
代替term
。
长答案:
这里要注意的重要一点是,您的搜索关键字,例如:{ "locality": "Shivajinagar" }
和{ "city": "Bangalore" }
需要以与存储时相同的形式进行比较。
在这个问题中,映射指定了" locality"和"城市"字段为type: text
。根据文档,默认情况下,标准分析器会分析type: text
个字段。
默认
standard analyzer
会丢弃大多数标点符号,会分解文本 单个单词,小写单词。例如, 标准分析仪会把字符串“Quick Brown Fox!”变成 术语[快速,棕色,狐狸]。这种分析过程使其成为可能 在一大块全文中搜索单个单词。
term
查询会在字段的倒置中查找确切的字词 index - 它对该字段的分析器一无所知。这个 使其在关键字字段或数字中查找值非常有用 或日期字段。查询全文字段时,请使用匹配查询 相反,它了解该领域的分析方式。
所以,当你搜索"班加罗尔"它在一个术语查询中寻找"班加罗尔"在城市领域,而索引映射确保它被存储为" bangalore"。这就是你没有比赛的原因。
您可以在此处找到有关确切问题的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
侧提示:使用_analyze端点确切地检查特定分析器在传递输入文本时发出的内容。 _analyze端点的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html