requestError:requestError(400,search_phase_execution_exception未能在elasticsearch中为输入字符串创建查询

发布于 2025-02-11 14:06:14 字数 2337 浏览 3 评论 0 原文

下面是我的字典

abc = [
{'id':"1",  'name': 'cristiano ronaldo', 'description': '[email protected]'},
{'id':"2",  'name': 'lionel messi', 'description': '[email protected]'},
{'id':"3",  'name': 'Lionel Jr', 'description': '[email protected]'}
]

将播放器摄入

for i in abc:
    es.index(index="players", body=i, id=i['id'])

下面的Elasticsearch,是DSL查询

resp = es.search(index="players",body={
  "query": {
    "query_string": {
       "fields": ["id^12","description^2", "name^2"], 
      "query": "[email protected]"
    }
  }})
resp
  • 第1期: 如果“字段”:[“ ID^12”,“ Description^2”,“ name^2”] ,那么我正在获取错误 request> requesterror:requestError(400,'search_phase_execution_exection_exception','',''无法创建查询:输入字符串:“

  • 第2期: 如果我的字段是 [“ Descript^2”,“ Name^2”] 我希望一个文档包含 [email  pretained] ,但返回所有3个文档

:返回所有SAGAR我的设置ID的评论。映射在下面。 第1期已解决

{'players': {'mappings': {'properties': {'description': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
    'id': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
    'name': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}}}}}

Below is my dictionary

abc = [
{'id':"1",  'name': 'cristiano ronaldo', 'description': '[email protected]'},
{'id':"2",  'name': 'lionel messi', 'description': '[email protected]'},
{'id':"3",  'name': 'Lionel Jr', 'description': '[email protected]'}
]

Ingested the players into elasticsearch

for i in abc:
    es.index(index="players", body=i, id=i['id'])

Below is the dsl query

resp = es.search(index="players",body={
  "query": {
    "query_string": {
       "fields": ["id^12","description^2", "name^2"], 
      "query": "[email protected]"
    }
  }})
resp
  • Issue 1:
    if "fields": ["id^12","description^2", "name^2"] then i am getting error RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: For input string: "[email protected]"'

  • Issue 2:
    if my fields are ["description^2", "name^2"] I am expecting one document which contain [email protected] but returning all 3 documents

Edited: From the comment of sagar my setting id was long which i changed now . mapping is below. and issue1 is resolved

{'players': {'mappings': {'properties': {'description': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
    'id': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
    'name': {'type': 'text',
     'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}}}}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2025-02-18 14:06:14

问题1:如果“字段”:[“ id^12”,“描述^2”,“ name^2”],然后我是
获取错误请求请求:requestError(400,
'search_phase_execution_exception','未能创建查询:输入
字符串:“

f89a8a99829194b89e919e919e99e999d69b9795795 ID 字段定义为 Integer flot 字段类型(其他 text 字段类型)。您需要在查询中提供“ Lenient”:True ,并且不会返回任何例外。

问题2:如果我的字段是[“描述^2”,“ name^2”]我希望
一个包含

之所以发生,是因为您正在文本搜索时应用默认标准分析仪的字段类型。

因此,当您搜索 巴西 fifa.com 。在这里, fifa.com 在您的所有3个文档中都匹配,因此结果返回。要解决此问题,您可以使用 Description.keyword 字段。

下面的查询将解决您的两个问题:

{
  "query": {
    "query_string": {
      "lenient": true, 
      "fields": [
        "id^12",
        "description.keyword^2",
        "name^2"
      ],
      "query": "[email protected]"
    }
  }
}

更新:

如果要搜索 fifa ,则基于注释,然后您需要提供 Description 作为字段但是,当您搜索 您需要以双引号提供它以进行精确匹配。请参阅下面的示例:

{
  "query": {
    "query_string": {
      "lenient": true, 
      "fields": [
        "id^12",
        "description^2",
        "name^2"
      ],
      "query": "\"[email protected]\""
    }
  }
}

Issue 1: if "fields": ["id^12","description^2", "name^2"] then i am
getting error RequestError: RequestError(400,
'search_phase_execution_exception', 'failed to create query: For input
string: "[email protected]"'

Above issue you are getting because your id field is define as integer or flot type of field (other then text type of field). You need to provide "lenient": true in your query and it will not return any exception.

Issue 2: if my fields are ["description^2", "name^2"] I am expecting
one document which contain [email protected] but returning all 3
documents

Above issue is happening because you are searching on text type of field which applied default standard analyzer when you do search.

So when you search [email protected] then it will create two token brazil and fifa.com. Here, fifa.com is matching in your all 3 documents so it is returning in result. To resolved this issue, you can use description.keyword field.

Below query will resolved your both issue:

{
  "query": {
    "query_string": {
      "lenient": true, 
      "fields": [
        "id^12",
        "description.keyword^2",
        "name^2"
      ],
      "query": "[email protected]"
    }
  }
}

Updated:

Based on comment if you want to search fifa as well then you need to provide description as field but when you search [email protected] then you need to provide it in double quotes for exact match. Please see below example:

{
  "query": {
    "query_string": {
      "lenient": true, 
      "fields": [
        "id^12",
        "description^2",
        "name^2"
      ],
      "query": "\"[email protected]\""
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文