|
1 |
| -[[match-multi-word]] |
2 |
| -=== Multiword Queries |
3 | 1 | ##多词查询
|
4 | 2 |
|
5 | 3 | 如果一次只能查询一个关键词,全文检索将会很不方便。幸运的是,用``match``查询进行多词查询也很简单:
|
|
48 | 46 | ]
|
49 | 47 | }
|
50 | 48 |
|
51 |
| - <1> 文档4的相关度最高,因为包含两个``"brown"``和一个``"dog"``。 |
| 49 | + <1> 文档4的相关度最高,因为包含两个"brown"和一个"dog"。 |
52 | 50 |
|
53 |
| - <2> 文档2和3都包含一个``"brown"``和一个``"dog"``,且``'title'``字段长度相同,所以相关度相等。 |
| 51 | + <2> 文档2和3都包含一个"brown"和一个"dog",且'title'字段长度相同,所以相关度相等。 |
54 | 52 |
|
55 |
| - <3> 文档1只包含一个``"brown"``,不包含``"dog"``,所以相关度最低。 |
| 53 | + <3> 文档1只包含一个"brown",不包含"dog",所以相关度最低。 |
56 | 54 |
|
57 | 55 | 因为``match``查询需要查询两个关键词:``"brown"``和``"dog"``,在内部会执行两个``term``查询并综合二者的结果得到最终的结果。``match``的实现方式是将两个``term``查询放入一个``bool``查询,``bool``查询在之前的章节已经介绍过。
|
58 | 56 |
|
59 | 57 | 重要的一点是,``'title'``字段包含_至少一个_查询关键字的文档都被认为是符合查询条件的。匹配的单词数越多,文档的相关度越高。
|
60 | 58 |
|
61 |
| -[[match-improving-precision]] |
62 |
| -==== Improving Precision |
63 |
| - |
64 | 59 | ### 提高精度
|
65 | 60 |
|
66 | 61 | 匹配包含任意个数查询关键字的文档可能会得到一些看似不相关的结果,这是一种霰弹策略(shotgun approach)。然而我们可能想得到包含_所有_查询关键字的文档。换句话说,我们想得到的是匹配``'brown AND dog'``的文档,而非``'brown OR dog'``。
|
|
79 | 74 | }
|
80 | 75 | }
|
81 | 76 |
|
82 |
| - <1> The structure of the ``match`` query has to change slightly in order to |
83 |
| - accommodate the ``'operator'`` parameter. |
84 |
| - |
| 77 | + <1> 为了加入``'operator'``参数,``match``查询的结构有一些不同。 |
| 78 | + |
85 | 79 | 这个查询会排除文档1,因为文档1只包含了一个查询关键词。
|
86 | 80 |
|
87 |
| -[[match-precision]] |
88 |
| -==== Controlling Precision |
89 |
| - |
90 | 81 | ### 控制精度
|
91 | 82 |
|
92 | 83 | 在 _all_ 和 _any_ 之间的选择有点过于非黑即白。如果用户指定了5个查询关键字,而一个文档只包含了其中的4个?将``'operator'``设置为``'and'``会排除这个文档。
|
93 | 84 |
|
94 | 85 | 有时这的确是用户想要的结果。但在大多数全文检索的使用场景下,用户想得到相关的文档,排除那些不太可能相关的文档。换句话说,我们需要介于二者之间的选项。
|
95 | 86 |
|
96 |
| -The `match` query supports((("match query", "minimum_should_match parameter")))((("minimum_should_match parameter"))) the `minimum_should_match` parameter, which allows |
97 |
| -you to specify the number of terms that must match for a document to be considered |
98 |
| -relevant. While you can specify an absolute number of terms, it usually makes |
99 |
| -sense to specify a percentage instead, as you have no control over the number of words the user may enter: |
100 |
| - |
101 |
| -[source,js] |
102 |
| --------------------------------------------------- |
103 |
| -GET /my_index/my_type/_search |
104 |
| -{ |
105 |
| - "query": { |
106 |
| - "match": { |
107 |
| - "title": { |
108 |
| - "query": "quick brown dog", |
109 |
| - "minimum_should_match": "75%" |
110 |
| - } |
111 |
| - } |
112 |
| - } |
113 |
| -} |
114 |
| --------------------------------------------------- |
115 |
| -// SENSE: 100_Full_Text_Search/05_Match_query.json |
116 |
| - |
117 |
| -When specified as a percentage, `minimum_should_match` does the right thing: |
118 |
| -in the preceding example with three terms, `75%` would be rounded down to `66.6%`, |
119 |
| -or two out of the three terms. No matter what you set it to, at least one term |
120 |
| -must match for a document to be considered a match. |
121 |
| - |
122 |
| -[NOTE] |
123 |
| -==== |
124 |
| -The `minimum_should_match` parameter is flexible, and different rules can |
125 |
| -be applied depending on the number of terms the user enters. For the full |
126 |
| -documentation see the |
127 |
| -http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html#query-dsl-minimum-should-match |
128 |
| -==== |
129 |
| - |
130 |
| -To fully understand how the `match` query handles multiword queries, we need |
131 |
| -to look at how to combine multiple queries with the `bool` query. |
| 87 | +``match``查询有``'minimum_should_match'``参数,参数值表示被视为_相关_的文档必须匹配的关键词个数。参数值可以设为整数,也可以设置为百分数。因为不能提前确定用户输入的查询关键词个数,使用百分数也很合理。 |
| 88 | + |
| 89 | + GET /my_index/my_type/_search |
| 90 | + { |
| 91 | + "query": { |
| 92 | + "match": { |
| 93 | + "title": { |
| 94 | + "query": "quick brown dog", |
| 95 | + "minimum_should_match": "75%" |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +当``'minimum_should_match'``被设置为百分数时,查询进行如下:在上面的例子里,``'75%'``会被下舍为``'66.6%'``,也就是2个关键词。不论参数值为多少,进入结果集的文档至少应匹配一个关键词。 |
| 102 | + |
| 103 | +####[提示] |
| 104 | + |
| 105 | +``'minimum_should_match'``参数很灵活,根据用户输入的关键词个数,可以采用不同的匹配规则。更详细的内容可以查看[文档](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html#query-dsl-minimum-should-match)。 |
| 106 | + |
| 107 | +要全面理解``match``查询是怎样处理多词查询,我们需要了解怎样用``bool``查询合并多个查询。 |
0 commit comments