【问题标题】:Elastic Search : How to search string starting with some characters弹性搜索:如何搜索以某些字符开头的字符串
【发布时间】:2020-12-03 04:07:32
【问题描述】:

我想从弹性搜索中搜索数据,其中数据从开头的前 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 以获得高于预期的结果。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    作为响应,查询返回在字符串中的任何位置匹配的字符串,因为字符串被分成单独的标记(基于标准分析器)。因此,它将匹配在任何位置包含 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
        }
      ]
    }
    

    要实现您的用例,您需要将name 字段设置为keyword 类型的字段

    索引映射:

    {
      "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 很高兴这对你有用:) 你能接受并投票赞成答案吗:)
    猜你喜欢
    • 2022-11-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多