1- === Retrieving a Document
1+ [[_retrieving_a_document]]
2+ === 检索文档
23
3- Now that we have some data stored in Elasticsearch,((("documents", "retrieving"))) we can get to work on the
4- business requirements for this application. The first requirement is the
5- ability to retrieve individual employee data.
4+ 目前我们已经在 Elasticsearch 中存储了一些数据,((("documents", "retrieving"))) 接下来就能专注于实现应用的业务需求了。第一个需求是可以检索到单个雇员的数据。
65
7- This is easy in Elasticsearch. We simply execute((("HTTP requests", "retrieving a document with GET"))) an HTTP +GET+ request and
8- specify the _address_ of the document-- the index, type, and ID.((("id", "specifying in a request")))((("indices", "specifying index in a request")))((("types", "specifying type in a request"))) Using
9- those three pieces of information, we can return the original JSON document:
6+ 这在 Elasticsearch 中很简单。简单地执行((("HTTP requests", "retrieving a document with GET"))) 一个 HTTP +GET+ 请求并指定文档的地址——索引库、类型和ID。((("id", "specifying in a request")))((("indices", "specifying index in a request")))((("types", "specifying type in a request"))) 使用这三个信息可以返回原始的 JSON 文档:
107
118[source,js]
129--------------------------------------------------
1310GET /megacorp/employee/1
1411--------------------------------------------------
1512// SENSE: 010_Intro/30_Get.json
1613
17- And the response contains some metadata about the document, and John Smith's
18- original JSON document ((("_source field", sortas="source field"))) as the `_source` field:
14+ 返回结果包含了文档的一些元数据,以及 `_source` 属性,内容是 John Smith 雇员的原始 JSON 文档((("_source field", sortas="source field"))):
1915
2016[source,js]
2117--------------------------------------------------
@@ -37,30 +33,22 @@ original JSON document ((("_source field", sortas="source field")))as the `_sour
3733
3834[TIP]
3935====
40- In the same way that we changed ((("HTTP methods")))the HTTP verb from `PUT` to `GET` in order to
41- retrieve the document, we could use the `DELETE` verb to delete the document,
42- and the `HEAD` verb to check whether the document exists. To replace an
43- existing document with an updated version, we just `PUT` it again.
36+ 将 HTTP 命令由 `PUT` 改为 `GET` 可以用来检索文档,同样的,可以使用 `DELETE` 命令来删除文档,以及使用 `HEAD` 指令来检查文档是否存在。如果想更新已存在的文档,只需再次 `PUT` 。
4437====
4538
46- === Search Lite
39+ === 轻量搜索
4740
48- A `GET` is fairly simple--you get back the document that you ask for.((("GET method")))((("searches", "simple search"))) Let's
49- try something a little more advanced, like a simple search!
41+ 一个 `GET` 是相当简单的,可以直接得到指定的文档。((("GET method")))((("searches", "simple search"))) 现在尝试点儿稍微高级的功能,比如一个简单的搜索!
5042
51- The first search we will try is the simplest search possible. We will search
52- for all employees, with this request:
43+ 第一个尝试的几乎是最简单的搜索了。我们使用下列请求来搜索所有雇员:
5344
5445[source,js]
5546--------------------------------------------------
5647GET /megacorp/employee/_search
5748--------------------------------------------------
5849// SENSE: 010_Intro/30_Simple_search.json
5950
60- You can see that we're still using index `megacorp` and type `employee`, but
61- instead of specifying a document ID, we now use the `_search` endpoint. The
62- response includes all three of our documents in the `hits` array. By default,
63- a search will return the top 10 results.
51+ 可以看到,我们仍然使用索引库 `megacorp` 以及类型 `employee`,但与指定一个文档 ID 不同,这次使用 `_search` 。返回结果包括了所有三个文档,放在数组 `hits` 中。一个搜索默认返回十条结果。
6452
6553[source,js]
6654--------------------------------------------------
@@ -116,23 +104,17 @@ a search will return the top 10 results.
116104}
117105--------------------------------------------------
118106
119- NOTE: The response not only tells us which documents matched, but also
120- includes the whole document itself: all the information that we need in order to
121- display the search results to the user.
107+ 注意:返回结果不仅告知匹配了哪些文档,还包含了整个文档本身:显示搜索结果给最终用户所需的全部信息。
122108
123- Next, let's try searching for employees who have ``Smith'' in their last name.
124- To do this, we'll use a _lightweight_ search method that is easy to use
125- from the command line. This method is often referred to as ((("query strings")))a _query-string_
126- search, since we pass the search as a URL query-string parameter:
109+ 接下来,尝试下搜索姓氏为 ``Smith`` 的雇员。为此,我们将使用一个 _高亮_ 搜索,很容易通过命令行完成。这个方法一般涉及到一个((("query strings"))) _查询字符串_ (_query-string_) 搜索,因为我们通过一个URL参数来传递查询信息给搜索接口:
127110
128111[source,js]
129112--------------------------------------------------
130113GET /megacorp/employee/_search?q=last_name:Smith
131114--------------------------------------------------
132115// SENSE: 010_Intro/30_Simple_search.json
133116
134- We use the same `_search` endpoint in the path, and we add the query itself in
135- the `q=` parameter. The results that come back show all Smiths:
117+ 我们仍然在请求路径中使用 `_search` 端点,并将查询本身赋值给参数 `q=` 。返回结果给出了所有的 Smith:
136118
137119[source,js]
138120--------------------------------------------------
@@ -167,15 +149,11 @@ the `q=` parameter. The results that come back show all Smiths:
167149}
168150--------------------------------------------------
169151
170- === Search with Query DSL
152+ === 使用查询表达式(query DSL)搜索
171153
172- Query-string search is handy for ad hoc searches((("ad hoc searches"))) from the command line, but
173- it has its limitations (see <<search-lite>>). Elasticsearch provides a rich,
174- flexible, query language called the _query DSL_, which((("Query DSL"))) allows us to build
175- much more complicated, robust queries.
154+ Query-string 搜索通过命令非常方便地进行临时性的即席搜索 ((("ad hoc searches"))) ,但它有自身的局限性(参见 <<search-lite>> )。Elasticsearch 提供一个丰富灵活的查询语言叫做 _查询表达式_ ,((("Query DSL"))) 它支持构建更加复杂和健壮的查询。
176155
177- The _domain-specific language_ (DSL) is((("DSL (Domain Specific Language)"))) specified using a JSON request body.
178- We can represent the previous search for all Smiths like so:
156+ _领域特定语言_ (DSL),((("DSL (Domain Specific Language)"))) 指定了使用一个 JSON 请求。我们可以像这样重写之前的查询所有 Smith 的搜索 :
179157
180158
181159[source,js]
@@ -191,18 +169,11 @@ GET /megacorp/employee/_search
191169--------------------------------------------------
192170// SENSE: 010_Intro/30_Simple_search.json
193171
194- This will return the same results as the previous query. You can see that a
195- number of things have changed. For one, we are no longer using _query-string_
196- parameters, but instead a request body. This request body is built with JSON,
197- and uses a `match` query (one of several types of queries, which we will learn
198- about later).
172+ 返回结果与之前的查询一样,但还是可以看到有一些变化。其中之一是,不再使用 _query-string_ 参数,而是一个请求体替代。这个请求使用 JSON 构造,并使用了一个 `match` 查询(属于查询类型之一,后续将会了解)。
199173
200- === More-Complicated Searches
174+ === 更复杂的搜索
201175
202- Let's make the search a little more complicated.((("searches", "more complicated")))((("filters"))) We still want to find all
203- employees with a last name of Smith, but we want only employees who are
204- older than 30. Our query will change a little to accommodate a _filter_,
205- which allows us to execute structured searches efficiently:
176+ 现在尝试下更复杂的搜索。((("searches", "more complicated")))((("filters"))) 同样搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的。查询需要稍作调整,使用过滤器 _filter_ ,它支持高效地执行一个结构化查询。
206177
207178[source,js]
208179--------------------------------------------------
@@ -226,15 +197,10 @@ GET /megacorp/employee/_search
226197--------------------------------------------------
227198// SENSE: 010_Intro/30_Query_DSL.json
228199
229- <1> This portion of the query is the((("match queries"))) same `match` _query_ that we used before.
230- <2> This portion of the query is a `range` _filter_, which((("range filters"))) will find all ages
231- older than 30—`gt` stands for _greater than_.
200+ <1> 这部分与我们之前使用的((("match queries"))) `match` _查询_ 一样。
201+ <2> 这部分是一个 `range` _过滤器_ ,((("range filters"))) 它能找到年龄大于 30 的文档,其中 `gt` 表示_大于_(_great than_)。
232202
233-
234- Don't worry about the syntax too much for now; we will cover it in great
235- detail later. Just recognize that we've added a _filter_ that performs a
236- range search, and reused the same `match` query as before. Now our results show
237- only one employee who happens to be 32 and is named Jane Smith:
203+ 目前无需太多担心语法问题,后续会更详细地介绍。只需明确我们添加了一个 _过滤器_ 用于执行一个范围查询,并复用之前的 `match` 查询。现在结果只返回了一个雇员,叫 Jane Smith,32 岁。
238204
239205[source,js]
240206--------------------------------------------------
@@ -259,13 +225,11 @@ only one employee who happens to be 32 and is named Jane Smith:
259225}
260226--------------------------------------------------
261227
262- === Full-Text Search
228+ === 全文搜索
263229
264- The searches so far have been simple: single names, filtered by age. Let's
265- try a more advanced, full-text search--a ((("full text search")))task that traditional databases
266- would really struggle with.
230+ 截止目前的搜索相对都很简单:单个姓名,通过年龄过滤。现在尝试下稍微高级点儿的全文搜索——一项((("full text search"))) 传统数据库确实很难搞定的任务。
267231
268- We are going to search for all employees who enjoy rock climbing:
232+ 搜索下所有喜欢攀岩( rock climbing)的雇员:
269233
270234[source,js]
271235--------------------------------------------------
@@ -280,8 +244,7 @@ GET /megacorp/employee/_search
280244--------------------------------------------------
281245// SENSE: 010_Intro/30_Query_DSL.json
282246
283- You can see that we use the same `match` query as before to search the `about`
284- field for ``rock climbing''. We get back two matching documents:
247+ 显然我们依旧使用之前的 `match` 查询在`about` 属性上搜索 ``rock climbing'' 。得到两个匹配的文档:
285248
286249[source,js]
287250--------------------------------------------------
@@ -317,32 +280,20 @@ field for ``rock climbing''. We get back two matching documents:
317280 }
318281}
319282--------------------------------------------------
320- <1> The relevance scores
283+ <1> 相关性得分
321284
322- By default, Elasticsearch sorts((("relevance scores"))) matching results by their relevance score,
323- that is, by how well each document matches the query. The first and highest-scoring result is obvious: John Smith's `about` field clearly says ``rock
324- climbing'' in it.
285+ Elasticsearch ((("relevance scores"))) 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 `about` 属性清楚地写着 ``rock
286+ climbing'' 。
325287
326- But why did Jane Smith come back as a result? The reason her document was
327- returned is because the word ``rock'' was mentioned in her `about` field.
328- Because only ``rock'' was mentioned, and not ``climbing,'' her `_score` is
329- lower than John's.
288+ 但为什么 Jane Smith 也作为结果返回了呢?原因是她的 `about` 属性里提到了 ``rock'' 。因为只有 ``rock'' 而没有 ``climbing'' ,所以她的相关性得分低于 John 的。
330289
331- This is a good example of how Elasticsearch can search _within_ full-text
332- fields and return the most relevant results first. This ((("relevance", "importance to Elasticsearch")))concept of _relevance_
333- is important to Elasticsearch, and is a concept that is completely foreign to
334- traditional relational databases, in which a record either matches or it doesn't.
290+ 这是一个很好的案例,阐明了 Elasticsearch 如何 _在_ 全文属性上搜索并返回相关性最强的结果。Elasticsearch中的 _相关性_ ((("relevance", "importance to Elasticsearch"))) 概念非常重要,也是完全区别于传统关系型数据库的一个概念,数据库中的一条记录要么匹配要么不匹配。
335291
336- === Phrase Search
292+ === 短语搜索
337293
338- Finding individual words in a field is all well and good, but sometimes you
339- want to match exact sequences of words or _phrases_.((("phrase matching"))) For instance, we could
340- perform a query that will match only employee records that contain both ``rock''
341- _and_ ``climbing'' _and_ that display the words next to each other in the phrase
342- ``rock climbing.''
294+ 找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者_短语_ 。((("phrase matching"))) 比如, 我们想执行这样一个查询,仅匹配同时包含 ``rock'' _和_ ``climbing'' ,_并且_ 二者以短语 ``rock climbing'' 的形式紧挨着的雇员记录。
343295
344- To do this, we use a slight variation of the `match` query called the
345- `match_phrase` query:
296+ 为此对 `match` 查询稍作调整,使用一个叫做 `match_phrase` 的查询:
346297
347298[source,js]
348299--------------------------------------------------
@@ -357,7 +308,7 @@ GET /megacorp/employee/_search
357308--------------------------------------------------
358309// SENSE: 010_Intro/30_Query_DSL.json
359310
360- This, to no surprise, returns only John Smith's document:
311+ 毫无悬念,返回结果仅有 John Smith 的文档。
361312
362313[source,js]
363314--------------------------------------------------
@@ -384,13 +335,11 @@ This, to no surprise, returns only John Smith's document:
384335--------------------------------------------------
385336
386337[[highlighting-intro]]
387- === Highlighting Our Searches
338+ === 高亮搜索
388339
389- Many applications like to _highlight_ snippets((("searches", "highlighting search results")))((("highlighting searches"))) of text from each search result
390- so the user can see _why_ the document matched the query. Retrieving
391- highlighted fragments is easy in Elasticsearch.
340+ 许多应用都倾向于在每个搜索结果中 _高亮_ ((("searches", "highlighting search results")))((("highlighting searches"))) 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。
392341
393- Let's rerun our previous query, but add a new `highlight` parameter:
342+ 再次执行前面的查询,并增加一个新的 `highlight` 参数:
394343
395344[source,js]
396345--------------------------------------------------
@@ -410,10 +359,7 @@ GET /megacorp/employee/_search
410359--------------------------------------------------
411360// SENSE: 010_Intro/30_Query_DSL.json
412361
413- When we run this query, the same hit is returned as before, but now we get a
414- new section in the response called `highlight`. This contains a snippet of
415- text from the `about` field with the matching words wrapped in `<em></em>`
416- HTML tags:
362+ 当执行该查询时,返回结果与之前一样,与此同时结果中还多了一个叫做 `highlight` 的部分。这个部分包含了 `about` 属性匹配的文本片段,并以 HTML 标签 `<em></em>` 封装:
417363
418364[source,js]
419365--------------------------------------------------
@@ -444,7 +390,6 @@ HTML tags:
444390}
445391--------------------------------------------------
446392
447- <1> The highlighted fragment from the original text
393+ <1> 原始文本中的高亮片段
448394
449- You can read more about the highlighting of search snippets in the
450- {ref}/search-request-highlighting.html[highlighting reference documentation].
395+ 关于高亮搜索片段,可以在 {ref}/search-request-highlighting.html[highlighting reference documentation] 了解更多信息。
0 commit comments