Querying Array data type in elasticsearch using python_dsl

682 views Asked by At

How complex can a query be on an integer array data type? Here is my class in python to inject data into elasticsearch:

class Paragraph(DocType):
    body = Text(analyzer="standard")
    published_from = Date()
    lines = Integer()
    n_paragraph = Integer()
    capture = Integer()

    class Meta:
        index = "my_index"

    def save(self, **kwargs):
        self.lines = len(self.body.split())
        return super(Paragraph, self).save(**kwargs)

I am injecting an array of integer in capture. Here is the interesting line :

paragraph.capture = [1, 0, 5, 7]
  1. I manage to query if a number is in the list:: cnx = Search().using(client) s = cnx.query("match", capture=5)

  2. as @Val said we can add another field that contains sum to query the sum

How to query a specific index e.g. paragraph.capture[1] >= 1 ?

we saw that Elasticsearch query on array index is related but I could not make the link.

1

There are 1 answers

0
Val On

The best way to query the sum is to add another field that contains it so you don't have to run a costly script query at search time.

Querying if at least one number is superior to 4 can already be done with a normal range query on the capture field.