(1)通常格式java
PUT blog/segmentfault/1 { "id":1, "title":"Elasticsearch简介", "author":"kyle", "content":"Elasticsearch是一个基于Lucene的搜索引擎" }
名称 | 参数 | 说明 |
---|---|---|
blog | _index | 索引名 |
segmentfault | _type | 类型名 |
1 | _id | 文档ID |
1 | _version | 版本号 |
(2)未指定文档IDgit
POST blog/segmentfault { "id":3, "title":"Java编程", "author":"kyle", "content":"Java面向对象程序设计" }
(3)添加多个type
6.x以后不容许添加多个type编程
POST blog/csdn { "id":3, "title":"Java编程", "author":"chengyuqiang", "content":"Java面向对象程序设计" } { "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Rejecting mapping update to [blog] as the final mapping would have more than 1 type: [csdn, segmentfault]" } ], "type": "illegal_argument_exception", "reason": "Rejecting mapping update to [blog] as the final mapping would have more than 1 type: [csdn, segmentfault]" }, "status": 400 }
GET /blog/segmentfault/1 { "_index": "blog", "_type": "segmentfault", "_id": "1", "_version": 1, "found": true, "_source": { "id": 1, "title": "Elasticsearch简介", "author": "kyle", "content": "Elasticsearch是一个基于Lucene的搜索引擎" } }
(1)、检索所有segmentfault
GET /blog/_search
(2)、term查询
term查询用于查找指定字段中包含指定分词的文件,只有当查询分词和文档中的分词精确匹配时才被检索到。app
GET /blog/_search { "query": { "term": { "content": { "value": "程" } } } } { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "blog", "_type": "segmentfault", "_id": "V0JLZmYBnUSb-hp-GzV6", "_score": 0.2876821, "_source": { "id": 3, "title": "Java编程", "author": "kyle", "content": "Java面向对象程序设计" } } ] } }
当查询”程序”时,title字段中找不到这样的分词,默认汉字被分为单字词。less
GET /blog/_search { "query": { "term": { "content": { "value": "程序" } } } } { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
(3)terms查询分布式
GET /blog/_search { "query": { "terms": { "title": [ "java", "git" ] } } } 注意,通过分词后英文单词变成了小写,好比”Java”词项变成了”java” { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "blog", "_type": "segmentfault", "_id": "V0JLZmYBnUSb-hp-GzV6", "_score": 1, "_source": { "id": 3, "title": "Java编程", "author": "kyle", "content": "Java面向对象程序设计" } }, { "_index": "blog", "_type": "segmentfault", "_id": "2", "_score": 1, "_source": { "id": 2, "title": "Git简介", "author": "lalala", "content": "Git是一个版本控制软件" } } ] } }
(4)match查询
与term精确查询不一样,对于match查询,只要被查询字段中存在任何一个词项被匹配,就会搜索到该文档。post
GET /blog/_search { "query": { "match": { "content": "程序" } } } { "took": 18, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "blog", "_type": "segmentfault", "_id": "V0JLZmYBnUSb-hp-GzV6", "_score": 0.5753642, "_source": { "id": 3, "title": "Java编程", "author": "kyle", "content": "Java面向对象程序设计" } } ] } }
(1)更新数据
文档在Elasticsearch中是不可变的,不能修改。若是咱们须要修改文档,Elasticsearch实际上重建新文档替换掉旧文档。搜索引擎
POST /blog/segmentfault/2 { "id":2, "title":"Git简介", "author":"hahaha", "content":"Git是一个分布式版本控制软件" } { "_index": "blog", "_type": "segmentfault", "_id": "2", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
NOTE:设计
(2)更新字段
POST /blog/segmentfault/2/_update { "script": { "source": "ctx._source.content=\"GIT是一个开源的分布式版本控制软件\"" } } { "_index": "blog", "_type": "segmentfault", "_id": "2", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
(3)添加新字段
POST /blog/segmentfault/2/_update { "script": "ctx._source.posttime=\"2018-10-09\"" } { "_index": "blog", "_type": "segmentfault", "_id": "2", "_version": 4, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
(4)查询更新
POST blog/_update_by_query { "script": { "source": "ctx._source.category=params.category", "lang":"painless", "params":{"category":"git"} }, "query":{ "term": {"title":"git"} } }
DELETE /blog/segmentfault/2 { "_index": "blog", "_type": "segmentfault", "_id": "2", "_version": 7, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 6, "_primary_term": 1 }