如何使Elasticsearch不区分大小写而不改变现有文档?

mgdq6dx1  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(143)

我是Elasticsearch的初学者,我正在做一个项目,其中的要求是寻找应该从提供的模式开始的字段值。
在本例中,我使用通配符作为搜索模式。(例如:Ste.*)。
此外,我使用_msearch选项在多个字段中进行搜索。
我需要匹配记录和匹配记录的计数。我使用聚合来获得计数。
然而,在搜索数值时没有问题。当我搜索String时,则存在大小写敏感的问题。当我搜索Ste时,我可以看到诸如Stephen,Steven和Stella的结果。
当我在小写中搜索ste时,没有返回任何内容。
下面是我尝试的脚本:

import logging
import os
import json
import boto3
import requests
import elasticsearch
from elasticsearch import Elasticsearch

ES_Index_Name = 'emp_details_1_1'
ES_Cluster_IP = 'localhost'

def lambda_handlet(event):
    ES_URL = f'http://{ES_Cluster_IP}:9200/{ES_Index_Name}/_msearch'
    print("URL - ", ES_URL)
    query_input = event

    query_input_star = '.*'
    query_input_re = query_input + query_input_star

    searchString = [ {"index": "emp_details_1_1"},
                     {"_source":[],"size":0,"min_score":1,"query":{"prefix":{"firstname.keyword":query_input}},"aggs":{"firstname":{"terms":{"field":"firstname.keyword","include":query_input_re}},"firstname_count":{"value_count":{"field":"firstname.keyword"}}}},
                     {"index": "emp_details_1_1"},
                     {"_source":[],"size":0,"min_score":1,"query":{"prefix":{"phone.keyword":query_input}},"aggs":{"phone":{"terms":{"field":"phone.keyword","include":query_input_re}},"phone_count":{"value_count":{"field":"phone.keyword"}}}}]
    searchQuery = ""    
    for d in searchString:
        searchQuery += json.dumps(d) + "," + "\n"
        print("API Global search fn: ",d)

    print("searchQuery: ", searchQuery)

    headers = {"Content-Type":"application/json","Accept":"test/plain"}
    searchResponse = requests.get(url=ES_URL, headers=headers, data=searchQuery)
    print("API Global response: ", json.loads(searchResponse.text))

lambda_handlet('M')

下面是索引的Map:

{
  "emp_details_1_1" : {
    "mappings" : {
      "properties" : {
        "firstname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "phone" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
      }
    }
  }
}

下面是示例输出:

{'took': 190, 'responses': [{'took': 190, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 3, 'relation': 'eq'}, 'max_score': None, 'hits': []}, 'aggregations': {'firstname': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 'buckets': [{'key': 'Masha', 'doc_count': 1}, {'key': 'Millard', 'doc_count': 1}, {'key': 'Monte', 'doc_count': 1}]}, 'firstname_count': {'value': 3}}, 'status': 200}, {'took': 20, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}, 'aggregations': {'phone_count': {'value': 0}, 'phone': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 'buckets': []}}, 'status': 200}]}

我如何修改我的脚本,使搜索执行不区分大小写的搜索,而不管我传递的字符串是小写还是大写?

5lwkijsr

5lwkijsr1#

增加大小写不敏感参数,[7.10.0]以后支持该功能

"query": {
        "prefix": {
            "firstname.keyword": {
                "value": "ste",
                "case_insensitive": true
            }
        }
    }

相关问题