-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chapter12_part3: /080_Structured_Search/10_compoundfilters.asciidoc #29
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
[[combining-filters]] | ||
=== Combining Filters | ||
=== 组合过滤器 | ||
|
||
The previous two examples showed a single filter in use.((("structured search", "combining filters")))((("filters", "combining"))) In practice, you | ||
will probably need to filter on multiple values or fields. For example, how | ||
would you express this SQL in Elasticsearch? | ||
前面的两个例子都是单个过滤器(filter)的使用方式。((("structured search", "combining filters")))((("filters", "combining"))) 在实际应用中,我们 | ||
很有可能会过滤多个值或字段。比方说,怎样 | ||
用 Elasticsearch 来表达下面的 SQL ? | ||
|
||
[source,sql] | ||
-------------------------------------------------- | ||
|
@@ -13,14 +13,14 @@ WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3") | |
AND (price != 30) | ||
-------------------------------------------------- | ||
|
||
In these situations, you will need the `bool` filter.((("filters", "combining", "in bool filter")))((("bool filter"))) This is a _compound | ||
filter_ that accepts other filters as arguments, combining them in various | ||
Boolean combinations. | ||
这种情况下,我们需要 `bool` (布尔)过滤器。((("filters", "combining", "in bool filter")))((("bool filter"))) 这是个 _复合过滤器(compound filter)_ , | ||
它可以接受多个其他过滤器作为参数,并将这些过滤器结合构成各式各样的 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 结合构成 -> 结合成 这样是否跟通顺? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @looly 对这样更好,谢谢:0 |
||
布尔(逻辑)组合。 | ||
|
||
[[bool-filter]] | ||
==== Bool Filter | ||
==== 布尔过滤器 | ||
|
||
The `bool` filter is composed of three sections: | ||
一个 `bool` 过滤器由三部分组成: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 此处应该用中文标点 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是 |
||
|
||
[source,js] | ||
-------------------------------------------------- | ||
|
@@ -33,28 +33,27 @@ The `bool` filter is composed of three sections: | |
} | ||
-------------------------------------------------- | ||
|
||
`must`:: | ||
All of these clauses _must_ match. The equivalent of `AND`. | ||
|
||
`must_not`:: | ||
All of these clauses _must not_ match. The equivalent of `NOT`. | ||
|
||
`should`:: | ||
At least one of these clauses must match. The equivalent of `OR`. | ||
`must`:: | ||
所有的语句都 _必须(must)_ 匹配,与 `AND` 等价。 | ||
|
||
And that's it!((("should clause", "in bool filters")))((("must_not clause", "in bool filters")))((("must clause", "in bool filters"))) When you need multiple filters, simply place them into the | ||
different sections of the `bool` filter. | ||
`must_not`:: | ||
所有的语句都 _不能(must not)_ 匹配,与 `NOT` 等价。 | ||
|
||
`should`:: | ||
至少有一个语句要匹配,与 `OR` 等价。 | ||
|
||
就这么简单!((("should clause", "in bool filters")))((("must_not clause", "in bool filters")))((("must clause", "in bool filters"))) 当我们需要多个过滤器时,只须将它们置入 | ||
`bool` 过滤器的不同部分即可。 | ||
|
||
[NOTE] | ||
==== | ||
Each section of the `bool` filter is optional (for example, you can have a `must` | ||
clause and nothing else), and each section can contain a single filter or an | ||
array of filters. | ||
一个 `bool` 过滤器的每个部分都是可选的(例如,我们可以只有一个 `must` 语句), | ||
而且每个部分内部可以只有一个或一组过滤器。 | ||
==== | ||
|
||
To replicate the preceding SQL example, we will take the two `term` filters that | ||
we used((("term filter", "placing inside bool filter")))((("bool filter", "with two term filters in should clause and must_not clause"))) previously and place them inside the `should` clause of a `bool` | ||
filter, and add another clause to deal with the `NOT` condition: | ||
用 Elasticsearch 来表示本部分开始处的 SQL 例子,将两个 `term` 过滤器 | ||
置入 `bool` 过滤器的 `should` 语句 | ||
内,再增加一个语句处理 `NOT` (非)的条件: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 括号里的非是否多余? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 略显多余,已经去掉:0 |
||
|
||
[source,js] | ||
-------------------------------------------------- | ||
|
@@ -79,14 +78,14 @@ GET /my_store/products/_search | |
-------------------------------------------------- | ||
// SENSE: 080_Structured_Search/10_Bool_filter.json | ||
|
||
<1> Note that we still need to use a `filtered` query to wrap everything. | ||
<2> These two `term` filters are _children_ of the `bool` filter, and since they | ||
are placed inside the `should` clause, at least one of them needs to match. | ||
<3> If a product has a price of `30`, it is automatically excluded because it | ||
matches a `must_not` clause. | ||
<1> 注意,我们仍然需要一个 `filtered` 查询将所有的东西包起来。 | ||
<2> 在 `should` 语句块里面的两个 `term` 过滤器 | ||
与 `bool` 过滤器是父子关系,两个 `term` 条件需要匹配其一。 | ||
<3> 如果一个产品的价格是 `30` ,那么它会自动被排除,因为它 | ||
处于 `must_not` 语句里面。 | ||
|
||
Our search results return two hits, each document satisfying a different clause | ||
in the `bool` filter: | ||
我们搜索的结果返回了 2 个命中结果,两个文档分别匹配了 `bool` 过滤器 | ||
其中的一个条件: | ||
|
||
[source,json] | ||
-------------------------------------------------- | ||
|
@@ -109,17 +108,17 @@ in the `bool` filter: | |
} | ||
] | ||
-------------------------------------------------- | ||
<1> Matches the `term` filter for `productID = "XHDK-A-1293-#fJ3"` | ||
<2> Matches the `term` filter for `price = 20` | ||
<1> 与 `term` 过滤器中 `productID = "XHDK-A-1293-#fJ3"` 条件匹配 | ||
<2> 与 `term` 过滤器中 `price = 20` 条件匹配 | ||
|
||
==== Nesting Boolean Filters | ||
==== 嵌套布尔过滤器 | ||
|
||
Even though `bool` is a compound filter and accepts children filters, it is | ||
important to understand that `bool` is just a filter itself.((("filters", "combining", "nesting bool filters")))((("bool filter", "nesting in another bool filter"))) This means you | ||
can nest `bool` filters inside other `bool` filters, giving you the | ||
ability to make arbitrarily complex Boolean logic. | ||
尽管 `bool` 是一个复合的过滤器,可以接受多个子过滤器,需要 | ||
注意的是 `bool` 过滤器本身仍然还只是一个过滤器。((("filters", "combining", "nesting bool filters")))((("bool filter", "nesting in another bool filter"))) 这意味着我们 | ||
可以将一个 `bool` 过滤器置于其他 `bool` 过滤器内部,这为我们 | ||
提供了对任意复杂布尔逻辑进行处理的能力。 | ||
|
||
Given this SQL statement: | ||
对于以下这个 SQL 语句: | ||
|
||
[source,sql] | ||
-------------------------------------------------- | ||
|
@@ -130,7 +129,7 @@ WHERE productID = "KDKE-B-9947-#kL5" | |
AND price = 30 ) | ||
-------------------------------------------------- | ||
|
||
We can translate it into a pair of nested `bool` filters: | ||
我们将其转换成一组嵌套的 `bool` 过滤器: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
|
@@ -157,14 +156,12 @@ GET /my_store/products/_search | |
-------------------------------------------------- | ||
// SENSE: 080_Structured_Search/10_Bool_filter.json | ||
|
||
<1> Because the `term` and the `bool` are sibling clauses inside the first | ||
Boolean `should`, at least one of these filters must match for a document | ||
to be a hit. | ||
|
||
<2> These two `term` clauses are siblings in a `must` clause, so they both | ||
have to match for a document to be returned as a hit. | ||
<1> 因为 `term` 和 `bool` 过滤器是兄弟关系,他们都处于外层的 | ||
布尔逻辑 `should` 的内部,返回的命中文档至少须匹配其中一个过滤器的条件。 | ||
<2> 这两个 `term` 语句作为兄弟关系,同时处于 `must` 语句之中,所以 | ||
返回的命中文档要必须都能同时匹配这两个条件。 | ||
|
||
The results show us two documents, one matching each of the `should` clauses: | ||
得到的结果有两个文档,它们各匹配 `should` 语句中的一个条件: | ||
|
||
[source,json] | ||
-------------------------------------------------- | ||
|
@@ -187,8 +184,8 @@ The results show us two documents, one matching each of the `should` clauses: | |
} | ||
] | ||
-------------------------------------------------- | ||
<1> This `productID` matches the `term` in the first `bool`. | ||
<2> These two fields match the `term` filters in the nested `bool`. | ||
<1> 这个 `productID` 与外层的 `bool` 过滤器 `should` 里的唯一一个 `term` 匹配。 | ||
<2> 这两个字段与嵌套的 `bool` 过滤器 `must` 里的两个 `term` 匹配。 | ||
|
||
This was a simple example, but it demonstrates how Boolean filters can be | ||
used as building blocks to construct complex logical conditions. | ||
这只是个简单的例子,但足以展示布尔过滤器可以 | ||
用来作为构造复杂逻辑条件的基本构建模块。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“怎样”和“用”之间这个地方换行没有必要吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,不应该手工换行的, medcl 也给我指正了