0

I have a requirement to build a search query with the following set of rules.

  1. Get all the data which has the column / node active=true
  2. Get all the data which has the column / node active=false and also which is one week old.

For instance if i have a 1000 records in a index in which there are 70% of the records having active=true and 30% of the records having active=false. Out of 30% of the records 10% is updated recently. So i need to fetch only the 80% of the records i.e 70% (active=true) + 10% (active=false).

I have tried with filter query but it's returning only the 10% of the result i.e satisfying only the Rule 2 even though i have mentioned the Rule 1 in the should block it's not accepting.

   {
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "UpdatedOn": {
                  "gte": "now-7d/d",
                  "lte": "now/d"
                }
              }
            },
            {
              "match": {
                "Active": "false"
              }
            }
          ]
        }
      },
      "should": [
        {
          "match": {
            "Active": "true"
          }
        }
      ]
    }
  }
}

ElasticSearch Version 5.4

Thanks in advance.

1 Answer 1

2

You need to do it like this:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "match": {
            "Active": "true"
          }
        },
        {
          "bool": {
            "filter": [
              {
                "range": {
                  "UpdatedOn": {
                    "gte": "now-7d/d",
                    "lte": "now/d"
                  }
                }
              },
              {
                "match": {
                  "Active": "false"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

A quick question. I have removed the "minimum_should_match": 1 and i'm getting the same set of results. Do we really need this... ?
No you can remove it, I added it first because you were using must, but then I changed to filter (since no ranking is involved), hence you can remove the clause
i have checked the above query.It's not satisfying the condition. It's returning the results which is two weeks old.
Then you need to revise your conditions in your question ;-)
Condition 1 doesn't set any constraints on the time, so that's probably why you're receiving documents that are 2 weeks old. Which are the real conditions you want?
|

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.