Elasticsearch系列七:常见用法手册

前面几篇文章介绍了搜索引擎ElasticSearch的内部原理,这篇文章总结了在ElasticSearch使用过程当中常见的用法。node

一、查看集群信息

ElasticSearch 查看集群健康信息,经常使用命令以下:git

1.一、查看集群状态

ElasticSearch查看集群状态命令:github

curl 'localhost:9200/_cat/health?v'算法

其中,status为绿色表示一切正常, 黄色表示全部的数据可用可是部分副本尚未分配,红色表示部分数据由于某些缘由不可用.

1.二、查看索引分片状况

ElasticSearch查看索引分片状况命令:json

curl 'localhost:9200/_cat/shards?v'数据结构

能够看到例子中有两个索引分别是:people和product,他们各有一个主分片和一个还没有分配的副分片。

1.三、查看节点信息

ElasticSearch查看节点信息命令:app

  • 主节点:curl 'localhost:9200/_cat/master?v'
  • 全部节点:curl 'localhost:9200/_cat/nodes?v'

二、索引相关命令

2.一、建立索引

ElasticSearch建立索引命令。curl

7.0版本去掉了type的概念。elasticsearch

2.1.一、7.0以前版本建立索引

curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d '
{
  "mappings": {
    "type_name": {
      "properties": {
        "price": {
          "type": "integer"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}'

2.1.二、7.0及其以后的版本建立索引

curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d '
{
  "mappings": {
      "properties": {
        "price": {
          "type": "integer"
        },
        "name": {
          "type": "text"
        }
      }
    }
}'

2.二、查询索引

ElasticSearch查询全部的索引命令。搜索引擎

curl 'localhost:9200/_cat/indices?v'

能够看到上面的例子中,集群中有两个索引。他们的名字分别为:people、product。且分别有一个主分片和副分片。

2.三、查询索引结构

2.3.一、查看全部的索引mapping数据结构

ElasticSearch查询索引mapping结构命令:

curl 'localhost:9200/_mapping?pretty'

上面的例子查询了索引people和product的mapping数据结构。

2.3.二、查看指定的索引mapping数据结构

2.3.2.一、7.0以前版本

查看索引名为indexName且type名为typeName的索引mapping数据结构:

curl -XGET "127.0.0.1:9200/indexName/typeName/_mapping?pretty"

2.3.2.二、7.0及其以后版本

查看索引名为indexName的索引mapping数据结构:

curl -XGET "127.0.0.1:9200/indexName/_mapping?pretty"

2.四、给索引添加字段

ElasticSearch在索引中添加字段命令

2.4.一、7.0以前版本

curl -XPOST "127.0.0.1:9200/indexName/typeName/_mapping?pretty" -H 'Content-Type: application/json' -d '{
 "typeName": {
            "properties": {
                 "tags":{
                    "type":"text"
               }
            }
        }
}'
上面的例子,表示给索引名为indexName且type名为typeName的mapping数据结构添加tags字段

2.4.二、7.0及其以后版本

curl -XPOST "127.0.0.1:9200/product/_mapping?pretty" -H 'Content-Type: application/json' -d '{
    "properties": {
         "tags":{
            "type":"text"
       }
    }
}'
上面的例子,表示给索引名为product的mapping数据结构添加tags字段

2.五、索引中删除字段

ElasticSearch如今不支持

2.六、删除索引

ElasticSearch 删除某个索引命令。

curl -X DELETE 'localhost:9200/product'

上面的例子表示删除名为product的索引。

三、数据文档搜索查询相关命令

下面提到的数据即索引中的文档。

3.一、写入数据命令

ElasticSearch写入数据命令.

7.0版本去掉了type的概念。

3.1.一、7.0以前版本写入数据

curl -X PUT 'localhost:9200/product/type_name/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山苹果"
}'

3.1.二、7.0及其以后的版本写入数据

curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山苹果"
}'

3.二、搜索查询数据命令

ElasticSearch搜索数据命令

3.2.一、7.0以前版本搜索数据

3.2.1.一、主键搜索

curl -X GET 'localhost:9200/product/type_name/1?pretty'

3.2.1.二、关键字搜索
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '
{
  "query": {
    "match": {
      "name": "苹果"
    }
  },
  "from":0,
  "size":1
}'

3.2.二、7.0及其以后的版本搜索数据

3.2.2.一、主键搜索

curl -X GET 'localhost:9200/product/_doc/1?pretty'

3.2.2.二、关键字搜索
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '
{
  "query": {
    "match": {
      "name": "苹果"
    }
  },
  "from":0,
  "size":1
}'

3.2.三、对多值的字段搜索

查询name包含“苹果”且tags有“苹果”或者“富士山”的文档数据:

curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query":{
        "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {"match":{"name":"苹果"}},
                            {"terms": {"tags": ["苹果","富士山"]}}
                        ]
                    }
                }
            ]
        }
    },
    "sort":{
        "_score":{"order":"desc"}
    },
    "from":0,
    "size":10
}'

3.2.四、多字段联合查询

查询name包含“苹果”且price为1的文档数据:

curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query":{
        "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {"match":{"name":"苹果"}},
                            {"match":{"price":1}}
                        ]
                    }
                }
            ]
        }
    },
    "sort":{
        "_score":{"order":"desc"}
    },
    "from":0,
    "size":10
}'

3.三、更新数据命令

ElasticSearch中更新数据命令:

curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山苹果",
    "tags":["富士山","名牌","苹果"]
}'

3.四、删除数据

ElasticSearch中删除文档数据命令:

3.4.一、7.0以前版本

删除一个文档

curl -XDELETE 'http://localhost:9200/indexName/typeName/1'

上面的例子表示删除索引名为indexName且type名为typeName的索引中文档ID为1的文档。

3.4.二、7.0及其以后的版本

curl -XDELETE 'http://localhost:9200/indexName/_doc/1'

四、ElasticSearch中分词器分析器在命令行中的用法

ElasticSearch支持不一样的分词插件,在下面的例子中咱们使用了analysis-ik分词插件。

经过ElasticSearch的API接口,能够分析不一样分词器的分词结果1。具体的步骤以下:

4.一、添加两个字符类型的字段,并指定不一样的分词器:

curl -XPOST "127.0.0.1:9200/product/_mapping?pretty" -H 'Content-Type: application/json' -d '{
    "properties": {
        "pNameIkMaxWord":{
            "type":"text",
            "analyzer":"ik_max_word"
        },
        "pNameIkSmart":{
            "type":"text",
            "analyzer":"ik_smart"
        }
    }
}'

4.二、使用ik_max_word分词分析

curl -XPOST 'http://localhost:9200/product/_analyze?pretty' -H 'Content-Type: application/json' -d '
{
    "field": "pNameIkMaxWord",
    "text": "中华人民共和国国歌"
}'

分词结果以下:

{
  "tokens" : [
    {
      "token" : "中华人民共和国",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "中华人民",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "中华",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "华人",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "人民共和国",
      "start_offset" : 2,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "人民",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "共和国",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "共和",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "国",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 8
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 9
    }
  ]
}

4.三、使用ik_smart分词分析

curl -XPOST 'http://localhost:9200/product/_analyze?pretty' -H 'Content-Type: application/json' -d '
{
    "field": "pNameIkSmart",
    "text": "中华人民共和国国歌"
}'

分词结果以下:

{
  "tokens" : [
    {
      "token" : "中华人民共和国",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

系列文章

  1. ElasticSearch系列一:源码编译和Debug环境搭建
  2. ElasticSearch系列二:启动过程详解
  3. Elasticsearch系列三:建立索引过程详解
  4. Elasticsearch系列四:搜索过程详解
  5. Elasticsearch系列五:搜索相关性排序算法详解
  6. Elasticsearch系列六:ES中的倒排索引
  7. Elasticsearch系列七:常见用法手册

  1. https://elasticsearch.cn/arti...
相关文章
相关标签/搜索