暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

5、ElasticSearch入门-开始搜索

产品与码农 2020-04-07
294

Start searching

  • 将一些数据摄取到Elasticsearch索引后,您可以通过将请求发送到_search端点来对其进行搜索。要访问全套搜索功能,请使用Elasticsearch Query DSL在请求正文中指定搜索条件。您可以在请求URI中指定要搜索的索引的名称。Once you have ingested some data into an Elasticsearch index, you can search it by sending requests to the _search endpoint. To access the full suite of search capabilities, you use the Elasticsearch Query DSL to specify the search criteria in the request body. You specify the name of the index you want to search in the request URI.

  • 例如,以下请求检索bank 按帐号排序的索引中的所有文档:For example, the following request retrieves all documents in the bank index sorted by account number:

GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}

复制
  • 默认情况下,hits响应部分包含与搜索条件匹配的前10个文档:By default, the hits section of the response includes the first 10 documents that match the search criteria:


"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value": 1000,
"relation": "eq"
},
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"
_type" : "_doc",
"
_id" : "0",
"
sort": [0],
"
_score" : null,
"
_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
}, {
"
_index" : "bank",
"
_type" : "_doc",
"
_id" : "1",
"
sort": [1],
"
_score" : null,
"
_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}, ...
]
}
}

复制
  • 该响应还提供有关搜索请求的以下信息:The response also provides the following information about the search request:

    • took – Elasticsearch运行查询多长时间(以毫秒为单位)how long it took Elasticsearch to run the query, in milliseconds

    • timed_out – 搜索请求是否超时whether or not the search request timed out

    • _shards – 搜索了多少个分片以及成功,失败或跳过了多少个分片。how many shards were searched and a breakdown of how many shards succeeded, failed, or were skipped.

    • max_score –找到的最相关文件的分数the score of the most relevant document found

    • hits.total.value -找到了多少个匹配的文档how many matching documents were found

    • hits.sort -文档的排序位置(不按相关性得分排序时)the document’s sort position (when not sorting by relevance score)

    • hits._score-文档的相关性得分(使用时不适用match_all)the document’s relevance score (not applicable when using match_all)

  • 每个搜索请求都是独立的:Elasticsearch在请求中不维护任何状态信息。要翻阅搜索结果,请在您的请求中指定from和size参数。Each search request is self-contained: Elasticsearch does not maintain any state information across requests. To page through the search hits, specify the from and size parameters in your request.

  • 例如,以下请求的匹配数为10到19:For example, the following request gets hits 10 through 19:

GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
],
"from": 10,
"size": 10
}

复制
  • 既然您已经了解了如何提交基本的搜索请求,则可以开始构建比有趣的查询match_all。Now that you’ve seen how to submit a basic search request, you can start to construct queries that are a bit more interesting than match_all.

  • 要在字段中搜索特定术语,可以使用match查询。例如,以下请求搜索该address字段以查找地址包含mill或的客户lane:To search for specific terms within a field, you can use a match query. For example, the following request searches the address field to find customers whose addresses contain mill or lane:

GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}


复制
  • 要执行词组搜索而不是匹配单个词,请使用 match_phrase代替match。例如,以下请求仅匹配包含短语的地址mill lane: To perform a phrase search rather than matching individual terms, you use match_phrase instead of match. For example, the following request only matches addresses that contain the phrase mill lane:

GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}

复制
  • 要构造更复杂的查询,可以使用bool查询来组合多个查询条件。您可以根据需要(必须匹配),期望(应该匹配)或不期望(必须不匹配)指定条件。To construct more complex queries, you can use a bool query to combine multiple query criteria. You can designate criteria as required (must match), desirable (should match), or undesirable (must not match).

  • 例如,以下请求在bank索引中搜索属于40岁客户的帐户,但不包括居住在爱达荷州(ID)的任何人:For example, the following request searches the bank index for accounts that belong to customers who are 40 years old, but excludes anyone who lives in Idaho (ID):

GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}

复制
  • 布尔查询中的每个must,should和must_not元素称为查询子句。文档满足每个条款must或 should条款的标准的程度有助于文档的相关性得分。分数越高,文档就越符合您的搜索条件。默认情况下,Elasticsearch返回按这些相关性分数排名的文档。 Each must, should, and must_not element in a Boolean query is referred to as a query clause. How well a document meets the criteria in each must or should clause contributes to the document’s relevance score. The higher the score, the better the document matches your search criteria. By default, Elasticsearch returns documents ranked by these relevance scores.

  • must_not子句中的条件被视为过滤器。它会影响文档是否包含在结果中,但不会影响文档的评分方式。您还可以显式指定任意过滤器,以基于结构化数据包括或排除文档。The criteria in a must_not clause is treated as a filter. It affects whether or not the document is included in the results, but does not contribute to how documents are scored. You can also explicitly specify arbitrary filters to include or exclude documents based on structured data.

  • 例如,以下请求使用范围过滤器将结果限制为余额在20,000美元到30,000美元(含)之间的帐户。For example, the following request uses a range filter to limit the results to accounts with a balance between $20,000 and $30,000 (inclusive).

GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}

复制


文章转载自产品与码农,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论