FrancMo
FrancMo

Reputation: 2699

How can I generate an elasticsearch query from a boolean expression, using Python?

I'd like to "translate" a string like:

A AND (C OR B) AND NOT D

into an Elasticsearch query like:

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "text": "A"
        }
      },
      "must_not": {
        "term": {
          "text": "D"
        }
      },
      "should": [
        {
          "term": {
            "text": "B"
          }
        },
        {
          "term": {
            "text": "C"
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1
    }
  }
}

does exists some library which I can use ? any help appreciated

Thanks!

Upvotes: 1

Views: 1112

Answers (1)

FrancMo
FrancMo

Reputation: 2699

ok according to:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

I can do query like:

{
    "query": {
        "query_string" : {
            "default_field" : "text",
            "query" : (this AND (submitted OR flowers) AND NOT blight"
        }
    }
}

which works great.

Upvotes: 1

Related Questions