0

I am querying elasticsearch in python. I want to change a parameter 'default operator' in case query returns zero values. Here's my code:

default_operator = "and"
if searchfields is not None:
    if query is not None and query != '':
        queries.append(Q("simple_query_string", query=query, default_operator= default_operator,
                        flags="PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE", fields=searchfields))

There is a long code after this in which different filters are being made and added to the query. But here's the code where I am trying to change the default operator:

res = s.execute()
if res["hits"]["total"]["value"] == 0:
   default_operator = "or"
   print("Query with default operator OR: ", str(queries))
   res = s.execute()
else:
  pass

But it is not changing the default operator. How can I do this?

sheharbano
  • 211
  • 2
  • 13
  • Do you recreate the query itself with new value of `default_operator`? – ilvar Jan 07 '22 at 16:05
  • That's the question that how to update a query without having to recreate it all together? – sheharbano Jan 10 '22 at 10:22
  • The same code, `Q("simple_query_string", query=query, default_operator= default_operator, flags="PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE", fields=searchfields)` – ilvar Jan 10 '22 at 14:45

0 Answers0