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

Constant score percolate query incorrectly matching date range query #52617

Closed
davidbkemp opened this issue Feb 21, 2020 · 3 comments · Fixed by #52748
Closed

Constant score percolate query incorrectly matching date range query #52617

davidbkemp opened this issue Feb 21, 2020 · 3 comments · Fixed by #52748
Assignees
Labels
>bug :Search Relevance/Percolator Reverse search: find queries that match a document Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch

Comments

@davidbkemp
Copy link
Contributor

Elasticsearch version 7.5.1

Plugins installed: []

JVM version 1.8.0_232

OS version Darwin C02XF3HBJHD2 18.7.0 Darwin Kernel Version 18.7.0: Sun Dec 1 18:59:03 PST 2019; root:xnu-4903.278.19~1/RELEASE_X86_64 x86_64

A percolate search wrapped in a constant score query seems to incorrectly match range queries.

Steps to reproduce:

  1. Create an index
PUT /myindex
{
  "mappings": {
    "properties": {
      "d": {
        "type": "date"
      },
      "q": {
        "type": "percolator"
      }
    }
  }
}
  1. Index a couple of range queries that should be mutually exclisive

PUT /myindex/_doc/1
{
  "q": {
    "bool": {
      "must": [
        {
          "range": {
            "d": {
              "gt": "now"
            }
          }
        }
      ]
    }
  }
}


PUT /myindex/_doc/2
{
  "q": {
    "bool": {
      "must": [
        {
          "range": {
            "d": {
              "lt": "now"
            }
          }
        }
      ]
    }
  }
}
  1. Perform a constant-score percolate query
GET /myindex/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "percolate": {
          "field": "q",
          "document": {
            "d": "2020-02-18T15:00:00.000+11:00"
          }
        }
      }
    }
  }
}

Expected: Only one of the saved queries should match (document id "2").

Actual: Both of the saved queries match.

Note that you do get the correct behaviour if you do not use constant_score.
i.e. the following search gives the expected behaviour.

GET /myindex/_search
{
  "query": {
    "percolate": {
      "field": "q",
      "document": {
        "d": "2020-02-18T15:05:00.000+11:00"
      }
    }
  }
}
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/Percolator)

@matriv
Copy link
Contributor

matriv commented Feb 21, 2020

Reproduces in master as well.

@romseygeek
Copy link
Contributor

This is due to the way we handle range queries that contain now, which get rewritten in preprocessing to a match_all. When the percolator query is wrapped in a constant score query, we bypass checking the query itself and use the verified short cut - which in this case, will always return true, because match_all is always verified.

romseygeek added a commit that referenced this issue Feb 25, 2020
Currently, date ranges queries using NOW-based date math are rewritten to
MatchAllDocs queries when being preprocessed for the percolator. However,
since we added the verification step, this can result in incorrect matches when
percolator queries are run without scores. This commit changes things to instead
wrap date queries that use NOW with a new DateRangeIncludingNowQuery.
This is a simple wrapper query that returns its delegate at rewrite time, but it can
be detected by the percolator QueryAnalyzer and be dealt with accordingly.

This also allows us to remove a method on QueryRewriteContext, and push all
logic relating to NOW-based ranges into the DateFieldMapper.

Fixes #52617
romseygeek added a commit that referenced this issue Feb 25, 2020
Currently, date ranges queries using NOW-based date math are rewritten to
MatchAllDocs queries when being preprocessed for the percolator. However,
since we added the verification step, this can result in incorrect matches when
percolator queries are run without scores. This commit changes things to instead
wrap date queries that use NOW with a new DateRangeIncludingNowQuery.
This is a simple wrapper query that returns its delegate at rewrite time, but it can
be detected by the percolator QueryAnalyzer and be dealt with accordingly.

This also allows us to remove a method on QueryRewriteContext, and push all
logic relating to NOW-based ranges into the DateFieldMapper.

Fixes #52617
@javanna javanna added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search Relevance/Percolator Reverse search: find queries that match a document Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants