elasticsearch 弹性搜索:如何搜索以某些字符开头的字符串
我想从弹性搜索中搜索数据,在弹性搜索中,数据从开始的前n个字符开始匹配 下面是我在ES索引中的数据集/数据
elasticsearch 弹性搜索:如何搜索以某些字符开头的字符串,
elasticsearch,
elasticsearch,我想从弹性搜索中搜索数据,在弹性搜索中,数据从开始的前n个字符开始匹配 下面是我在ES索引中的数据集/数据 { "took": 0, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, &qu
{
"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",
}
}
]
}
}
我尝试过使用query/term/prefix/wildcard方法进行搜索。我想查找以“Ind*”开头的名称,但作为响应,它返回的字符串在字符串中的任何位置都匹配。我只希望结果以“Ind*”开头,但结果应该是“India”,而不是其他
请建议如何查询ES以获得高于预期的结果。作为响应,查询返回字符串中任何位置匹配的字符串,因为字符串被分离为单个标记(基于标准分析器)。因此,它将在任何位置匹配包含India的所有文档,而不仅仅是在
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
}
]
}
搜索查询:
{
"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"
}
}
]
搜索结果:
{
"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"
}
}
]
@ChetanNage很高兴这对你有用:)你能接受并投票表决答案吗:)