mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 17:34:17 -04:00
Docs: Remove docs for the filtered
, and
, or
and (f)query
queries.
This commit is contained in:
parent
a4fbc275b8
commit
86f1b07df0
24 changed files with 179 additions and 356 deletions
|
@ -148,7 +148,7 @@ Example:
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"query" : {
|
"query" : {
|
||||||
"filtered" : { "filter": { "range" : { "price" : { "to" : "500" } } } }
|
"constant_score" : { "filter": { "range" : { "price" : { "to" : "500" } } } }
|
||||||
},
|
},
|
||||||
"aggs" : {
|
"aggs" : {
|
||||||
"prices" : {
|
"prices" : {
|
||||||
|
|
|
@ -9,8 +9,7 @@ Assuming the data consists of documents representing stock ticks, where each tic
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"query" : {
|
"query" : {
|
||||||
"filtered" : {
|
"constant_score" : {
|
||||||
"query" : { "match_all" : {}},
|
|
||||||
"filter" : {
|
"filter" : {
|
||||||
"range" : { "timestamp" : { "from" : "now/1d+9.5h", "to" : "now/1d+16h" }}
|
"range" : { "timestamp" : { "from" : "now/1d+9.5h", "to" : "now/1d+16h" }}
|
||||||
}
|
}
|
||||||
|
|
|
@ -862,17 +862,17 @@ In the previous section, we skipped over a little detail called the document sco
|
||||||
|
|
||||||
But queries do not always need to produce scores, in particular when they are only used for "filtering" the document set. Elasticsearch detects these situations and automatically optimizes query execution in order not to compute useless scores.
|
But queries do not always need to produce scores, in particular when they are only used for "filtering" the document set. Elasticsearch detects these situations and automatically optimizes query execution in order not to compute useless scores.
|
||||||
|
|
||||||
To understand filters, let's first introduce the <<query-dsl-filtered-query,`filtered` query>>, which allows you to combine a query (like `match_all`, `match`, `bool`, etc.) together with another query which is only used for filtering. As an example, let's introduce the <<query-dsl-range-query,`range` query>>, which allows us to filter documents by a range of values. This is generally used for numeric or date filtering.
|
The <<query-dsl-range-query,`range` query>> that we introduced in the previous section also supports `filter` clauses which allow to use a query to restrict the documents that will be matched by other clauses, without changing how scores are computed. As an example, let's introduce the <<query-dsl-range-query,`range` query>>, which allows us to filter documents by a range of values. This is generally used for numeric or date filtering.
|
||||||
|
|
||||||
This example uses a filtered query to return all accounts with balances between 20000 and 30000, inclusive. In other words, we want to find accounts with a balance that is greater than or equal to 20000 and less than or equal to 30000.
|
This example uses a bool query to return all accounts with balances between 20000 and 30000, inclusive. In other words, we want to find accounts with a balance that is greater than or equal to 20000 and less than or equal to 30000.
|
||||||
|
|
||||||
[source,sh]
|
[source,sh]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
|
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered": {
|
"bool": {
|
||||||
"query": { "match_all": {} },
|
"must": { "match_all": {} },
|
||||||
"filter": {
|
"filter": {
|
||||||
"range": {
|
"range": {
|
||||||
"balance": {
|
"balance": {
|
||||||
|
@ -886,9 +886,9 @@ curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
|
||||||
}'
|
}'
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
Dissecting the above, the filtered query contains a `match_all` query (the query part) and a `range` query (the filter part). We can substitute any other queries into the query and the filter parts. In the above case, the range query makes perfect sense since documents falling into the range all match "equally", i.e., no document is more relevant than another.
|
Dissecting the above, the bool query contains a `match_all` query (the query part) and a `range` query (the filter part). We can substitute any other queries into the query and the filter parts. In the above case, the range query makes perfect sense since documents falling into the range all match "equally", i.e., no document is more relevant than another.
|
||||||
|
|
||||||
In addition to the `match_all`, `match`, `bool`, `filtered`, and `range` queries, there are a lot of other query types that are available and we won't go into them here. Since we already have a basic understanding of how they work, it shouldn't be too difficult to apply this knowledge in learning and experimenting with the other query types.
|
In addition to the `match_all`, `match`, `bool`, and `range` queries, there are a lot of other query types that are available and we won't go into them here. Since we already have a basic understanding of how they work, it shouldn't be too difficult to apply this knowledge in learning and experimenting with the other query types.
|
||||||
|
|
||||||
=== Executing Aggregations
|
=== Executing Aggregations
|
||||||
|
|
||||||
|
|
|
@ -188,7 +188,7 @@ GET /_count
|
||||||
Also, the top-level `filter` parameter in search has been renamed to
|
Also, the top-level `filter` parameter in search has been renamed to
|
||||||
<<search-request-post-filter,`post_filter`>>, to indicate that it should not
|
<<search-request-post-filter,`post_filter`>>, to indicate that it should not
|
||||||
be used as the primary way to filter search results (use a
|
be used as the primary way to filter search results (use a
|
||||||
<<query-dsl-filtered-query,`filtered` query>> instead), but only to filter
|
<<query-dsl-bool-query,`bool` query>> instead), but only to filter
|
||||||
results AFTER aggregations have been calculated.
|
results AFTER aggregations have been calculated.
|
||||||
|
|
||||||
This example counts the top colors in all matching docs, but only returns docs
|
This example counts the top colors in all matching docs, but only returns docs
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
[[query-dsl-and-query]]
|
|
||||||
=== And Query
|
|
||||||
|
|
||||||
deprecated[2.0.0-beta1, Use the `bool` query instead]
|
|
||||||
|
|
||||||
A query that matches documents using the `AND` boolean operator on other
|
|
||||||
queries.
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered" : {
|
|
||||||
"query" : {
|
|
||||||
"term" : { "name.first" : "shay" }
|
|
||||||
},
|
|
||||||
"filter" : {
|
|
||||||
"and" : [
|
|
||||||
{
|
|
||||||
"range" : {
|
|
||||||
"postDate" : {
|
|
||||||
"from" : "2010-03-01",
|
|
||||||
"to" : "2010-04-01"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"prefix" : { "name.second" : "ba" }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
||||||
|
|
|
@ -41,18 +41,6 @@ documents which also match a `negative` query.
|
||||||
|
|
||||||
Execute one query for the specified indices, and another for other indices.
|
Execute one query for the specified indices, and another for other indices.
|
||||||
|
|
||||||
<<query-dsl-and-query,`and`>>, <<query-dsl-or-query,`or`>>, <<query-dsl-not-query,`not`>>::
|
|
||||||
|
|
||||||
Synonyms for the `bool` query.
|
|
||||||
|
|
||||||
<<query-dsl-filtered-query,`filtered` query>>::
|
|
||||||
|
|
||||||
Combine a query clause in query context with another in filter context. deprecated[2.0.0-beta1,Use the `bool` query instead]
|
|
||||||
|
|
||||||
<<query-dsl-limit-query,`limit` query>>::
|
|
||||||
|
|
||||||
Limits the number of documents examined per shard.
|
|
||||||
|
|
||||||
|
|
||||||
include::constant-score-query.asciidoc[]
|
include::constant-score-query.asciidoc[]
|
||||||
include::bool-query.asciidoc[]
|
include::bool-query.asciidoc[]
|
||||||
|
@ -60,10 +48,5 @@ include::dis-max-query.asciidoc[]
|
||||||
include::function-score-query.asciidoc[]
|
include::function-score-query.asciidoc[]
|
||||||
include::boosting-query.asciidoc[]
|
include::boosting-query.asciidoc[]
|
||||||
include::indices-query.asciidoc[]
|
include::indices-query.asciidoc[]
|
||||||
include::and-query.asciidoc[]
|
|
||||||
include::not-query.asciidoc[]
|
include::not-query.asciidoc[]
|
||||||
include::or-query.asciidoc[]
|
|
||||||
include::filtered-query.asciidoc[]
|
|
||||||
include::limit-query.asciidoc[]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
[[query-dsl-filtered-query]]
|
|
||||||
=== Filtered Query
|
|
||||||
|
|
||||||
deprecated[2.0.0-beta1, Use the `bool` query instead with a `must` clause for the query and a `filter` clause for the filter]
|
|
||||||
|
|
||||||
The `filtered` query is used to combine a query which will be used for
|
|
||||||
scoring with another query which will only be used for filtering the result
|
|
||||||
set.
|
|
||||||
|
|
||||||
TIP: Exclude as many document as you can with a filter, then query just the
|
|
||||||
documents that remain.
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered": {
|
|
||||||
"query": {
|
|
||||||
"match": { "tweet": "full text search" }
|
|
||||||
},
|
|
||||||
"filter": {
|
|
||||||
"range": { "created": { "gte": "now-1d/d" }}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
||||||
|
|
||||||
The `filtered` query can be used wherever a `query` is expected, for instance,
|
|
||||||
to use the above example in search request:
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
curl -XGET localhost:9200/_search -d '
|
|
||||||
{
|
|
||||||
"query": {
|
|
||||||
"filtered": { <1>
|
|
||||||
"query": {
|
|
||||||
"match": { "tweet": "full text search" }
|
|
||||||
},
|
|
||||||
"filter": {
|
|
||||||
"range": { "created": { "gte": "now-1d/d" }}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'
|
|
||||||
--------------------------------------------------
|
|
||||||
<1> The `filtered` query is passed as the value of the `query`
|
|
||||||
parameter in the search request.
|
|
||||||
|
|
||||||
==== Filtering without a query
|
|
||||||
|
|
||||||
If a `query` is not specified, it defaults to the
|
|
||||||
<<query-dsl-match-all-query,`match_all` query>>. This means that the
|
|
||||||
`filtered` query can be used to wrap just a filter, so that it can be used
|
|
||||||
wherever a query is expected.
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
curl -XGET localhost:9200/_search -d '
|
|
||||||
{
|
|
||||||
"query": {
|
|
||||||
"filtered": { <1>
|
|
||||||
"filter": {
|
|
||||||
"range": { "created": { "gte": "now-1d/d" }}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'
|
|
||||||
--------------------------------------------------
|
|
||||||
<1> No `query` has been specified, so this request applies just the filter,
|
|
||||||
returning all documents created since yesterday.
|
|
||||||
|
|
||||||
===== Multiple filters
|
|
||||||
|
|
||||||
Multiple filters can be applied by wrapping them in a
|
|
||||||
<<query-dsl-bool-query,`bool` query>>, for example:
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered": {
|
|
||||||
"query": { "match": { "tweet": "full text search" }},
|
|
||||||
"filter": {
|
|
||||||
"bool": {
|
|
||||||
"must": { "range": { "created": { "gte": "now-1d/d" }}},
|
|
||||||
"should": [
|
|
||||||
{ "term": { "featured": true }},
|
|
||||||
{ "term": { "starred": true }}
|
|
||||||
],
|
|
||||||
"must_not": { "term": { "deleted": false }}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
|
@ -22,8 +22,8 @@ Then the following simple query can be executed with a
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -75,8 +75,8 @@ representation of the geo point, the filter can accept it as well:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"boost" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -106,8 +106,8 @@ conform with http://geojson.org/[GeoJSON].
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -130,8 +130,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -152,8 +152,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -181,8 +181,8 @@ values separately.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -227,8 +227,8 @@ are not supported. Here is an example:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -22,8 +22,8 @@ filter:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -51,8 +51,8 @@ representation of the geo point, the filter can accept it as well:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -77,8 +77,8 @@ conform with http://geojson.org/[GeoJSON].
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -99,8 +99,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -119,8 +119,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -6,8 +6,8 @@ Filters documents that exists within a range from a specific point:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"boost" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -7,7 +7,7 @@ points. Here is an example:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"query" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
|
@ -53,8 +53,8 @@ conform with http://geojson.org/[GeoJSON].
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -80,8 +80,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -105,8 +105,8 @@ Format in `lat,lon`.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -40,8 +40,8 @@ The following query will find the point using the Elasticsearch's
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"query":{
|
"query":{
|
||||||
"filtered": {
|
"bool": {
|
||||||
"query": {
|
"must": {
|
||||||
"match_all": {}
|
"match_all": {}
|
||||||
},
|
},
|
||||||
"filter": {
|
"filter": {
|
||||||
|
@ -81,8 +81,8 @@ shape:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered": {
|
"bool": {
|
||||||
"query": {
|
"must": {
|
||||||
"match_all": {}
|
"match_all": {}
|
||||||
},
|
},
|
||||||
"filter": {
|
"filter": {
|
||||||
|
|
|
@ -43,8 +43,8 @@ next to the given cell.
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"match_all" : {}
|
"match_all" : {}
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
[[query-dsl-limit-query]]
|
|
||||||
=== Limit Query
|
|
||||||
|
|
||||||
A limit query limits the number of documents (per shard) to execute on.
|
|
||||||
For example:
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered" : {
|
|
||||||
"filter" : {
|
|
||||||
"limit" : {"value" : 100}
|
|
||||||
},
|
|
||||||
"query" : {
|
|
||||||
"term" : { "name.first" : "shay" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
|
@ -6,8 +6,8 @@ A query that filters out matched documents using a query. For example:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"term" : { "name.first" : "shay" }
|
"term" : { "name.first" : "shay" }
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -29,8 +29,8 @@ Or, in a longer form with a `filter` element:
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"term" : { "name.first" : "shay" }
|
"term" : { "name.first" : "shay" }
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
[[query-dsl-or-query]]
|
|
||||||
=== Or Query
|
|
||||||
|
|
||||||
deprecated[2.0.0-beta1, Use the `bool` query instead]
|
|
||||||
|
|
||||||
A query that matches documents using the `OR` boolean operator on other
|
|
||||||
queries.
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered" : {
|
|
||||||
"query" : {
|
|
||||||
"term" : { "name.first" : "shay" }
|
|
||||||
},
|
|
||||||
"filter" : {
|
|
||||||
"or" : [
|
|
||||||
{
|
|
||||||
"term" : { "name.second" : "banon" }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"term" : { "name.nick" : "kimchy" }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
=== Script Query
|
=== Script Query
|
||||||
|
|
||||||
A query allowing to define
|
A query allowing to define
|
||||||
<<modules-scripting,scripts>> as filters. For
|
<<modules-scripting,scripts>> as queries. They are typically used in a filter
|
||||||
example:
|
context, for example:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
...
|
...
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
@ -28,8 +28,8 @@ to use the ability to pass parameters to the script itself, for example:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
...
|
...
|
||||||
},
|
},
|
||||||
"filter" : {
|
"filter" : {
|
||||||
|
|
|
@ -79,16 +79,12 @@ curl -XPUT localhost:9200/tweets/tweet/1 -d '{
|
||||||
# search on all the tweets that match the followers of user 2
|
# search on all the tweets that match the followers of user 2
|
||||||
curl -XGET localhost:9200/tweets/_search -d '{
|
curl -XGET localhost:9200/tweets/_search -d '{
|
||||||
"query" : {
|
"query" : {
|
||||||
"filtered" : {
|
"terms" : {
|
||||||
"filter" : {
|
"user" : {
|
||||||
"terms" : {
|
"index" : "users",
|
||||||
"user" : {
|
"type" : "user",
|
||||||
"index" : "users",
|
"id" : "2",
|
||||||
"type" : "user",
|
"path" : "followers"
|
||||||
"id" : "2",
|
|
||||||
"path" : "followers"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,20 +77,6 @@ in ``query context'' and as a filter in ``filter context'' (see <<query-dsl>>).
|
||||||
Queries and filters have been merged. Any query clause can now be used as a query
|
Queries and filters have been merged. Any query clause can now be used as a query
|
||||||
in ``query context'' and as a filter in ``filter context'' (see <<query-dsl>>).
|
in ``query context'' and as a filter in ``filter context'' (see <<query-dsl>>).
|
||||||
|
|
||||||
[role="exclude",id="query-dsl-and-filter"]
|
|
||||||
=== And Filter
|
|
||||||
|
|
||||||
The `and` filter has been replaced by the <<query-dsl-and-query>>. It behaves
|
|
||||||
as a query in ``query context'' and as a filter in ``filter context'' (see
|
|
||||||
<<query-dsl>>).
|
|
||||||
|
|
||||||
[role="exclude",id="query-dsl-or-filter"]
|
|
||||||
=== Or Filter
|
|
||||||
|
|
||||||
The `or` filter has been replaced by the <<query-dsl-or-query>>. It behaves
|
|
||||||
as a query in ``query context'' and as a filter in ``filter context'' (see
|
|
||||||
<<query-dsl>>).
|
|
||||||
|
|
||||||
[role="exclude",id="query-dsl-not-filter"]
|
[role="exclude",id="query-dsl-not-filter"]
|
||||||
=== Not Filter
|
=== Not Filter
|
||||||
|
|
||||||
|
@ -195,13 +181,6 @@ The `indices` filter has been replaced by the <<query-dsl-indices-query>>. It b
|
||||||
as a query in ``query context'' and as a filter in ``filter context'' (see
|
as a query in ``query context'' and as a filter in ``filter context'' (see
|
||||||
<<query-dsl>>).
|
<<query-dsl>>).
|
||||||
|
|
||||||
[role="exclude",id="query-dsl-limit-filter"]
|
|
||||||
=== Limit Filter
|
|
||||||
|
|
||||||
The `limit` filter has been replaced by the <<query-dsl-limit-query>>.
|
|
||||||
It behaves as a query in ``query context'' and as a filter in ``filter
|
|
||||||
context'' (see <<query-dsl>>).
|
|
||||||
|
|
||||||
[role="exclude",id="query-dsl-match-all-filter"]
|
[role="exclude",id="query-dsl-match-all-filter"]
|
||||||
=== Match All Filter
|
=== Match All Filter
|
||||||
|
|
||||||
|
@ -381,3 +360,86 @@ The shard query cache has been renamed <<shard-request-cache>>.
|
||||||
=== Query cache
|
=== Query cache
|
||||||
|
|
||||||
The filter cache has been renamed <<query-cache>>.
|
The filter cache has been renamed <<query-cache>>.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-filtered-query"]
|
||||||
|
=== Filtered query
|
||||||
|
|
||||||
|
The `filtered` query is replaced in favour of the <<query-dsl-bool-query,bool>> query. Instead of
|
||||||
|
the following:
|
||||||
|
|
||||||
|
[source,js]
|
||||||
|
-------------------------
|
||||||
|
GET _search
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"filtered": {
|
||||||
|
"query": {
|
||||||
|
"match": {
|
||||||
|
"text": "quick brown fox"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"filter": {
|
||||||
|
"term": {
|
||||||
|
"status": "published"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
move the query and filter to the `must` and `filter` parameters in the `bool`
|
||||||
|
query:
|
||||||
|
|
||||||
|
[source,js]
|
||||||
|
-------------------------
|
||||||
|
GET _search
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must": {
|
||||||
|
"match": {
|
||||||
|
"text": "quick brown fox"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"filter": {
|
||||||
|
"term": {
|
||||||
|
"status": "published"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-or-query"]
|
||||||
|
=== Or query
|
||||||
|
|
||||||
|
The `or` query is replaced in favour of the <<query-dsl-bool-query,bool>> query.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-or-filter"]
|
||||||
|
=== Or filter
|
||||||
|
|
||||||
|
The `or` filter is replaced in favour of the <<query-dsl-bool-query,bool>> query.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-and-query"]
|
||||||
|
=== And query
|
||||||
|
|
||||||
|
The `and` query is replaced in favour of the <<query-dsl-bool-query,bool>> query.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-and-filter"]
|
||||||
|
=== And filter
|
||||||
|
|
||||||
|
The `and` filter is replaced in favour of the <<query-dsl-bool-query,bool>> query.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-limit-query"]
|
||||||
|
=== Limit query
|
||||||
|
|
||||||
|
The `limit` query is replaced in favour of the <<search-request-body,terminate_after>>
|
||||||
|
parameter of search requests.
|
||||||
|
|
||||||
|
[role="exclude",id="query-dsl-limit-filter"]
|
||||||
|
=== Limit filter
|
||||||
|
|
||||||
|
The `limit` filter is replaced in favour of the <<search-request-body,terminate_after>>
|
||||||
|
parameter of search requests.
|
||||||
|
|
|
@ -34,8 +34,8 @@ only the relevant shard:
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
|
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"query_string" : {
|
"query_string" : {
|
||||||
"query" : "some query string here"
|
"query" : "some query string here"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,16 @@
|
||||||
[[search-request-named-queries-and-filters]]
|
[[search-request-named-queries-and-filters]]
|
||||||
=== Named Queries and Filters
|
=== Named Queries
|
||||||
|
|
||||||
Each filter and query can accept a `_name` in its top level definition.
|
Each filter and query can accept a `_name` in its top level definition.
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
{
|
{
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"should" : [
|
||||||
"bool" : {
|
{"match" : { "name.first" : {"query" : "shay", "_name" : "first"} }},
|
||||||
"should" : [
|
{"match" : { "name.last" : {"query" : "banon", "_name" : "last"} }}
|
||||||
{"match" : { "name.first" : {"query" : "shay", "_name" : "first"} }},
|
],
|
||||||
{"match" : { "name.last" : {"query" : "banon", "_name" : "last"} }}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"filter" : {
|
"filter" : {
|
||||||
"terms" : {
|
"terms" : {
|
||||||
"name.last" : ["banon", "kimchy"],
|
"name.last" : ["banon", "kimchy"],
|
||||||
|
@ -26,32 +22,5 @@ Each filter and query can accept a `_name` in its top level definition.
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
The search response will include for each hit the `matched_queries` it matched on. The tagging of queries and filters
|
The search response will include for each hit the `matched_queries` it matched on. The tagging of queries and filters
|
||||||
only make sense for compound queries and filters (such as `bool` query and filter, `or` and `and` filter, `filtered` query etc.).
|
only make sense for the `bool` query.
|
||||||
|
|
||||||
Note, the query filter had to be enhanced in order to support this. In
|
|
||||||
order to set a name, the `fquery` filter should be used, which wraps a
|
|
||||||
query (just so there will be a place to set a name for it), for example:
|
|
||||||
|
|
||||||
[source,js]
|
|
||||||
--------------------------------------------------
|
|
||||||
{
|
|
||||||
"filtered" : {
|
|
||||||
"query" : {
|
|
||||||
"term" : { "name.first" : "shay" }
|
|
||||||
},
|
|
||||||
"filter" : {
|
|
||||||
"fquery" : {
|
|
||||||
"query" : {
|
|
||||||
"term" : { "name.last" : "banon" }
|
|
||||||
},
|
|
||||||
"_name" : "test"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--------------------------------------------------
|
|
||||||
|
|
||||||
==== Named queries
|
|
||||||
|
|
||||||
The support for the `_name` option on queries is available from version `0.90.4` and the support on filters is available
|
|
||||||
also in versions before `0.90.4`.
|
|
||||||
|
|
|
@ -2,28 +2,24 @@
|
||||||
=== Post filter
|
=== Post filter
|
||||||
|
|
||||||
The `post_filter` is applied to the search `hits` at the very end of a search
|
The `post_filter` is applied to the search `hits` at the very end of a search
|
||||||
request, after aggregations have already been calculated. It's purpose is
|
request, after aggregations have already been calculated. Its purpose is
|
||||||
best explained by example:
|
best explained by example:
|
||||||
|
|
||||||
Imagine that you are selling shirts, and the user has specified two filters:
|
Imagine that you are selling shirts, and the user has specified two filters:
|
||||||
`color:red` and `brand:gucci`. You only want to show them red shirts made by
|
`color:red` and `brand:gucci`. You only want to show them red shirts made by
|
||||||
Gucci in the search results. Normally you would do this with a
|
Gucci in the search results. Normally you would do this with a
|
||||||
<<query-dsl-filtered-query,`filtered` query>>:
|
<<query-dsl-bool-query,`bool` query>>:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XGET localhost:9200/shirts/_search -d '
|
curl -XGET localhost:9200/shirts/_search -d '
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered": {
|
"bool": {
|
||||||
"filter": {
|
"filter": [
|
||||||
"bool": {
|
{ "term": { "color": "red" }},
|
||||||
"must": [
|
{ "term": { "brand": "gucci" }}
|
||||||
{ "term": { "color": "red" }},
|
]
|
||||||
{ "term": { "brand": "gucci" }}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,15 +39,11 @@ This can be done with a
|
||||||
curl -XGET localhost:9200/shirts/_search -d '
|
curl -XGET localhost:9200/shirts/_search -d '
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered": {
|
"bool": {
|
||||||
"filter": {
|
"filter": [
|
||||||
"bool": {
|
{ "term": { "color": "red" }},
|
||||||
"must": [
|
{ "term": { "brand": "gucci" }}
|
||||||
{ "term": { "color": "red" }},
|
]
|
||||||
{ "term": { "brand": "gucci" }}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"aggs": {
|
"aggs": {
|
||||||
|
@ -78,7 +70,7 @@ the `post_filter`:
|
||||||
curl -XGET localhost:9200/shirts/_search -d '
|
curl -XGET localhost:9200/shirts/_search -d '
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered": {
|
"bool": {
|
||||||
"filter": {
|
"filter": {
|
||||||
{ "term": { "brand": "gucci" }} <1>
|
{ "term": { "brand": "gucci" }} <1>
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,8 +169,8 @@ We could write the query as:
|
||||||
------------------------------------------
|
------------------------------------------
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"filtered": {
|
"bool": {
|
||||||
"query": {
|
"must": {
|
||||||
"match": {
|
"match": {
|
||||||
"line": "{{text}}" <1>
|
"line": "{{text}}" <1>
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ via the REST API, should be written as a string:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------
|
--------------------
|
||||||
"inline": "{\"query\":{\"filtered\":{\"query\":{\"match\":{\"line\":\"{{text}}\"}},\"filter\":{{{#line_no}}\"range\":{\"line_no\":{{{#start}}\"gte\":\"{{start}}\"{{#end}},{{/end}}{{/start}}{{#end}}\"lte\":\"{{end}}\"{{/end}}}}{{/line_no}}}}}}"
|
"inline": "{\"query\":{\"bool\":{\"must\":{\"match\":{\"line\":\"{{text}}\"}},\"filter\":{{{#line_no}}\"range\":{\"line_no\":{{{#start}}\"gte\":\"{{start}}\"{{#end}},{{/end}}{{/start}}{{#end}}\"lte\":\"{{end}}\"{{/end}}}}{{/line_no}}}}}}"
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
==================================
|
==================================
|
||||||
|
|
|
@ -55,8 +55,8 @@ Or, with a request body:
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{
|
curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{
|
||||||
"query" : {
|
"query" : {
|
||||||
"filtered" : {
|
"bool" : {
|
||||||
"query" : {
|
"must" : {
|
||||||
"query_string" : {
|
"query_string" : {
|
||||||
"query" : "*:*"
|
"query" : "*:*"
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo&
|
||||||
"explanations" : [ {
|
"explanations" : [ {
|
||||||
"index" : "twitter",
|
"index" : "twitter",
|
||||||
"valid" : false,
|
"valid" : false,
|
||||||
"error" : "org.elasticsearch.index.query.QueryParsingException: [twitter] Failed to parse; org.elasticsearch.ElasticsearchParseException: failed to parse date field [foo], tried both date format [dateOptionalTime], and timestamp number; java.lang.IllegalArgumentException: Invalid format: \"foo\""
|
"error" : "[twitter] QueryParsingException[Failed to parse]; nested: IllegalArgumentException[Invalid format: \"foo\"];; java.lang.IllegalArgumentException: Invalid format: \"foo\""
|
||||||
} ]
|
} ]
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
@ -112,14 +112,14 @@ For Fuzzy Queries:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XGET 'http://localhost:9200/imdb/movies/_validate/query?rewrite=true'
|
curl -XGET 'http://localhost:9200/imdb/movies/_validate/query?rewrite=true' -d '
|
||||||
{
|
{
|
||||||
"query": {
|
"query": {
|
||||||
"fuzzy": {
|
"fuzzy": {
|
||||||
"actors": "kyle"
|
"actors": "kyle"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}'
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
Response:
|
Response:
|
||||||
|
@ -137,7 +137,7 @@ Response:
|
||||||
{
|
{
|
||||||
"index": "imdb",
|
"index": "imdb",
|
||||||
"valid": true,
|
"valid": true,
|
||||||
"explanation": "filtered(plot:kyle plot:kylie^0.75 plot:kyne^0.75 plot:lyle^0.75 plot:pyle^0.75)->cache(_type:movies)"
|
"explanation": "plot:kyle plot:kylie^0.75 plot:kyne^0.75 plot:lyle^0.75 plot:pyle^0.75 #_type:movies"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -175,7 +175,7 @@ Response:
|
||||||
{
|
{
|
||||||
"index": "imdb",
|
"index": "imdb",
|
||||||
"valid": true,
|
"valid": true,
|
||||||
"explanation": "filtered(((title:terminator^3.71334 plot:future^2.763601 plot:human^2.8415773 plot:sarah^3.4193945 plot:kyle^3.8244398 plot:cyborg^3.9177752 plot:connor^4.040236 plot:reese^4.7133346 ... )~6) -ConstantScore(_uid:movies#88247))->cache(_type:movies)"
|
"explanation": "((title:terminator^3.71334 plot:future^2.763601 plot:human^2.8415773 plot:sarah^3.4193945 plot:kyle^3.8244398 plot:cyborg^3.9177752 plot:connor^4.040236 plot:reese^4.7133346 ... )~6) -ConstantScore(_uid:movies#88247) #_type:movies"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue