elasticsearch 通过HTTP RESTful API 操作数据

1.检查es版本信息

http://IP:9200
curl 'IP:9200

 

2.查看集群是否健康

http://IP:9200/_cat/health?v
curl 'IP:9200/_cat/health?v'

3.查看节点列表

http://IP:9200/_cat/nodes?v
curl 'IP:9200/_cat/nodes?v'

4.列出所有索引及存储大小

http://IP:9200/_cat/indices?v
curl 'IP:9200/_cat/indices?v'

5.创建简单索引

 使用postman工具

curl -XPUT IP:9200/索引名称(小写)

6.创建复杂索引

curl -XPUT IP:9200/索引名称/ -d '{
    "settings":{
           "number_of_shards":1,     //设置分片数量
           "number_of_replicas":2,  //设置副本数量
           //自定义索引默认分析器
           "index":{
                  "analysis":{
                         "analyzer":{
                                "default":{
                                       "tokenizer":"standard",     //分词器
                                       "filter":[ //过滤器
                                              "asciifolding",
                                              "lowercase",
                                              "ourEnglishFilter"
                                       ]
                                }
                         },
                         "filter":{
                                "ourEnglishFilter":{
                                       "type":"kstem"
                                }
                         }
                  }
           }
    }
}'

7.分片设置

number_of_shards
每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。
number_of_replicas

每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。例如,我们可以创建只有 一个主分片,没有副本的小索引:

PUT IP:9200/索引名称
{
    "settings": {
    "number_of_shards" :   1,
    "number_of_replicas" : 0
    }
}

更改副本数量:

PUT IP:9200/索引名称/_settings
{
    "number_of_replicas": 2
}

每次更改分片之后可以使用

GET IP:9200/索引名称/_search_shards 

来查询索引信息

8.创建mapping

首先查看刚刚创建的索引的mapping是什么样子的

htt://IP:9200/索引名称/_mapping

可见新建的索引中,mapping是一个空集,所以我们就要创建这个index的mapping

curl -POST IP:9200/索引名称/索引类型/_mapping?pretty -d '{
  索引类型: {
    "properties": {
      "title": {
        "type": "text",
        "store": "true"
      },
      "description": {
        "type": "text",
        "index": "false"
      },
      "price": {
        "type": "double"
      },
      "onSale": {
        "type": "boolean"
      },
      "type": {
        "type": "integer"
      },
      "createDate": {
        "type": "date"
      }
    }
  }
}'

9.插入数据

curl -POST IP:9200/索引名称/索引类型 -d '{
  "title": "test title 001",
  "description": "this is a random desc ",
  "price": 22.6,
  "onSale": "true",
  "type": 2,
  "createDate": "2018-01-12"
}'

 然后查询一下所有数据,默认为match_all

curl -GET IP:9200/索引名称/索引类型/_search

按ID进行查询

curl -GET IP:9200/索引名称/索引类型/ID

10.直接创建索引并插入文档

curl -PUT  IP:9200/索引名称/索引类型/文档ID -d '{
  "title": "test title 003",
  "description": "this is a random desc ",
  "price": 22.6,
  "onSale": "true",
  "type": 2,
  "createDate": "2018-01-12"
}'

11.轻量搜索

  • 检索全部
curl IP:9200/索引名称/_search?pretty

curl -XPOST localhost:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }}"

query:告诉我们定义查询 
match_all:运行简单类型查询指定索引中的所有文档

  • match_all & 只返回前两个文档
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"size\" : 2}"

 如果不指定size,默认是返回10条文档信息

  • match_all & 返回第11到第20的10个文档信息 & 降序排序
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"from\" : 10, \"size\" : 10,\"sort\" : {\"timestamp\" : {\"order\" : \"desc\" }}}"

from:指定文档索引从哪里开始,默认从0开始 
size:从from开始,返回多个文档 
sort:排序,排序字段与排序方式
这feature在实现分页查询很有用

  • 比如只返回title和description两个字段
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"_source\": [\"title\", \"description\"]}"

  • 返回 name=student60
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match\": {\"name\": \"student60\" } }}"

  • 返回 title=test
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match\": {\"title\": \"test\" } }}"

  • 返回 title=005 or name=007
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match\": {\"title\": \"005 007\" } }}"

  • 返回 短语匹配 title=title 007
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_phrase\": {\"title\": \"title 007\" } }}"

  • 布尔值(bool)查询 返回 title=title & title=007
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"bool\": {\"must\": [{\"match\": {\"title\": \"title\" }},{\"match\": {\"title\": \"007\" }}]}}}"

 must:要求所有条件都要满足(类似于&&)

  • 返回 title=007 or title=005
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"bool\": {\"should\": [{\"match\": {\"title\": \"007\" }},{\"match\": {\"title\": \"005\" }}]}}}"

should:任何一个满足就可以(类似于||)

  • 返回 不匹配title=007 & title=005
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"bool\": {\"must_not\": [{\"match\": {\"title\": \"007\" }},{\"match\": {\"title\": \"005\" }}]}}}"

must_not:所有条件都不能满足(类似于! (&&))

  • 返回 type=2 & title!=005
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"bool\": {\"must\": [{\"match\": {\"type\": 2 }}],\"must_not\": [{\"match\": {\"title\": \"005\" }}]}}}"

 

猜你喜欢

转载自blog.csdn.net/ctwy291314/article/details/81202386