Elasticsearch multi-query / search ด้วย Python

Sep 26 2020

ฉันเพิ่งเริ่มใช้ Python และ Elasticsearch และฉันได้สร้างดัชนีที่มีข้อมูลบางอย่างใน Elasticsearch และฉันต้องการดำเนินการค้นหาด้วย Python ตามตัวกรองบางอย่างที่ได้รับจากผู้ใช้ (คำหลักหมวดหมู่)

from elasticsearch import Elasticsearch
import json,requests

es = Elasticsearch(HOST="http://localhost", PORT=9200)
es = Elasticsearch()

def QueryMaker (keyword,category):
   response = es.search(index="main-news-test-data",body={"from":0,"size":10000,"query":{
       "bool": {
      "should": [
        {
          "multi_match" : {
            "query":      keyword,
            "fields":     [ "content", "title","lead" ]
          }
        },
        {
          "multi_match" : {
            "query":      category,
            "fields":     [ "category" ]
          }
        }
      ]
    }
   }})
   return(response)

def Mapper (category):
 fhandle = open('categories.txt','r', encoding="utf8")
 for line in fhandle:
     line = line.rstrip()
     items = line.split(';')
     if f'"{category}"' in items:
         category = items[0]
         return(category)

if __name__ == '__main__': 
    keyword = input('Enter Keyword: ')
    print(type(keyword))
    category = input('Enter Category: ')
    print(type(category))
    #startDate = input('Enter StartDate: ')
    #endDate = input('Enter EndDate: ')
    
    mapCategory = Mapper(category)
    if mapCategory is not None:
      mapCategory = mapCategory.replace("%","")
      data = QueryMaker(keyword,mapCategory)
      print(data)
    else:
      data = QueryMaker(keyword,mapCategory)
      print(data)

ปัญหาคือโปรแกรมนี้จะส่งคืนข้อมูลที่ตรงกันก็ต่อเมื่อ 2 ช่องนั้นเต็ม แต่ฉันต้องการให้มันส่งคืนข้อมูลด้วยถ้า 1 ช่องเช่นหมวดหมู่ว่างเปล่า เมื่อคีย์เวิร์ดว่างเปล่าเหมือน '' และจะไม่ส่งคืนอะไรเลยและเมื่อหมวดหมู่ว่างเปล่าฉันได้รับข้อผิดพลาดนี้:

elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[multi_match] unknown token [VALUE_NULL] after [query]')   

ฉันทำอะไรผิดและฉันจะแก้ไขตัวกรองการค้นหาของฉันได้อย่างไร

คำตอบ

ESCoder Sep 27 2020 at 01:57

การเพิ่มตัวอย่างการทำงานที่มีข้อมูลดัชนีคำค้นหาและผลการค้นหา

ตามความคิดเห็นของคุณที่กล่าวไว้ข้างต้นหากcontentไม่มีฟิลด์keywordและหากcategoryฟิลด์categoryนั้นมีข้อความค้นหาจะดำเนินการกับcategoryฟิลด์ ซึ่งสามารถทำได้โดยใช้minimum_should_match

ข้อมูลดัชนี:

{
    "content": "keyword a",
    "title": "b",
    "lead": "c",
    "category": "d"
}
{
    "content": "a",
    "title": "b",
    "lead": "c",
    "category": "category"
}
{
    "content": "keyword a",
    "title": "b",
    "lead": "c",
    "category": "category"
}

การค้นหา:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "keyword",
            "fields": [
              "content",
              "title",
              "lead"
            ]
          }
        },
        {
          "multi_match": {
            "query": "category",
            "fields": [
              "category"
            ]
          }
        }
      ],
      "minimum_should_match":1
    }
  }
}

ผลการค้นหา:

"hits": [
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9666445,
        "_source": {
          "content": "keyword a",
          "title": "b",
          "lead": "c",
          "category": "category"
        }
      },
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.60996956,
        "_source": {
          "content": "keyword a",
          "title": "b",
          "lead": "c",
          "category": "d"
        }
      },
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.35667494,
        "_source": {
          "content": "a",
          "title": "b",
          "lead": "c",
          "category": "category"
        }
      }
    ]