lqshow / notes

Cheat Sheet
10 stars 2 forks source link

Elasticsearch Intro #14

Open lqshow opened 6 years ago

lqshow commented 6 years ago

文档中的每个字段都将被索引并且可以被查询。 默认按照相关性得分排序, 即每个文档跟查询的匹配程度。 默认一次返回10条结果,可以通过size字段改变这个设置。

Install Elasticsearch on OSX

brew update
brew install elasticsearch

View Info


brew info elasticsearch

elasticsearch --version

### Run Elasticsearch on OSX
```shell
elasticsearch &

Check Status

curl -X GET '127.0.0.1:9200'
{
  "name" : "zLAom2N",
  "cluster_name" : "elasticsearch_linqiong",
  "cluster_uuid" : "_wthEgJHSHmPX4CGJpW3Gg",
  "version" : {
    "number" : "5.6.2",
    "build_hash" : "57e20f3",
    "build_date" : "2017-09-23T13:16:45.703Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

Relational DB & Elasticsearch

Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices   -> Types  -> Documents -> Fields

查看索引健康状态

curl -X GET 'localhost:9200/_cluster/health?level=indices&pretty'

status 字段指示着当前集群在总体上是否工作正常。它的三种颜色含义如下

status desc
green 所有的主分片和副本分片都正常运行。
yellow 所有的主分片都正常运行,但不是所有的副本分片都正常运行。
red 有主分片没能正常运行。

es有两种查询模式

查询文档元数据

keyword desc
took 执行整个搜索请求耗费了多少毫秒
time_out 查询是否超时
_shards 在查询中参与分片的总数,以及这些分片成功了多少个失败了多少个
hits 命中的记录(查询结果)

hits 子字段

keyword desc
total 返回记录数
max_score 最高的匹配程度
hits 返回的记录组成的数组 (返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。)

每个文档对象(hits)

keyword desc
_index 文档在哪存放
_type 文档表示的对象类别
_id 文档唯一标识
{
    "took":57,
    "timed_out":false,
    "_shards":{
        "total":25,
        "successful":25,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":22,
        "max_score":1,
        "hits":[
            {
                "_index":"us",
                "_type":"tweet",
                "_id":"14",
                "_score":1,
                "_source":{
                    "date":"2014-09-24",
                    "name":"John Smith",
                    "tweet":"How many more cheesy tweets do I have to write?",
                    "user_id":1
                }
            }
        ]
    }
}

References