下面是我的字典
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]'}
]
將玩家引入 elasticsearch
for i in abc:
es.index(index="players", body=i, id=i['id'])
下面是dsl查詢
resp = es.search(index="players",body={
"query": {
"query_string": {
"fields": ["id^12","description^2", "name^2"],
"query": "[email protected]"
}
}})
resp
問題1:如果
"fields": ["id^12","description^2", "name^2"]那么我得到錯誤RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: For input string: "[email protected]"'問題 2:如果我的欄位是
["description^2", "name^2"]我期待一個包含[email protected]但回傳所有 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}}}}}}}
uj5u.com熱心網友回復:
問題 1:如果 "fields": ["id^12","description^2", "name^2"] 那么我收到錯誤 RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: For input字串:“巴西@fifa.com”'
您遇到的上述問題是因為您的id欄位被定義為欄位integer或flot欄位型別(其他text欄位型別)。您需要"lenient": true在查詢中提供,它不會回傳任何例外。
問題 2:如果我的欄位是 ["description^2", "name^2"] 我期待一份包含 [email protected] 但回傳所有 3 份檔案的檔案
發生上述問題是因為您正在搜索在搜索時應用默認分析器text的欄位型別。standard
因此,當您搜索時[email protected],它將創建兩個令牌brazil和fifa.com. 在這里,fifa.com在您的所有 3 個檔案中都匹配,因此它回傳結果。要解決此問題,您可以使用description.keyword欄位。
以下查詢將解決您的兩個問題:
{
"query": {
"query_string": {
"lenient": true,
"fields": [
"id^12",
"description.keyword^2",
"name^2"
],
"query": "[email protected]"
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/496587.html
上一篇:限定符在結構指標上被丟棄
下一篇:在映射索引中不可能進行多重搜索?
