我想从弹性搜索中搜索数据,其中数据从头n个字符开始匹配。
以下是我进入ES索引的数据集/数据
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 47,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "search_suggestions",
"_type": "_doc",
"_id": "VW5pdGVkIFN0YXRlcw==",
"_score": 1,
"_source": {
"id": "VW5pdGVkIFN0YXRlcw==",
"name": "India",
}
},
{
"_index": "search_suggestions",
"_type": "_doc",
"_id": "RW1iYXNzeSBvZiB0aGUgViwgREMsIFVTQQ==",
"_score": 1,
"_source": {
"id": "RW1iYXNzeSBvZiB0aGUgViwgREMsIFVTQQ",
"name": "Maharashtra, India",
}
},
{
"_index": "search_suggestions",
"_type": "_doc",
"_id": "TWFoaSBCYXpB",
"_score": 1,
"_source": {
"id": "TWFoaSBCYXpB",
"name": "Pune, Maharashtra, India",
}
}
]
}
}
我已经尝试通过使用查询/术语/前缀/通配符方法进行搜索。我想查找以“ Ind *”开头的名称,但作为响应,它返回的字符串在字符串中的任何位置都匹配。我只想要以“ Ind *”开头的结果,而其他则不是。
请提出如何查询ES以获得高于预期结果的方法。
作为响应,查询将返回在字符串中任何位置都匹配的字符串,因为该字符串被分成单独的令牌(基于标准分析器)。因此,它将匹配包含印度的所有文档,这些文档在任何位置都可以,而不仅仅是在name
字段开始时
分析API
{
"analyzer" : "standard",
"text" : "Maharashtra, India"
}
生成以下令牌:
{
"tokens": [
{
"token": "maharashtra",
"start_offset": 0,
"end_offset": 11,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "india",
"start_offset": 13,
"end_offset": 18,
"type": "<ALPHANUM>",
"position": 1
}
]
}
要实现用例,您需要将name
字段设置为keyword
type
索引映射:
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
搜索查询:
{
"query": {
"prefix": {
"name": {
"value": "Ind"
}
}
}
}
搜索结果:
"hits": [
{
"_index": "65119778",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"id": "VW5pdGVkIFN0YXRlcw==",
"name": "India"
}
}
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句