假设一个系统中有多个汽车,图书,家具 等等索引
图书索引,按类型分为科普,小说,心理,历史等等类型。
具体到每一本书籍就是文档,最小的存储单位
分片:每个索引都有多个分片,每个分片是一个Lucene索引
备份:拷贝一份分片就完成了分片的备份
url :http://192.168.1.106:9200/people put
{
"settings":{
"number_of_shards": 3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
创建索引类型
number_of_shards:指定分片数
number_of_replicas:指定副本数
{
"name":"盖伦",
"country":"American",
"age":20,
"date":"1998-01-01"
}
插入成功返回
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
{
"name":"吕布",
"country":"China",
"age":20,
"date":"1999-01-01"
}
插入成功返回
{
"_index": "people",
"_type": "man",
"_id": "FAE5_mcBx_MQjeQ5a8AI",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
此时系统会给_id 生成一个id
url:http://192.168.1.106:9200/people/man/1/_update post方式
数据
{
"doc":{
"name":"赵信"
}
}
将“盖伦” 修改名称为“赵信“,成功之后返回
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
还可以用脚本修改:如把年龄增加10岁。
{
"script":{
"lang":"painless",
"inline":"ctx._source.age += 10"
}
}
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age",
"params":{
"age":100
}
}
}
成功返回数据
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
url:http://192.168.1.106:9200/people/man/1 delete方式
删除name为“赵信”的文档,成功返回
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 5,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
url http://192.168.1.106:9200/people/ delete方式
首先初始化一些数据,创建索引book
put http://192.168.1.106:9200/book
{
"settings":{
"number_of_shards": 3,
"number_of_replicas":1
},
"mappings":{
"novel":{
"properties":{
"word_count":{
"type":"integer"
},
"author":{
"type":"keyword"
},
"title":{
"type":"text"
},
"publish_date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
文档数据
{
"author":"罗贯中",
"title":"三国演义",
"word_count":5000,
"publish_date":"1995-01-10"
}
{
"author":"陈寿",
"title":"三国志",
"word_count":10000,
"publish_date":"2000-10-10"
}
{
"author":"杰克",
"title":"Java入门",
"word_count":1000,
"publish_date":"2010-01-10"
}
{
"author":"bob",
"title":"ElasticSearch菜谱",
"word_count":2000,
"publish_date":"2015-05-10"
}
{
"author":"张三",
"title":"ElasticSearch入门",
"word_count":2000,
"publish_date":"2015-05-10"
}
{
"author":"张三",
"title":"ElasticSearch精通",
"word_count":2000,
"publish_date":"2015-05-10"
}
{
"author":"李四",
"title":"ElasticSearch入门精通到放弃",
"word_count":2000,
"publish_date":"2015-05-10"
}
{
"author":"王小波",
"title":"黄金时代",
"word_count":4000,
"publish_date":"2000-07-10"
}
get http://192.168.1.106:9200/book/novel/1
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"author": "罗贯中",
"title": "三国演义",
"word_count": 5000,
"publish_date": "1995-01-10"
}
}
post http://192.168.1.106:9200/book/_search
参数:
{
"query":{
"match_all":{}
}
}
{
"query":{
"match_all":{}
},
"from":1,
"size":1
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "4",
"_score": 1,
"_source": {
"author": "杰克",
"title": "Java入门",
"word_count": 1000,
"publish_date": "2010-01-10"
}
}
]
}
}
post http://192.168.1.106:9200/book/_search
{
"query":{
"match":{
"title":"三国"
}
}
}
显示带“三国”关键字的所有文档记录
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 3.0884533,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "2",
"_score": 3.0884533,
"_source": {
"author": "陈寿",
"title": "三国志",
"word_count": 10000,
"publish_date": "2000-10-10"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1.3097504,
"_source": {
"author": "罗贯中",
"title": "三国演义",
"word_count": 5000,
"publish_date": "1995-01-10"
}
}
]
}
}
{
"query":{
"match":{
"title":"三国"
}
},
"sort":[
{
"publish_date":{
"order":"asc"
}
}
]
}
post http://192.168.1.106:9200/book/_search
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
}
}
}
返回结果:
"aggregations": {
"group_by_word_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2000,
"doc_count": 4
},
{
"key": 1000,
"doc_count": 1
},
{
"key": 4000,
"doc_count": 1
},
{
"key": 5000,
"doc_count": 1
},
{
"key": 10000,
"doc_count": 1
}
]
}
}
多个聚合字段 terms
post http://192.168.1.106:9200/book/_search
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
},
"group_by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
字段统计 stats
post http://192.168.1.106:9200/book/_search
{
"aggs":{
"static_word_count":{
"stats":{
"field":"word_count"
}
}
}
}
返回结果
"aggregations": {
"static_word_count": {
"count": 8,
"min": 1000,
"max": 10000,
"avg": 3500,
"sum": 28000
}
}
还有min,max等等聚合查询
{
"aggs":{
"static_word_count":{
"min":{
"field":"word_count"
}
}
}
}
在查询过程中,除了判断文档是否满足条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和条件查询匹配 有多好,匹配度。
{
"query":{
"match":{
"author":"张三"
}
}
}
{
"query":{
"match_phrase":{
"title":"ElasticSearch入门"
}
}
}
结果
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1.8012037,
"_source": {
"author": "张三",
"title": "ElasticSearch入门",
"word_count": 2000,
"publish_date": "2015-05-10"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 1.147541,
"_source": {
"author": "李四",
"title": "ElasticSearch入门精通到放弃",
"word_count": 2000,
"publish_date": "2015-05-10"
}
}
查询包含author和title都包含的“王小波”
{
"query":{
"multi_match":{
"query":"王小波",
"fields":["author","title"]
}
}
}
结果
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 2.6694732,
"_source": {
"author": "王小波",
"title": "王小波自传",
"word_count": 14000,
"publish_date": "2000-07-10"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.2876821,
"_source": {
"author": "王小波",
"title": "黄金时代",
"word_count": 4000,
"publish_date": "2000-07-10"
}
}
{
"query":{
"term":{
"author":"王五"
}
}
}
Integer类型查询
{
"query":{
"range":{
"word_count":{
"gte":1000,
"lte":2000
}
}
}
}
日期类型查询
{
"query":{
"range":{
"publish_date":{
"gt":"2015-01-01",
"lte":"now"
}
}
}
}
在查询过程中,只判断改文档是否满足条件,只有Yes或者No
{
"query":{
"bool":{
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
结果
{
"_index": "book",
"_type": "novel",
"_id": "4",
"_score": 0,
"_source": {
"author": "杰克",
"title": "Java入门",
"word_count": 1000,
"publish_date": "2010-01-10"
}
}
固定分数查询
{
"query":{
"constant_score":{
"filter":{
"match":{
"title":"ElasticSearch"
}
},
"boost":2
}
}
}
boost:设定指定分数。
布尔查询 或关系查询
查询作者为“张三”或者标题是“ElasticSearch”的文档
{
"query":{
"bool":{
"should":[
{
"match":{
"author":"张三"
}
},
{
"match":{
"title":"ElasticSearch"
}
}
]
}
}
}
{
"query":{
"bool":{
"must":[
{
"match":{
"author":"张三"
}
},
{
"match":{
"title":"ElasticSearch"
}
}
]
}
}
}