Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache range filter on date field by default #7122

Merged
merged 1 commit into from Aug 12, 2014

Conversation

dadoonet
Copy link
Member

@dadoonet dadoonet commented Aug 1, 2014

A range filter on a date field with a numeric from/to value is not cached by default:

DELETE /test

PUT /test/t/1
{
  "date": "2014-01-01"
}

GET /_validate/query?explain
{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "date": {
            "from": 0
          }
        }
      }
    }
  }
}

Returns:

"explanation": "ConstantScore(no_cache(date:[0 TO *]))"

This patch fixes as well not caching from/to when using now value not rounded.
Previously, a query like:

GET /_validate/query?explain
{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "date": {
            "from": "now"
            "to": "now/d+1"
          }
        }
      }
    }
  }
}

was cached.

Also, this patch does not cache anymore now even if the user asked for caching it.
As it won't be cached at all by definition.

Added as well tests for all possible combinations.

Closes #7114.

@dadoonet dadoonet self-assigned this Aug 1, 2014
@dadoonet
Copy link
Member Author

dadoonet commented Aug 1, 2014

@martijnvg Could you review my PR? Thanks! :)

} else {
String value = convertToString(lowerTerm);
cache = explicitCaching || !hasNowExpressionWithNoRounding(value);
cache = (explicitCaching != null && explicitCaching) || !hasNowExpressionWithNoRounding(value);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martijnvg The more I look at this, the more I think there something wrong here.
I tried to fix an issue when you explicitly set _cache:false. But in that case, even if you don't want to cache the filter, as soon as you don't use now, it will be cached because of || !hasNowExpressionWithNoRounding(value).

That's what we have in the past, so it's not a regression here.

Though, I think I need to update the PR with that finding.

Will rebase on master as recent changes - support timezone - created a git conflict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I missed this comment. You're right. If explicit caching is configured and set to true, we override it to false if now without rounding is used. If no explicit caching is defined we should set cache to true if now with no rounding is specified.

It should look something like this:

if (explicitCaching != null) {
  if (explicitCaching) {
     cache = hasNowExpressionWithNoRounding(value) == false;
  } else {
     cache = false;
  }
} else {
   cache = hasNowExpressionWithNoRounding(value) == false;
}

}


private boolean computeCache(Boolean explicitCaching, boolean defaultValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method looks unused?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh! I forgot to remove it! :)

@martijnvg
Copy link
Member

Left two comments, also the SimpleQueryTests#testRangeFilterNoCacheWithNow fails, because it assumes that a filter with now is being cached and that is no longer the case. Can you change that assertion as well?

@dadoonet
Copy link
Member Author

I don't understand. When I run test locally, I don't get any error for test org.elasticsearch.search.query.SimpleQueryTests#testRangeFilterNoCacheWithNow. Could you paste your seed?

@martijnvg
Copy link
Member

LGTM, I find the code much more understandable than was before!

A range filter on a date field with a numeric `from`/`to` value is **not** cached by default:

    DELETE /test

    PUT /test/t/1
    {
      "date": "2014-01-01"
    }

    GET /_validate/query?explain
    {
      "query": {
        "filtered": {
          "filter": {
            "range": {
              "date": {
                "from": 0
              }
            }
          }
        }
      }
    }

Returns:

    "explanation": "ConstantScore(no_cache(date:[0 TO *]))"

This patch fixes as well not caching `from`/`to` when using `now` value not rounded.
Previously, a query like:

    GET /_validate/query?explain
    {
      "query": {
        "filtered": {
          "filter": {
            "range": {
              "date": {
                "from": "now"
                "to": "now/d+1"
              }
            }
          }
        }
      }
    }

was cached.

Also, this patch does not cache anymore `now` even if the user asked for caching it.
As it won't be cached at all by definition.

Added as well tests for all possible combinations.

Closes elastic#7114.
@dadoonet dadoonet merged commit 9e68687 into elastic:master Aug 12, 2014
@dadoonet dadoonet deleted the issue/7114-cache-date-range-ms branch August 12, 2014 17:19
@clintongormley clintongormley changed the title Query DSL: Cache range filter on date field by default Cache range filter on date field by default Jun 7, 2015
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Query DSL labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories v1.3.2 v1.4.0.Beta1 v2.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query DSL: Cache range filter on date field by default
3 participants