0

I am using elasticsearch-dsl in my django project. Elasticsearch is totally new for me and at some point i got this error: error

So i started debugging and removing some part of my code. But here i am my code has become extremely simple but i still don't know why i get this error.

The code:

import re

from django.conf import settings
from elasticsearch_dsl.query import Bool, Match, MatchPhrase, Nested

from .documents import RubricPageDocument


def _build_match_query(query, field):
    if len(query) <= 4:
        return Match(**{field: {'query': query}})

    if re.match('^".*"$', query):
        return MatchPhrase(**{field: {'query': query}})

    return Match(**{field: {'query': query, 'fuzziness': 'AUTO'}})


def search_query_in_rubric_pages(query):
    q_title = _build_match_query(query, 'title')

    q = q_title

    s = RubricPageDocument.search().query(q)

    response = s.execute() # error raise on this line

    return [_format_hit(hit, 'rubric') for hit in response] 


def search_query(query):
    """
    search in elastic search index for rubric pages
    * query (str): the query

    """

    page_hits = search_query_in_rubric_pages(query)

    return {
        'hits': page_hits,
    }

Please help me

ZvNico
  • 1
  • What is the ES version you are using? Can you also give the actual ES query being generated by your code? – Nishikant Tayade Feb 24 '22 at 12:00
  • @NishikantTayade you can see the query generated in the error i linked, after formatting it from the raise error it's something like this : { "match" : { "title" : { "query" : "eqsd", "operator" : "OR", "prefix_length" : 0, "max_expansions" : 50, "fuzzy_transpositions" : true, "lenient" : false, "zero_terms_query" : "NONE", "auto_generate_synonyms_phrase_query" : true, "boost" : 1.0 } } } – ZvNico Feb 24 '22 at 12:14
  • what is the mapping for title, is it text field? – Nishikant Tayade Feb 24 '22 at 12:17
  • @NishikantTayade yes it is – ZvNico Feb 24 '22 at 12:24
  • @NishikantTayade i forgot to tell you the version: elasticsearch==7.15.2 elasticsearch-dsl==7.2.0 – ZvNico Feb 24 '22 at 12:29
  • Can you try removing ** from Match(**{field: {'query': query}}) and other match query as well as --> Match({field: {'query': query}}) – Nishikant Tayade Feb 24 '22 at 13:22
  • You should try to match the elasticsearch-dsl version with your elasticsearch version, otherwise you're bound to encounter incompatibility issues – Val Feb 24 '22 at 14:08

0 Answers0