ElasticSearch系列文章:
1.蛋疼的ElasticSearch(一)之安装ElasticSearch
2.蛋疼的ElasticSearch(二)之配置ElasticSearch Head插件
3.蛋疼的ElasticSearch(三)之配置elasticsearch-analysis-ik和集群
回顾
搭建好了elasticsearch集群,通过elasticsearch-head插件可以监控各节点健康状态。
通过health命令http://192.168.10.6:9200/_cat/health?v
,可以看出集群处于健康状态。
创建索引
我们搭建的elasticsearch集群,一个master,2个slave。每一个节点的分片数是3个,备份数是1个。
image.png
ElasticSearch中的索引:是含有相同属性的文档集合。
ElasticSearch中的类型:索引可以定义一个或者多个类型,文档必须属于一个类型。比如一本图书的类型是语文。
ElasticSearch中的文档:文档是可以被索引的基本数据单位,比如一本高中人教版语文书。
分片:每个索引都有多个分片,每个分片都是一个Lucene索引。
备份:拷贝一份分片就完成了索引的备份。
下图中,边框加粗的分片是主分片,边框较浅的是备份分片。
image.png
ElasticSearch API基本格式:
http://<ip>:<port>/<索引>/<类型>/<文档id>
索引又分为结构化索引和非结构化索引。
在head插件,这样新建索引,就是一个非结构化索引。
image.png
非结构索引中的mappings属性是为空的,如下图所示。
image.png
基本用法
结构化索引,需要设置mappings属性的。ElasticSearch 6.0.0或者更高版本中创建的索引只能包含一个mapping。
结构化索引,类似MySQL,我们会对索引结构做预定义,包含字段名,字段类型。那么非结构化索引,就类似Mongo DB,索引结构未知。在根据具体的数据来update索引的mapping,结构化相比于非结构化,性能更好。非结构化比较灵活,只是频繁update索引mapping会有一定的性能损耗。
接下来,我们来创建一个结构化索引,索引名为people。
PUT 192.168.10.6:9200/pepole
{
"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"
}
}
}
}
}
我们可以看到people索引是有mappings属性的。
image.png
people索引插入数据,指定文档id
POST 192.168.10.6:9200/pepole/man/2
{
"name":"12345",
"country":"guangzhou",
"age":23,
"date":"1995-06-06"
}
people索引插入数据,不指定文档id。elasticsearch会自动帮你创建一个文档id。
POST 192.168.10.6:9200/pepole/man/
{
"name":"dada",
"country":"guangzhou",
"age":30,
"date":"1987-06-06"
}
通过文档形式修改数据。
POST 192.168.10.6:9200/pepole/man/1/_update
{
"doc":{
"name":"123"
}
}
通过脚本语言修改数据,painless是elasticsearch自带的脚本。
POST 192.168.10.6:9200/pepole/man/1/_update
{
"script": {
"lang":"painless",
"inline":"ctx._source.age+=10"
}
}
或者
{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age",
"params":{
"age":100
}
}
}
删除数据
DELETE 192.168.10.6:9200/pepole/man/1/
Query查询
在elasticsearch中查询,可以分为子条件查询和复合条件查询。
子条件查询:特定字段查询所特定值,子条件查询又可细分为Query Context和Filter Context。
复合条件查询:以一定的逻辑组合子条件查询。
Query Context:在查询过程中,除了判断文档是否满足查询条件外,es还会计算出一个_score来标识匹配的程序,旨在判断目标文档和查询条件匹配的有多好。
常用查询又分为全文本查询、字段级别查询。全文本查询是针对文本类型数据。字段级别查询是针对结构化数据,比如数字,日期。
我们按照上文的步骤,继续创建一个名为book的索引,mapping类型为novel。
PUT 192.168.10.6:9200/book
{
"settings" : {
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"work_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"
}
}
}
}
}
image.png
分页查询,在es中,from是从0开始的。
POST 192.168.10.6:9200/book/_search
{
"query":{
"match_all":{}
},
"from":2,
"size":1
}
返回:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
}
]
}
}
按字段模糊匹配并按字段排序。因为我们指定了排序方式,_score字段都为null。
POST 192.168.10.6:9200/book/_search
{
"query":{
"match":{
"title":"叫"
}
},
"sort":[
{
"publish_date": {
"order":"desc"
}
}
]
}
返回:
{
"took": 199,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": null,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "eA5qn2YBHFER5MTJMoKT",
"_score": null,
"_source": {
"title": "叫你玩辅助",
"author": "卷毛",
"word_count": 5000,
"publish_date": "1997-03-01"
},
"sort": [
857174400000
]
},
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": null,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
},
"sort": [
854755200000
]
},
{
"_index": "book",
"_type": "novel",
"_id": "dg5pn2YBHFER5MTJnYLt",
"_score": null,
"_source": {
"title": "叫你玩打野",
"author": "厂长",
"word_count": 3000,
"publish_date": "1996-02-01"
},
"sort": [
823132800000
]
},
{
"_index": "book",
"_type": "novel",
"_id": "dQ5pn2YBHFER5MTJaoLT",
"_score": null,
"_source": {
"title": "叫你玩中单",
"author": "若风",
"word_count": 2000,
"publish_date": "1995-02-01"
},
"sort": [
791596800000
]
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": null,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
},
"sort": [
788918400000
]
}
]
}
}
按字段聚合查询。
POST 192.168.10.6:9200/book/_search
{
"aggs":{
"group_by_word_count": {
"terms": {
"field":"word_count"
}
},
"group_by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
返回:
{
"took": 387,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": 1,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eA5qn2YBHFER5MTJMoKT",
"_score": 1,
"_source": {
"title": "叫你玩辅助",
"author": "卷毛",
"word_count": 5000,
"publish_date": "1997-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dg5pn2YBHFER5MTJnYLt",
"_score": 1,
"_source": {
"title": "叫你玩打野",
"author": "厂长",
"word_count": 3000,
"publish_date": "1996-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dQ5pn2YBHFER5MTJaoLT",
"_score": 1,
"_source": {
"title": "叫你玩中单",
"author": "若风",
"word_count": 2000,
"publish_date": "1995-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
},
"aggregations": {
"group_by_publish_date": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 825638400000,
"key_as_string": "1996-03-01 00:00:00",
"doc_count": 2
},
{
"key": 788918400000,
"key_as_string": "1995-01-01 00:00:00",
"doc_count": 1
},
{
"key": 791596800000,
"key_as_string": "1995-02-01 00:00:00",
"doc_count": 1
},
{
"key": 794016000000,
"key_as_string": "1995-03-01 00:00:00",
"doc_count": 1
},
{
"key": 823132800000,
"key_as_string": "1996-02-01 00:00:00",
"doc_count": 1
},
{
"key": 854755200000,
"key_as_string": "1997-02-01 00:00:00",
"doc_count": 1
},
{
"key": 857174400000,
"key_as_string": "1997-03-01 00:00:00",
"doc_count": 1
},
{
"key": 1540166400000,
"key_as_string": "2018-10-22 00:00:00",
"doc_count": 1
},
{
"key": 1540339200000,
"key_as_string": "2018-10-24 00:00:00",
"doc_count": 1
}
]
},
"group_by_word_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1000,
"doc_count": 2
},
{
"key": 0,
"doc_count": 1
},
{
"key": 1234,
"doc_count": 1
},
{
"key": 2000,
"doc_count": 1
},
{
"key": 3000,
"doc_count": 1
},
{
"key": 4000,
"doc_count": 1
},
{
"key": 5000,
"doc_count": 1
},
{
"key": 6666,
"doc_count": 1
},
{
"key": 8888,
"doc_count": 1
}
]
}
}
}
全文本匹配查询。
POST 192.168.10.6:9200/book/_search
{
"query": {
"match_phrase": {
"title":"uzi的坑锅之路"
}
}
}
返回:
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 6.7130113,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 6.7130113,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
}
]
}
}
字段统计。
POST 192.168.10.6:9200/book/_search
{
"aggs":{
"grades_word_count": {
"stats": {
"field":"word_count"
}
}
}
}
返回:
{
"took": 47,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": 1,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eA5qn2YBHFER5MTJMoKT",
"_score": 1,
"_source": {
"title": "叫你玩辅助",
"author": "卷毛",
"word_count": 5000,
"publish_date": "1997-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dg5pn2YBHFER5MTJnYLt",
"_score": 1,
"_source": {
"title": "叫你玩打野",
"author": "厂长",
"word_count": 3000,
"publish_date": "1996-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dQ5pn2YBHFER5MTJaoLT",
"_score": 1,
"_source": {
"title": "叫你玩中单",
"author": "若风",
"word_count": 2000,
"publish_date": "1995-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
},
"aggregations": {
"grades_word_count": {
"count": 10,
"min": 0,
"max": 8888,
"avg": 3278.8,
"sum": 32788
}
}
}
多文本匹配查询。
POST 192.168.10.6:9200/book/_search
{
"query": {
"multi_match": {
"query":"uzi",
"fields":["title", "author"]
}
}
}
返回:
{
"took": 56,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.2039728,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1.2039728,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1.2039728,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1.0594962,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 0.6099695,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
}
]
}
}
多文本匹配查询
POST http://192.168.10.6:9200/book/_search
{
"query":{
"query_string": {
"query":"uzi OR UZI OR 微笑",
"fields":["title","author"]
}
}
}
返回:
{
"took": 98,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 3.1784885,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 3.1784885,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 2.4079456,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1.8299085,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": 1.2039728,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1.2039728,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
}
]
}
}
字段级别查询,keyword是关键字不可切分,是全匹配的。
POST http://192.168.10.6:9200/book/_search
{
"query": {
"term": {
"author":"uzi"
}
}
}
返回:
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.2039728,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1.2039728,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1.2039728,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
}
]
}
}
范围查询,gte和lte是闭区间。gt和lt是开区间。
POST http://192.168.10.6:9200/book/_search
{
"query": {
"range": {
"word_count": {
"gte":1000,
"lte":2000
}
}
}
}
或者
{
"query": {
"range": {
"word_count": {
"from":0,
"to":1000
}
}
}
}
返回:
{
"took": 35,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dQ5pn2YBHFER5MTJaoLT",
"_score": 1,
"_source": {
"title": "叫你玩中单",
"author": "若风",
"word_count": 2000,
"publish_date": "1995-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
}
}
时间范围查询
POST http://192.168.10.6:9200/book/_search
{
"query": {
"range": {
"publish_date": {
"gte":"1997-01-01",
"lte":"now"
}
}
}
}
返回:
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": 1,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eA5qn2YBHFER5MTJMoKT",
"_score": 1,
"_source": {
"title": "叫你玩辅助",
"author": "卷毛",
"word_count": 5000,
"publish_date": "1997-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
}
}
Filter查询
Filter查询是在查询过程中,只判断该文档是否满足条件,只有YES或者No。
POST http://192.168.10.6:9200/book/_search
{
"query": {
"bool": {
"filter": {
"term": {
"word_count":1000
}
}
}
}
}
返回:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 0,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 0,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
}
}
]
}
}
固定分数查询
POST http://192.168.10.6:9200/book/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"title":"uzi欠香锅一个世界冠军"
}
}
}
}
}
返回:
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
}
}
固定分数查询,指定boost
POST http://192.168.10.6:9200/book/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"title":"uzi欠香锅一个世界冠军"
}
},
"boost":40000
}
}
}
返回:
{
"took": 56,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 40000,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 40000,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 40000,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 40000,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 40000,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
}
}
复杂查询
should,可以满足的条件。
POST http://192.168.10.6:9200/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"author":"uzi"
}
},
{
"match": {
"title":"香锅"
}
}
]
}
}
}
返回:
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.89712,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1.89712,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1.5749675,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 1.2039728,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
}
]
}
}
must,必须要满足的条件
POST http://192.168.10.6:9200/_search
{
"query": {
"bool": {
"must":[
{
"match": {
"title":"uzi"
}
},
{
"match": {
"author":"uzi"
}
}
]
}
}
}
返回:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.4079456,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 2.4079456,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
}
]
}
}
must + filter混合查询
POST http://192.168.10.6:9200/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title":"uzi"
}
}
],
"filter":[
{
"term": {
"word_count":1000
}
}
]
}
}
}
返回:
{
"took": 46,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.2039728,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "ew57n2YBHFER5MTJt4Ky",
"_score": 1.2039728,
"_source": {
"title": "UZI的坑锅之路",
"author": "uzi",
"word_count": 1000,
"publish_date": "1996-03-01"
}
}
]
}
}
must_not 必须不满足的条件。
POST http://192.168.10.6:9200/_search
{
"query": {
"bool": {
"must_not": {
"term": {
"author":"uzi"
}
}
}
}
}
返回:
{
"took": 39,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "dw5pn2YBHFER5MTJ3oIc",
"_score": 1,
"_source": {
"title": "叫你玩ADC",
"author": "微笑",
"word_count": 4000,
"publish_date": "1997-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eA5qn2YBHFER5MTJMoKT",
"_score": 1,
"_source": {
"title": "叫你玩辅助",
"author": "卷毛",
"word_count": 5000,
"publish_date": "1997-03-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "eg5rn2YBHFER5MTJQYIz",
"_score": 1,
"_source": {
"title": "教你怎么做麻辣香锅",
"author": "香锅",
"word_count": 8888,
"publish_date": "1996-03-01"
}
},
{
"_index": "pepole",
"_type": "man",
"_id": "2",
"_score": 1,
"_source": {
"name": "12345",
"country": "guangzhou",
"age": 23,
"date": "1995-06-06"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dg5pn2YBHFER5MTJnYLt",
"_score": 1,
"_source": {
"title": "叫你玩打野",
"author": "厂长",
"word_count": 3000,
"publish_date": "1996-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "vxAspmYBI-Y9epepwYvX",
"_score": 1,
"_source": {
"title": "微博热点-uzi不配洗白+2",
"author": "知名博主",
"word_count": "0",
"publish_date": 1540339200000
}
},
{
"_index": "pepole",
"_type": "man",
"_id": "dA4En2YBHFER5MTJ_oKo",
"_score": 1,
"_source": {
"name": "dada",
"country": "guangzhou",
"age": 30,
"date": "1987-06-06"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"title": "叫你玩上单",
"author": "草莓",
"word_count": 1000,
"publish_date": "1995-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "dQ5pn2YBHFER5MTJaoLT",
"_score": 1,
"_source": {
"title": "叫你玩中单",
"author": "若风",
"word_count": 2000,
"publish_date": "1995-02-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1jZJpWYBKYrkmyVA-wAp",
"_score": 1,
"_source": {
"title": "微博热点-uzi是弟弟",
"author": "微博",
"word_count": "1234",
"publish_date": 1540166400000
}
}
]
}
}
分页+范围+全文本查询
POST 192.168.10.6:9200/_search
{
"from":0,
"size":10,
"query":{
"bool":{
"must":[
{
"match":{
"author":{
"query":"uzi"
}
}
},
{
"match":{
"title":{
"query":"如"
}
}
}
],
"filter":[
{
"range":{
"word_count":{
"from":0,
"to":10000
}
}
}
]
}
}
}
返回:
{
"took": 56,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.4079456,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "eQ5qn2YBHFER5MTJi4LZ",
"_score": 2.4079456,
"_source": {
"title": "教你如何自闭",
"author": "uzi",
"word_count": 6666,
"publish_date": "1995-03-01"
}
}
]
}
}
尾言
大家好,我是cmazxiaoma(寓意是沉梦昂志的小马),感谢各位阅读本文章。
小弟不才。
如果您对这篇文章有什么意见或者错误需要改进的地方,欢迎与我讨论。
如果您觉得还不错的话,希望你们可以点个赞。
希望我的文章对你能有所帮助。
有什么意见、见解或疑惑,欢迎留言讨论。
最后送上:心之所向,素履以往。生如逆旅,一苇以航。
saoqi.png
网友评论