蛤蟆功Elasticsearch——使用Elasticsearch建立索引并查询文本

转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/89310301
本文出自【我是干勾鱼的博客

Ingredients:

之前在蛤蟆功Elasticsearch——Elasticsearch安装及简单使用中介绍了Elasticsearch的安装方式,这里来介绍一下Elasticsearch的基本用法,官网在Exploring Your Cluster进行了比较详细的介绍。

1 安全检查(Cluster Health

命令如下:

GET /_cat/health?v

例如:

curl -XGET 'localhost:9200/_cat/health?v'

2 索引列表(List All Indices

命令如下:

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

之前还没有创建任何索引,所以这时候查看不到任何索引内容。

3 创建索引(Create an Index

命令如下:

PUT /customer?pretty
GET /_cat/indices?v

例如:

[root@shizhi002 elasticsearch-6.6.2]# curl -X PUT 'localhost:9200/customer?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}

这时候就能查看索引列表的内容了:

curl -XGET 'flx01:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   customer   5   1          0            0       720b           720b

4 建立索引、查询文件(Index and Query a Document

4.1 建立索引

命令如下:

PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}

例如:

[root@shizhi002 elasticsearch-6.6.2]# curl -X PUT -H 'Content-Type: application/json' 'http://localhost:9200/customer/_doc/1?pretty' -d '
> {
> "name": "John Doe"
> }'
{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 2
}

有一点需要注意的是,在为文本建立索引之前,是不需要预先显性的创建一个索引的,也就是说,只要你使用命令将文本放入一个索引中,这个索引就创建了,后面继续将文本放入其中就可以了。

4.2 检索之前已经建立了索引的文本

命令如下:

GET /customer/_doc/1?pretty

例如:

[root@shizhi002 elasticsearch-6.6.2]# curl -XGET 'localhost:9200/customer/_doc/1?pretty'
{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

5 删除索引(Delete an Index

命令如下:

DELETE /customer?pretty
GET /_cat/indices?v

例如:

[root@shizhi002 elasticsearch-6.6.2]# curl -X DELETE 'localhost:9200/customer?pretty'
{
  "acknowledged" : true
}

6 API的语法

之前涉及到的常用API是这些:

PUT /customer
PUT /customer/_doc/1
{
  "name": "John Doe"
}
GET /customer/_doc/1
DELETE /customer

其语法可以总结如下:

<HTTP Verb> /<Index>/<Type>/<ID>

7 查看索引的所有数据

如果要查找某个Index中所有的数据,则:

curl -XGET 'localhost:9200/customer/_doc/_search?pretty'

8 关于mapping

Elasticsearch中的mapping,其实相当于数据库表中的字段类型。

8.1 创建mapping

创建mapping,这里参考了Building real-time dashboard applications with Apache Flink, Elasticsearch, and Kibana

curl -XPUT 'flx01:9200/nyc-idx/_mapping/popular-locations' -d '
{
"popular-locations":{
  "properties": {
    "cnt": {"type": "integer"}, 
    "location": {"type": "geo_point"}, 
    "time": {"type": "date"}
  }
 }
}'

8.2 查看索引的所有mapping

查看mapping:

[root@shizhi002 opt]# curl -XGET http://localhost:9200/customer/_mapping?pretty
{
  "customer" : {
    "mappings" : {
      "_doc" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

9 参考

Getting Started(Elasticsearch 6.6)

Elasticsearch Mapping的解析、数据类型、Mapping 支持属性、Mapping 字段设置流程

Building real-time dashboard applications with Apache Flink, Elasticsearch, and Kibana

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页