一、索引操作
---------------------------------
创建索引(PUT)
PUT /索引名
curl -X PUT http://10.20.20.214:9200/shopping
设置映射关系(类似mysql的表的字段设置属性) (PUT /索引名/_mapping)
{"properties":{ //特性
"name":{ //字段
"type":"text", //字段属性
"index":true //是否能被索引
}
}
}
curl -X PUT http://10.20.20.214:9200/user {"acknowledged":true,"shards_acknowledged":true,"index":"user"} curl -X PUT http://10.20.20.214:9200/user/_mapping?pretty -d '{"properties":{"name":{"type":"text","index":true}, "sex":{"type":"keyword","index":true},"tel":{"type":"keyword","index":false}}}' -H "Content-Type:application/json" { "acknowledged" : true }
查询映射关系:GET /索引名/_mapping
curl -X GET http://10.20.20.214:9200/user/_mapping?pretty { "user" : { "mappings" : { "properties" : { "name" : { "type" : "text" }, "sex" : { "type" : "keyword" }, "tel" : { "type" : "keyword", "index" : false } } } } }
- 查询字段type:keyword的则 match查询时,全词模糊匹配。
- 查询字段type:text 的则 match查询时,输入的查询关键词会被拆分后再进行模糊匹配
查询索引(GET /_cat/indices?v)
GET http://10.20.20.214:9200/_cat/indices?v
二、数据操作
--------------------------------
添加数据 (body中添加数据{} json格式)
- POST /索引名/_doc
- PUT /索引名/_create
curl -X POST http://10.20.20.214:9200/shopping/_doc -d '{"name":"zhangmingda", "age":23}' -H "Content-Type:application/json"
查询数据
- 查单条数据:GET /索引名/_doc/索引ID
- 查所有数据:GET /索引名/_search
[root@vm10-20-9-45 ElasticSearch]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty=true { "took" : 438, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "shopping", "_type" : "_doc", "_id" : "4V9BSnsBL-0_1XxfCTs7", "_score" : 1.0, "_source" : { "name" : "zhangmingda", "age" : 23 } }, ....... ] } }
-
过滤查询
- GET /index/_search?pretty body中传递查询参数
{"query":{"match":{"category":"小米"}}}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match":{"category":"小米"}}}'
- 查询所有(body传参)
{"query":{"match_all":{}}}
-
- 分页查询
{"query":{"match_all":{}},"from":0,"size":2}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"from":0,"size":2}' { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 13, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "shopping", "_type" : "_doc", "_id" : "4V9BSnsBL-0_1XxfCTs7", "_score" : 1.0, "_source" : { "name" : "zhangmingda", "age" : 23 } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "519NSnsBL-0_1XxfIztW", "_score" : 1.0, "_source" : { "name" : "zhangmingda", "age" : 23 } } ] } }
- 只要数据的特定字段"query":{"match_all":{}},"_source":["title"]}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}'
[root@vm10-20-9-45 ~]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}' { "took" : 13, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 13, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "shopping", "_type" : "_doc", "_id" : "4V9BSnsBL-0_1XxfCTs7", "_score" : 1.0, "_source" : { } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "519NSnsBL-0_1XxfIztW", "_score" : 1.0, "_source" : { } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "6F9NSnsBL-0_1XxfMjvj", "_score" : 1.0, "_source" : { } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10001", "_score" : 1.0, "_source" : { "title" : "华为V8" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10002", "_score" : 1.0, "_source" : { "title" : "小米手机4" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10003", "_score" : 1.0, "_source" : { "title" : "小米手机4" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10004", "_score" : 1.0, "_source" : { "title" : "小米手机4" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10005", "_score" : 1.0, "_source" : { "title" : "小米手机6" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "10008", "_score" : 1.0, "_source" : { "title" : "小米手机8" } }, { "_index" : "shopping", "_type" : "_doc", "_id" : "20008", "_score" : 1.0, "_source" : { "title" : "华为V1" } } ] } }