java面试题网站:www.javaoffers.com
基础知识:
在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库:
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
PUT 插入值 (PUT 必须大写)
cmj@cmj-Inspiron-7590:~/nohup$ curl -X PUT -H 'content-type:application/json' -d '{"first_name":"Cao","last_name":"MJ","age":25,"about":"I like to build cabinets","interests":["forestry"]}' localhost:9200/a/b/2
GET 查询值
#索引查询
cmj@cmj-Inspiron-7590:~/nohup$ curl -X GET localhost:9200/a/b/2
结果:
{"_index":"a","_type":"b","_id":"2","_version":1,"found":true,"_source":{"first_name":"Cao","last_name":"MJ","age":25,"about":"I like to build cabinets","interests":["forestry"]}}
#参数查询
cmj@cmj-Inspiron-7590:~/nohup$ curl -X GET localhost:9200/a/b/_search?q=last_name:CMJ
结果:
{"took":68,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
#使用dsl语句查询
curl -X GET -H 'content-type:application/json' localhost:9200/a/b/_search -d "{\"query\":{\"match\":{\"last_name\":\"Fir\"}}}"
结果
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"a","_type":"b","_id":"1","_score":0.2876821,"_source":{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests":["forestry"]}}]}}
DELETE删除
cmj@cmj-Inspiron-7590:~/nohup$ curl -X DELETE localhost:9200/a/b/2
{"_index":"a","_type":"b","_id":"2","_version":6,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":5,"_primary_term":19}