0

I'm new to Python and Elasticsearch and I have created an index with some data in Elasticsearch and I want to perform a query on them with Python based on some filters that are received from the user (keyword, category)

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)

The problem is that this program only returns the matched data only if the 2 fields are full, but I want it to return data too if 1 field like category is empty. When the Keyword is empty its like ' ' and it returns nothing and when the Category is empty I receive this error:

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

What am I doing wrong and how can I fix my search filter?

5
  • return data too if 1 field like category is empty, regarding this, does that mean that category field is having the document like ' ', and you want to query on that empty field value ? Commented Sep 27, 2020 at 1:45
  • did you get a chance to go through my answer, looking forward to get feedback from you :) Commented Sep 27, 2020 at 5:23
  • @BhavyaGupta I mean, when the category field is empty, I only want to search by key word. when keyword is empty, I want to only search by category and when both are full, I want to search and filter the data by both Commented Sep 27, 2020 at 6:18
  • @BhavyaGupta like, sometimes the user enters no keyword but enters a category and I want to query the data with the category only, and vice versa Commented Sep 27, 2020 at 6:20
  • please go through my updated answer and let me know if this was your issue ? Commented Sep 27, 2020 at 7:04

1 Answer 1

0

Adding a working example with index data, search query, and search result

According to your comments mentioned above, if the content field does not contain keyword, and if the category field contains category, then search query will execute for category field. This can be achieved by using minimum_should_match

Index Data:

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

Search Query:

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

Search Result:

"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"
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

18 Comments

still doesnt work, now when I put the keyword empty:
{'took': 0, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}
and when the category is empty:
elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[multi_match] unknown token [VALUE_NULL] after [query]')
@bluxixi keyword is empty, that means? Are you not taking keyword field in one of the document?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.