蛤蟆功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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章