Skip to content

Commit 3764c8d

Browse files
committed
Merge branch 'cn' of github.com:elasticsearch-cn/elasticsearch-definitive-guide into cn
2 parents 676b7af + 6dd6fc5 commit 3764c8d

File tree

5 files changed

+99
-202
lines changed

5 files changed

+99
-202
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+
这只是个简单的例子,但足以展示布尔过滤器可以用来作为构造复杂逻辑条件的基本构建模块。

100_Full_Text_Search/10_Multi_word_queries.asciidoc

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[[match-multi-word]]
2-
=== Multiword Queries
2+
=== 多词查询
33

4-
If we could search for only one word at a time, full-text search would be
5-
pretty inflexible. Fortunately, the `match` query((("full text search", "multi-word queries")))((("match query", "multi-word query"))) makes multiword queries
6-
just as simple:
4+
如果我们一次只能搜索一个词,那么全文搜索就会不太灵活,幸运的是 `match` 查询让多词查询变得简单:((("full text search", "multi-word queries")))((("match query", "multi-word query")))
75

86
[source,js]
97
--------------------------------------------------
@@ -18,7 +16,7 @@ GET /my_index/my_type/_search
1816
--------------------------------------------------
1917
// SENSE: 100_Full_Text_Search/05_Match_query.json
2018

21-
The preceding query returns all four documents in the results list:
19+
上面这个查询返回所有四个文档:
2220

2321
[source,js]
2422
--------------------------------------------------
@@ -56,33 +54,22 @@ The preceding query returns all four documents in the results list:
5654
}
5755
--------------------------------------------------
5856

59-
<1> Document 4 is the most relevant because it contains `"brown"` twice and `"dog"`
60-
once.
57+
<1> 文档 4 最相关,因为它包含词 `"brown"` 两次以及 `"dog"` 一次。
6158

62-
<2> Documents 2 and 3 both contain `brown` and `dog` once each, and the `title`
63-
field is the same length in both docs, so they have the same score.
59+
<2> 文档 2、3 同时包含 `brown` 和 `dog` 各一次,而且它们 `title` 字段的长度相同,所以具有相同的评分。
6460

65-
<3> Document 1 matches even though it contains only `brown`, not `dog`.
61+
<3> 文档 1 也能匹配,尽管它只有 `brown` 没有 `dog`
6662

67-
Because the `match` query has to look for two terms&#x2014;`["brown","dog"]`&#x2014;internally it has to execute two `term` queries and combine their individual
68-
results into the overall result. To do this, it wraps the two `term` queries
69-
in a `bool` query, which we examine in detail in <<bool-query>>.
63+
因为 `match` 查询必须查找两个词( `["brown","dog"]` ),它在内部实际上先执行两次 `term` 查询,然后将两次查询的结果合并作为最终结果输出。为了做到这点,它将两个 `term` 查询包入一个 `bool` 查询中,详细信息见 <<bool-query, 布尔查询>>。
7064

71-
The important thing to take away from this is that any document whose
72-
`title` field contains _at least one of the specified terms_ will match the
73-
query. The more terms that match, the more relevant the document.
65+
以上示例告诉我们一个重要信息:即任何文档只要 `title` 字段里包含 _指定词项中的至少一个词_ 就能匹配,被匹配的词项越多,文档就越相关。
7466

7567
[[match-improving-precision]]
76-
==== Improving Precision
68+
==== 提高精度
7769

78-
Matching any document that contains _any_ of the query terms may result in a
79-
long tail of seemingly irrelevant results. ((("full text search", "multi-word queries", "improving precision")))((("precision", "improving for full text search multi-word queries"))) It's a shotgun approach to search.
80-
Perhaps we want to show only documents that contain _all_ of the query terms.
81-
In other words, instead of `brown OR dog`, we want to return only documents
82-
that match `brown AND dog`.
70+
用 _任意_ 查询词项匹配文档可能会导致结果中出现不相关的长尾。((("full text search", "multi-word queries", "improving precision")))((("precision", "improving for full text search multi-word queries")))这是种散弹式搜索。可能我们只想搜索包含 _所有_ 词项的文档,也就是说,不去匹配 `brown OR dog` ,而通过匹配 `brown AND dog` 找到所有文档。
8371

84-
The `match` query accepts an `operator` parameter((("match query", "operator parameter")))((("or operator", "in match queries")))((("and operator", "in match queries"))) that defaults to `or`.
85-
You can change it to `and` to require that all specified terms must match:
72+
`match` 查询还可以接受 `operator` 操作符作为输入参数,默认情况下该操作符是 `or` 。我们可以将它修改成 `and` 让所有指定词项都必须匹配:
8673

8774
[source,js]
8875
--------------------------------------------------
@@ -100,27 +87,18 @@ GET /my_index/my_type/_search
10087
--------------------------------------------------
10188
// SENSE: 100_Full_Text_Search/05_Match_query.json
10289

103-
<1> The structure of the `match` query has to change slightly in order to
104-
accommodate the `operator` parameter.
90+
<1> `match` 查询的结构需要做稍许调整才能使用 `operator` 操作符参数。
10591

106-
This query would exclude document 1, which contains only one of the two terms.
92+
这个查询可以把文档 1 排除在外,因为它只包含两个词项中的一个。
10793

10894
[[match-precision]]
109-
==== Controlling Precision
95+
==== 控制精度
11096

111-
The choice between _all_ and _any_ is a bit((("full text search", "multi-word queries", "controlling precision"))) too black-or-white. What if the
112-
user specified five query terms, and a document contains only four of them?
113-
Setting `operator` to `and` would exclude this document.
97+
在 _所有_ 与 _任意_ 间二选一有点过于非黑即白。((("full text search", "multi-word queries", "controlling precision")))如果用户给定 5 个查询词项,想查找只包含其中 4 个的文档,该如何处理?将 `operator` 操作符参数设置成 `and` 只会将此文档排除。
11498

115-
Sometimes that is exactly what you want, but for most full-text search use
116-
cases, you want to include documents that may be relevant but exclude those
117-
that are unlikely to be relevant. In other words, we need something
118-
in-between.
99+
有时候这正是我们期望的,但在全文搜索的大多数应用场景下,我们既想包含那些可能相关的文档,同时又排除那些不太相关的。换句话说,我们想要处于中间某种结果。
119100

120-
The `match` query supports((("match query", "minimum_should_match parameter")))((("minimum_should_match parameter"))) the `minimum_should_match` parameter, which allows
121-
you to specify the number of terms that must match for a document to be considered
122-
relevant. While you can specify an absolute number of terms, it usually makes
123-
sense to specify a percentage instead, as you have no control over the number of words the user may enter:
101+
`match` 查询支持 `minimum_should_match` 最小匹配参数,((("match query", "minimum_should_match parameter")))((("minimum_should_match parameter")))这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数,因为我们无法控制用户搜索时输入的单词数量:
124102

125103
[source,js]
126104
--------------------------------------------------
@@ -138,18 +116,12 @@ GET /my_index/my_type/_search
138116
--------------------------------------------------
139117
// SENSE: 100_Full_Text_Search/05_Match_query.json
140118

141-
When specified as a percentage, `minimum_should_match` does the right thing:
142-
in the preceding example with three terms, `75%` would be rounded down to `66.6%`,
143-
or two out of the three terms. No matter what you set it to, at least one term
144-
must match for a document to be considered a match.
119+
当给定百分比的时候, `minimum_should_match` 会做合适的事情:在之前三词项的示例中, `75%` 会自动被截断成 `66.6%` ,即三个里面两个词。无论这个值设置成什么,至少包含一个词项的文档才会被认为是匹配的。
145120

146121
[NOTE]
147122
====
148-
The `minimum_should_match` parameter is flexible, and different rules can
149-
be applied depending on the number of terms the user enters. For the full
150-
documentation see the
123+
参数 `minimum_should_match` 的设置非常灵活,可以根据用户输入词项的数目应用不同的规则。完整的信息参考文档
151124
{ref}/query-dsl-minimum-should-match.html#query-dsl-minimum-should-match
152125
====
153126

154-
To fully understand how the `match` query handles multiword queries, we need
155-
to look at how to combine multiple queries with the `bool` query.
127+
为了完全理解 `match` 是如何处理多词查询的,我们就需要查看如何使用 `bool` 查询将多个查询条件组合在一起。

0 commit comments

Comments
 (0)