码迷,mamicode.com
首页 > 其他好文 > 详细

ElasticSearch搜索介绍四

时间:2018-01-17 00:34:36      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:pen   sql   gpo   close   参数   显示   5.5   文档   获取   

ElasticSearch搜索

 

最基础的搜索:

curl -XGET http://localhost:9200/_search

返回的结果为:

技术分享图片
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 16,
    "successful": 16,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 1,
    "hits": [
      {
        "_index": ".kibana",
        "_type": "config",
        "_id": "5.5.1",
        "_score": 1,
        "_source": {
          "buildNum": 15405
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "8",
        "_score": 1,
        "_source": {
          "name": "deda",
          "age": 31,
          "job": "stu"
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "30",
        "_score": 1,
        "_source": {
          "name": "tomi",
          "age": 19,
          "job": "master"
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "ming",
          "age": 33,
          "job": "coding",
          "like": "football",
          "tel": "122334"
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "fukk",
          "age": 30,
          "job": "stu"
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "mask",
          "age": 40,
          "job": "CEO"
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "6",
        "_score": 1,
        "_source": {
          "name": "nan",
          "age": 25,
          "job": "coding"
        }
      },
      {
        "_index": "peoples",
        "_type": "employee",
        "_id": "2",
        "_score": 1,
        "_source": {
          "first_name": "Jane",
          "last_name": "Smith",
          "age": 32,
          "about": "I like to collect rock albums",
          "interests": [
            "music"
          ]
        }
      },
      {
        "_index": "megacorp",
        "_type": "employee",
        "_id": "1",
        "_score": 1,
        "_source": {
          "first_name": "John",
          "last_name": "Smith",
          "age": 25,
          "about": "I love to go rock climbing",
          "interests": [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index": "people",
        "_type": "man",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "ahaii",
          "age": 27,
          "job": "coding"
        }
      }
    ]
  }
}
View Code

索引所有文档,返回结果中默认显示前十条数据。

hits:

返回结果中最重要的部分是 hits ,它 包含 total 字段来表示匹配到的文档总数,并且一个 hits 数组包含所查询结果的前十个文档。

在 hits 数组中每个结果包含文档的 _index 、 _type 、 _id ,加上 _source 字段。这意味着我们可以直接从返回的搜索结果中使用整个文档。这不像其他的搜索引擎,仅仅返回文档的ID,需要你单独去获取文档。

每个结果还有一个 _score ,它衡量了文档与查询的匹配程度。默认情况下,首先返回最相关的文档结果,就是说,返回的文档是按照 _score 降序排列的。在这个例子中,我们没有指定任何查询,故所有的文档具有相同的相关性,因此对所有的结果而言 1 是中性的 _score 。

took:

took 值表示执行整个搜索请求耗费了多少毫秒。

shards:

_shards 部分告诉我们在查询中参与分片的总数,以及这些分片成功了多少个失败了多少个。正常情况下我们不希望分片失败,但是分片失败是可能发生的。如果我们遭遇到一种灾难级别的故障,在这个故障中丢失了相同分片的原始数据和副本,那么对这个分片将没有可用副本来对搜索请求作出响应。假若这样,Elasticsearch 将报告这个分片是失败的,但是会继续返回剩余分片的结果。

不同索引、不同类型的搜索:

/_search在所有的索引中搜索所有的类型

/gb/_search在 gb 索引中搜索所有的类型

/gb,us/_search在 gb 和 us 索引中搜索所有的文档

/g*,u*/_search在任何以 g 或者 u 开头的索引中搜索所有的类型

/gb/user/_search在 gb 索引中搜索 user 类型

/gb,us/user,tweet/_search在 gb 和 us 索引中搜索 user 和 tweet 类型

/_all/user,tweet/_search在所有的索引中搜索 user 和 tweet 类型

当在单一的索引下进行搜索的时候,Elasticsearch 转发请求到索引的每个分片中,可以是主分片也可以是副本分片,然后从每个分片中收集结果。多索引搜索恰好也是用相同的方式工作的--只是会涉及到更多的分片。

搜索一个索引有五个主分片和搜索五个索引各有一个分片准确来所说是等价的。

 

搜索结果分页:

和 SQL 使用 LIMIT 关键字返回单个 page 结果的方法相同,Elasticsearch 接受 from 和 size 参数:

size
显示应该返回的结果数量,默认是 10
from
显示应该跳过的初始结果数量,默认是 0

 显示3条搜索结果:

curl -XGET http://localhost:9200/_search?size=3

显示第4-6条搜索结果:

curl -XGET http://localhost:9200/_search?size=3&from=3

显示第7-9条搜索结果:

curl -XGET http://localhost:9200/_search?size=3&from=6

 

ElasticSearch搜索介绍四

标签:pen   sql   gpo   close   参数   显示   5.5   文档   获取   

原文地址:https://www.cnblogs.com/ahaii/p/8298014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!