0

I am trying to use elasticsearch in order to find documents with a rule based on two doc properties.

Lets say the documents are in the following structure:

{
  "customer_payment_timestamp" : 14387930787,
  "customer_delivery_timestamp" : 14387230787,
}

and i would like to query these kind of documents and find all documents where customer_payment_timestamp is greater than customer_delivery_timestamp.

Tried the official documentation, but I couldn't find any relevant example regarding the query itself or a pre-mapped field... is it even possible?

1 Answer 1

1

You can achieve this with a script filter like this:

POST index/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "doc.customer_payment_timestamp.value > doc. customer_delivery_timestamp.value"
        }
      }
    }
  }
}

Note: you need to make sure that dynamic scripting is enabled

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. i get the following error when using this: "reason": { "type": "script_exception", "reason": "scripts of type [inline], operation [search] and lang [groovy] are disabled" }
It did... Thanks! I guess that dynamic scripting hits the performance, right?
Yes, it does a little bit. In your case, you should probably create another boolean field payment_after_delivery at indexing time that contains that condition and then you'll be able to query that new field with a very simple term query
thought so, is it possible using the mapping file only or should i contaminate my entity?
You don't have to see this as a "contamination", it's simply helping you search your entities. It's not uncommon to have lots of "helper" fields being indexed in order to make searches faster.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.