Skip to content

Commit cbba8f8

Browse files
committed
Merge pull request #29 from richardwei2008/p2_chapter12/10_compoundfilters
chapter12_part3: /080_Structured_Search/10_compoundfilters.asciidoc
2 parents d9a2851 + 10cb12f commit cbba8f8

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

080_Structured_Search/10_compoundfilters.asciidoc

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[[combining-filters]]
2-
=== Combining Filters
2+
=== 组合过滤器
33

4-
The previous two examples showed a single filter in use.((("structured search", "combining filters")))((("filters", "combining"))) In practice, you
5-
will probably need to filter on multiple values or fields. For example, how
6-
would you express this SQL in Elasticsearch?
4+
前面的两个例子都是单个过滤器(filter)的使用方式。((("structured search", "combining filters")))((("filters", "combining"))) 在实际应用中,我们很有可能会过滤多个值或字段。比方说,怎样用 Elasticsearch 来表达下面的 SQL ?
75

86
[source,sql]
97
--------------------------------------------------
@@ -13,14 +11,12 @@ WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
1311
AND (price != 30)
1412
--------------------------------------------------
1513

16-
In these situations, you will need the `bool` filter.((("filters", "combining", "in bool filter")))((("bool filter"))) This is a _compound
17-
filter_ that accepts other filters as arguments, combining them in various
18-
Boolean combinations.
14+
这种情况下,我们需要 `bool` (布尔)过滤器。((("filters", "combining", "in bool filter")))((("bool filter"))) 这是个 _复合过滤器(compound filter)_ ,它可以接受多个其他过滤器作为参数,并将这些过滤器结合成各式各样的布尔(逻辑)组合。
1915

2016
[[bool-filter]]
21-
==== Bool Filter
17+
==== 布尔过滤器
2218

23-
The `bool` filter is composed of three sections:
19+
一个 `bool` 过滤器由三部分组成:
2420

2521
[source,js]
2622
--------------------------------------------------
@@ -33,28 +29,23 @@ The `bool` filter is composed of three sections:
3329
}
3430
--------------------------------------------------
3531

36-
`must`::
37-
All of these clauses _must_ match. The equivalent of `AND`.
38-
39-
`must_not`::
40-
All of these clauses _must not_ match. The equivalent of `NOT`.
41-
42-
`should`::
43-
At least one of these clauses must match. The equivalent of `OR`.
32+
`must`::
33+
所有的语句都 _必须(must)_ 匹配,与 `AND` 等价。
4434

45-
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
46-
different sections of the `bool` filter.
35+
`must_not`::
36+
所有的语句都 _不能(must not)_ 匹配,与 `NOT` 等价。
37+
38+
`should`::
39+
至少有一个语句要匹配,与 `OR` 等价。
40+
41+
就这么简单!((("should clause", "in bool filters")))((("must_not clause", "in bool filters")))((("must clause", "in bool filters"))) 当我们需要多个过滤器时,只须将它们置入 `bool` 过滤器的不同部分即可。
4742

4843
[NOTE]
4944
====
50-
Each section of the `bool` filter is optional (for example, you can have a `must`
51-
clause and nothing else), and each section can contain a single filter or an
52-
array of filters.
45+
一个 `bool` 过滤器的每个部分都是可选的(例如,我们可以只有一个 `must` 语句),而且每个部分内部可以只有一个或一组过滤器。
5346
====
5447

55-
To replicate the preceding SQL example, we will take the two `term` filters that
56-
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`
57-
filter, and add another clause to deal with the `NOT` condition:
48+
用 Elasticsearch 来表示本部分开始处的 SQL 例子,将两个 `term` 过滤器置入 `bool` 过滤器的 `should` 语句内,再增加一个语句处理 `NOT` 非的条件:
5849

5950
[source,js]
6051
--------------------------------------------------
@@ -79,14 +70,11 @@ GET /my_store/products/_search
7970
--------------------------------------------------
8071
// SENSE: 080_Structured_Search/10_Bool_filter.json
8172

82-
<1> Note that we still need to use a `filtered` query to wrap everything.
83-
<2> These two `term` filters are _children_ of the `bool` filter, and since they
84-
are placed inside the `should` clause, at least one of them needs to match.
85-
<3> If a product has a price of `30`, it is automatically excluded because it
86-
matches a `must_not` clause.
73+
<1> 注意,我们仍然需要一个 `filtered` 查询将所有的东西包起来。
74+
<2> 在 `should` 语句块里面的两个 `term` 过滤器与 `bool` 过滤器是父子关系,两个 `term` 条件需要匹配其一。
75+
<3> 如果一个产品的价格是 `30` ,那么它会自动被排除,因为它处于 `must_not` 语句里面。
8776

88-
Our search results return two hits, each document satisfying a different clause
89-
in the `bool` filter:
77+
我们搜索的结果返回了 2 个命中结果,两个文档分别匹配了 `bool` 过滤器其中的一个条件:
9078

9179
[source,json]
9280
--------------------------------------------------
@@ -109,17 +97,14 @@ in the `bool` filter:
10997
}
11098
]
11199
--------------------------------------------------
112-
<1> Matches the `term` filter for `productID = "XHDK-A-1293-#fJ3"`
113-
<2> Matches the `term` filter for `price = 20`
100+
<1> `term` 过滤器中 `productID = "XHDK-A-1293-#fJ3"` 条件匹配
101+
<2> `term` 过滤器中 `price = 20` 条件匹配
114102

115-
==== Nesting Boolean Filters
103+
==== 嵌套布尔过滤器
116104

117-
Even though `bool` is a compound filter and accepts children filters, it is
118-
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
119-
can nest `bool` filters inside other `bool` filters, giving you the
120-
ability to make arbitrarily complex Boolean logic.
105+
尽管 `bool` 是一个复合的过滤器,可以接受多个子过滤器,需要注意的是 `bool` 过滤器本身仍然还只是一个过滤器。((("filters", "combining", "nesting bool filters")))((("bool filter", "nesting in another bool filter"))) 这意味着我们可以将一个 `bool` 过滤器置于其他 `bool` 过滤器内部,这为我们提供了对任意复杂布尔逻辑进行处理的能力。
121106

122-
Given this SQL statement:
107+
对于以下这个 SQL 语句:
123108

124109
[source,sql]
125110
--------------------------------------------------
@@ -130,7 +115,7 @@ WHERE productID = "KDKE-B-9947-#kL5"
130115
AND price = 30 )
131116
--------------------------------------------------
132117

133-
We can translate it into a pair of nested `bool` filters:
118+
我们将其转换成一组嵌套的 `bool` 过滤器:
134119

135120
[source,js]
136121
--------------------------------------------------
@@ -157,14 +142,10 @@ GET /my_store/products/_search
157142
--------------------------------------------------
158143
// SENSE: 080_Structured_Search/10_Bool_filter.json
159144

160-
<1> Because the `term` and the `bool` are sibling clauses inside the first
161-
Boolean `should`, at least one of these filters must match for a document
162-
to be a hit.
163-
164-
<2> These two `term` clauses are siblings in a `must` clause, so they both
165-
have to match for a document to be returned as a hit.
145+
<1> 因为 `term` 和 `bool` 过滤器是兄弟关系,他们都处于外层的布尔逻辑 `should` 的内部,返回的命中文档至少须匹配其中一个过滤器的条件。
146+
<2> 这两个 `term` 语句作为兄弟关系,同时处于 `must` 语句之中,所以返回的命中文档要必须都能同时匹配这两个条件。
166147

167-
The results show us two documents, one matching each of the `should` clauses:
148+
得到的结果有两个文档,它们各匹配 `should` 语句中的一个条件:
168149

169150
[source,json]
170151
--------------------------------------------------
@@ -187,8 +168,7 @@ The results show us two documents, one matching each of the `should` clauses:
187168
}
188169
]
189170
--------------------------------------------------
190-
<1> This `productID` matches the `term` in the first `bool`.
191-
<2> These two fields match the `term` filters in the nested `bool`.
171+
<1> 这个 `productID` 与外层的 `bool` 过滤器 `should` 里的唯一一个 `term` 匹配。
172+
<2> 这两个字段与嵌套的 `bool` 过滤器 `must` 里的两个 `term` 匹配。
192173

193-
This was a simple example, but it demonstrates how Boolean filters can be
194-
used as building blocks to construct complex logical conditions.
174+
这只是个简单的例子,但足以展示布尔过滤器可以用来作为构造复杂逻辑条件的基本构建模块。

0 commit comments

Comments
 (0)