Python ile Elasticsearch çoklu sorgu / arama

Sep 26 2020

Python ve Elasticsearch'te yeniyim ve Elasticsearch'teki bazı verilerle bir dizin oluşturdum ve kullanıcıdan alınan bazı filtrelere (anahtar kelime, kategori) dayalı olarak Python ile bunlarla ilgili bir sorgu gerçekleştirmek istiyorum.

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)

Sorun şu ki, bu program sadece eşleşen verileri sadece 2 alan doluysa döndürüyor, ancak kategori gibi 1 alan boşsa da veri döndürmesini istiyorum. Anahtar Kelime boş olduğunda '' gibi ve hiçbir şey döndürmediğinde ve Kategori boş olduğunda şu hatayı alıyorum:

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

Neyi yanlış yapıyorum ve arama filtremi nasıl düzeltebilirim?

Yanıtlar

ESCoder Sep 27 2020 at 01:57

Dizin verileri, arama sorgusu ve arama sonucuyla çalışan bir örnek ekleme

Yukarıda belirttiğiniz yorumlarınıza göre eğer contentalan içermiyorsa keywordve categoryalan içeriyorsa alan categoryiçin arama sorgusu çalıştırılacaktır category. Bu, kullanılarak elde edilebilirminimum_should_match

Endeks Verileri:

{
    "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"
}

Arama Sorgusu:

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

Arama sonuçları:

"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"
        }
      }
    ]