亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python中Elasticsearch多字段查詢請求

Python中Elasticsearch多字段查詢請求

繁星coding 2023-09-12 20:10:53
我是 Elasticsearch 和 Python 的初學者,我在 Elasticsearch 中使用一些數據創建了一個索引,我想使用 python 對這些數據執行查詢請求。這是我在 Kibana 開發工具中創建的數據映射:PUT /main-news-test-data{  "mappings": {    "properties": {      "content": {        "type": "text"      },      "title": {        "type": "text"      },      "lead": {        "type": "text"      },      "agency": {        "type": "keyword"      },      "date_created": {        "type": "date"      },      "url": {        "type": "keyword"      },      "image": {        "type": "keyword"      },      "category": {        "type": "keyword"      },      "id":{        "type": "keyword"      }    }  }}這是我的 Python 代碼,其中我們給它一個關鍵字和一個類別號,它必須檢查彈性數據的標題、引導和內容字段中是否有匹配的關鍵字,并檢查輸入的類別號和數據類別號并返回/打印出符合此條件的任何對象:from elasticsearch import Elasticsearchimport json,requestses = Elasticsearch(HOST="http://localhost", PORT=9200)es = Elasticsearch()def QueryMaker (keyword,category):   response = es.search(index="main-news-test-data",body={"from":0,"size":5,"query":{"multi_match":{       "content":keyword,"category":category,"title":keyword,"lead":keyword}}})   return(response)if __name__ == '__main__':     keyword = input('Enter Keyword: ')    category = input('Enter Category: ')    #startDate = input('Enter StartDate: ')    #endDate = input('Enter EndDate: ')    data = QueryMaker(keyword,category)    print(data)但是當我將數據提供給輸入時,我收到此錯誤:elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[multi_match] query does not support [content]')我究竟做錯了什么?編輯:關鍵字必須包含在標題、線索和內容中,但不必與它們相同
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

您的multi_match查詢語法在這里是錯誤的,我也認為您需要這樣的東西,

{

? "from":0,

? "size":5,

? "query": {

? ? "bool": {

? ? ? "should": [

? ? ? ? {

? ? ? ? ? "multi_match" : {

? ? ? ? ? ? "query":? ? ? keyword,

? ? ? ? ? ? "fields":? ? ?[ "content", "title","lead" ]

? ? ? ? ? }

? ? ? ? },

? ? ? ? {

? ? ? ? ? "multi_match" : {

? ? ? ? ? ? "query":? ? ? category,

? ? ? ? ? ? "fields":? ? ?[ "category" ]

? ? ? ? ? }

? ? ? ? }

? ? ? ]

? ? }

? }

}


查看完整回答
反對 回復 2023-09-12
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號