回顧
搭建好了elasticsearch集羣,通過elasticsearch-head插件可以監控各節點健康狀態。
通過health命令http://192.168.10.6:9200/_cat/health?v
,可以看出集羣處於健康狀態。
創建索引
我們搭建的elasticsearch集羣,一個master,2個slave。每一個節點的分片數是3個,備份數是1個。
ElasticSearch中的索引:是含有相同屬性的文檔集合。
ElasticSearch中的類型:索引可以定義一個或者多個類型,文檔必須屬於一個類型。比如一本圖書的類型是語文。
ElasticSearch中的文檔:文檔是可以被索引的基本數據單位,比如一本高中人教版語文書。
分片:每個索引都有多個分片,每個分片都是一個Lucene索引。
備份:拷貝一份分片就完成了索引的備份。
下圖中,邊框加粗的分片是主分片,邊框較淺的是備份分片。
ElasticSearch API基本格式:
http://<ip>:<port>/<索引>/<類型>/<文檔id>
索引又分爲結構化索引和非結構化索引。
在head插件,這樣新建索引,就是一個非結構化索引。
非結構索引中的mappings屬性是爲空的,如下圖所示。
基本用法
結構化索引,需要設置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屬性的。
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"
}
}
}
}
}
分頁查詢,在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
鏈接:https://www.jianshu.com/p/242001d5da64
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。