直接點選Dev Tools,來看基本操作
1,輸入:GET /
{
"name": "GddjX_V",
"cluster_name": "elasticsearch",
"cluster_uuid": "Z4oj43mcQKGiMF70lFDv-Q",
"version": {
"number": "6.3.2",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "053779d",
"build_date": "2018-07-20T05:20:23.451332Z",
"build_snapshot": false,
"lucene_version": "7.3.1",
"minimum_wire_compatibility_version": "5.6.0",
"minimum_index_compatibility_version": "5.0.0"
},
"tagline": "You Know, for Search"
}
在右側将看到和啟動完ES後在浏覽器輸入localhost:9200相同的内容
2,建立索引
輸入:
PUT mytest
#或者直接插入一條資料
POST mytest/doc/1
{
"name": "zs",
"age": 11,
"state": 1
}
輸出:
{
"_index": "mytest",
"_type": "doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
3,檢視剛才建立的索引
輸入:
GET mytest/doc/1
輸出
{
"_index": "mytest",
"_type": "doc",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "zs",
"age": 22,
"state": 1
}
}
index是剛才建立的索引名稱;_type是類型,_id為建立時的ID,如果建立索引的時候不設定ID,那麼ES将預設配置設定一個ID,不過樣式會比較長,不好記憶;_version為版本号,如果我們之後對該資料進行了修改,那麼他會随之變化;_source裡邊就是我們剛才加進去的資料内容
4,删除索引
輸入:
DELETE mytest
5,修改資料
輸入:
PUT mytest/doc/1
{
"name": "zsname",
"age": 33,
"state": 1
}
6,bulk方法批量插入資料
輸入:
POST _bulk
{"index":{"_index":"mytest","_type":"doc"}}
{"name": "zsname","age": 11,"state": 1}
{"index":{"_index":"mytest","_type":"doc"}}
{"name": "lisi","age": 22,"state": 1}
{"index":{"_index":"mytest","_type":"doc"}}
{"name": "wangwu","age": 33,"state": 0}
使用POST方法,然後每一條資料的格式是一緻的,首先第一行輸入
{"index":{"_index":"mytest","_type":"doc"}}
第二行輸入要插入的完整資料,這裡特别提醒下,插入的這條資料不能使用剛才建立資料時的那種多行形式,隻能使用沒有回車的一條資料,否則會報錯
執行完畢後,我們再次擷取資料看一下
GET mytest/_search
7,按照條件查詢
輸入: 查詢名字為zsname的資訊
get mytest/_search
{
"query":{
"match":{
"name":"zsname"
}
}
}
bool聯合查詢: must,should,must_not
聯合查詢就會使用到must,should,must_not三種關鍵詞。
這三個可以這麼了解
must: 完全比對條件 相當于sql中的and
should: 至少滿足一個條件 相當于sql中的 or
must_not: 文檔必須不比對條件 相當于sql中的!=
8,當同一個屬性滿足邏輯或時的查詢
輸入:
GET mytest/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "zsname"
}
},
{
"match": {
"name": "lisi"
}
}
]
}
}
}
這裡是查詢屬性"name"等于zsname或者lisi的資料 ,query bool should 組成了 或 的關系
9,多條件查詢
輸入:
GET mytest/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "zsname"
}
},
{
"match": {
"age": 11
}
}
]
}
}
}
這裡是查詢屬性"name"等于zsname,并且屬性"age"為11 的資料, query bool must 組合成了 且 的關系,跟上面的差別 就是 should和must的差別
10,範圍查詢并進行排序
輸入:
GET mytest/_search
{
"query": {
"range": {
"age": {
"gte": 1,
"lte": 20
}
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
年齡 1-20的結果,按照age升序排列
注意排序的字段要是數字,字元串會報如下錯誤 。
Fielddata is disabled on text fields by default. Set fielddata=true on [name]
in order to load fielddata in memory by uninverting the inverted index.
Note that this can however use significant memory. Alternatively use a keyword field instead.
原因分析
Elasticsearch 5.x版本以後,對排序和聚合等操作,用單獨的資料結構(fielddata)緩存到記憶體裡了,預設是不開啟的,需要單獨開啟。
參考:https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
解決方案
執行如下語句,将 age字段進行開啟映射mapping
PUT 索引名字/_mapping/類型/
PUT mytest/_mapping/doc/
{
"properties":{
"age":{
"type":"text",
"fielddata":true
}
}
}
有時排序會遇到這個錯誤:
No mapping found for [create_time] in order to sort on
解決方法:sort的時候 強制指定 sort 字段類型
"sort":{
"create_time":{
"order":"desc",
"unmapped_type": "long"
}
}
11, SQL查詢
POST /_xpack/sql?format=json
{
"query": "SELECT * FROM live_msg limit 1"
}
12,聚合查詢
輸入:
GET mytest/_search
{
"size": 0,
"aggs": {
"state": {
"range": {
"field": "state",
"ranges": [
{
"from": 1,
"to": 5
},
{
"from": 5,
"to": 15
}
]
}
}
}
}
field後邊輸入屬性名
from和to後邊輸入要分段的範圍
如果想要檢視滿足條件的資料,size值置為非零數
查詢state的統計資訊 stats 統計count max min avg sum 5個值
GET mytest/_search
{
"size": 2,
"aggs": {
"statsState": {
"stats": {
"field": "state"
}
}
}
}
12、複雜的組合查詢
查詢某個字段為空 或者 空字元串:
{
"query": {
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"wildcard": {
"字段名": {
"value": "*"
}
}
}
]
}
}
]
}
}
}
term 是代表完全比對,也就是精确查詢,搜尋前不會再對搜尋詞進行分詞拆解。相當于sql中的 =
terms 是代表完全比對,相當于sql中的in
match 進行搜尋的時候,會先進行分詞拆分,拆完後,再來比對 相當于sql中的like,比like更強大(分詞後like)
match_phrase 稱為短語搜尋,要求所有的分詞必須同時出現在文檔中,同時位置必須緊鄰一緻 相當于sql中的like,比like更強大(分詞後like)
查詢 age=1 and name!="" and name!="zs" 的資料
POST mytest/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"age": 11
}
},
{
"bool": {
"must_not": [
{
"term": {
"name": ""
}
}
]
}
},
{
"bool": {
"must_not": [
{
"term": {
"name": "zs"
}
}
]
}
}
]
}
}
}
實作如下sql:
SELECT id,create_time FROM live_msg WHERE
l_id = 48
AND content like "%美股%"
AND kind IN ('charge', 'free')
AND state IN ('', 'del_own')
AND unshow_group = ''
AND view_self != 'yes'
AND (not_vip_show != 'no' or (create_time> 1588814979 AND vip_show != 'no'))
ORDER BY create_time DESC LIMIT 5;
ES文法
GET live_msg/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"l_id": 48
}
},
{
"match_phrase": {
"content": "美股"
}
},
{
"terms": {
"kind": [
"charge",
"free"
]
}
},
{
"bool": {
"should": [
{
"bool": {
"must_not": {
"wildcard": {
"state": {
"value": "*"
}
}
}
}
},
{
"term": {
"state": "del_own"
}
}
]
}
},
{
"bool": {
"must_not": [
{
"wildcard": {
"unshow_group": {
"value": "*"
}
}
},
{
"term": {
"view_self": "yes"
}
}
]
}
},
{
"bool": {
"should": [
{
"bool": {
"must_not": {
"term": {
"not_vip_show": "no"
}
}
}
},
{
"bool": {
"must": [
{
"range": {
"create_time": {
"gt": 1588814979
}
}
},
{
"bool": {
"must_not": {
"term": {
"vip_show": "no"
}
}
}
}
]
}
}
]
}
}
]
}
},
"from": 0,
"size": 5,
"sort": {
"create_time": {
"order": "desc",
"unmapped_type": "long"
}
},
"_source":["id","create_time"]
}