查詢集羣的名字數據庫
⇒ curl -XGET 'http://localhost:9200'
複製代碼
查詢集羣的健康情況bash
⇒ curl -XGET 'http://localhost:9200/_cluster/health?format=yaml'
複製代碼
status字段說明:app
- green 一切正常
- yellow replicas沒有分配[多是隻有單個節點],集羣正常
- red 某些數據取不到 format=yaml指定使用yaml格式輸出,方便查看
獲取集羣的全部索引curl
⇒ curl -XGET 'http://localhost:9200/_cat/indices'
複製代碼
索引的字段ui
⇒ curl -XGET 'http://localhost:9200/mytest/_mapping?format=yaml'
複製代碼
結果url
mytest:
mappings:
external:
properties:
addre:
type: "string"
name:
type: "string"
複製代碼
它相似於數據庫的schema,描述文檔可能具備的字段或屬性、每一個字段的數據類型。spa
字段對於非string類型,通常只須要設置type。string域兩重要屬性 index analyzercode
indexorm
1. analyzed 全文索引這個域。首先分析字符串,而後索引
2. not_analyzed 精確索引 ,不分析
3. no 此域不會被搜索
複製代碼
analyzer索引
將文本分紅四核倒排索引的獨立詞條,後將詞條統一化提升可搜索性
複製代碼
動態映射: 文檔中出現以前從未遇到過的字段,動態肯定數據類型,並自動把新的字段添加到類型映射
⇒ curl -XPUT 'localhost:9200/mytest'
複製代碼
⇒ curl -XDELETE 'localhost:9200/mytest?format=yaml'
複製代碼
插入單條數據
⇒ curl -XPUT 'localhost:9200/mytest/external/1?format=yaml' -d ' quote> { "name":"paxi"}'
複製代碼
查詢單條數據
⇒ curl -XGET 'localhost:9200/mytest/external/1?format=yaml'
複製代碼
刪除單條數據
curl -XDELETE 'localhost:9200/mytest/external/3?format=yaml'
複製代碼
curl -XGET 'localhost:9200/_analyze?format=yaml' -d ' {"papa xixi write"}'
複製代碼
結果爲
tokens:
- token: "papa"
start_offset: 3
end_offset: 7
type: "<ALPHANUM>"
position: 1
- token: "xixi"
start_offset: 8
end_offset: 12
type: "<ALPHANUM>"
position: 2
- token: "write"
start_offset: 13
end_offset: 18
type: "<ALPHANUM>"
position: 3
複製代碼
token 表示實際存儲的詞條,position表示詞條在原始文本中的位置。
能夠看出完整的文本會被切割存儲成不一樣的詞條
curl -XGET 'localhost:9200/mytest/_search?filter_path=hits.hits._source&format=yaml' -d ' { "query":{"match":{"name":"papa xixi write"}}}'
複製代碼
低版本沒法生效
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"match":{"name":"papa xixi write"}},"_source":["name"]}'
複製代碼
低版本無效,能夠用通配符
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"match":{"name":"papa xixi write"}}}'
複製代碼
查詢匹配的結果以下
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.22545706
_source:
name: "papa xixi"
- _index: "mytest"
_type: "external"
_id: "2"
_score: 0.12845722
_source:
name: "papa"
- _index: "mytest"
_type: "external"
_id: "10"
_score: 0.021688733
_source:
name: "xixi"
複製代碼
從查詢結果,它獲取到了全部包含 papa 、 xixi和write 的詞,至關於將原來的詞拆開,而後兩個單詞作了 OR 操做,若是要所有匹配,可使用AND操做
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"match":{"name":{"query":"papa xixi write","operator":"and"}}}}'
---
hits:
total: 1
max_score: 0.6532502
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
複製代碼
若是隻是想提升精度
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"match":{"name":{"query":"papa xixi write","minimum_should_match":"75%"}}}}'
---
hits:
total: 2
max_score: 0.6532502
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.22545706
_source:
name: "papa xixi"
複製代碼
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"term":{"name":"papa xixi write"}}}'
複製代碼
它的結果是什麼也沒有查到
total: 0
max_score: null
hits: []
複製代碼
換用查詢語句
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"term":{"name":"papa"}}}'
複製代碼
結果爲
hits:
- _index: "mytest"
_type: "external"
_id: "2"
_score: 1.0
_source:
name: "papa"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.37158427
_source:
name: "papa xixi"
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.2972674
_source:
name: "papa xixi write"
複製代碼
match 若是在全文字段上查詢,會使用正確的分析器分析查詢字符串;若是精確值字段使用,會精確匹配。 term精確匹配,只要包含了對應的文本就能夠,不對文本分析(not_analyzed文本會精確匹配,terms 多個值只要有一個匹配就匹配);
從"papa xixi write"的存儲文本分析來看,它自己會被切割成不一樣的詞條,因此用 term查詢"papa xixi write",沒法獲取到結果,可是match確可以匹配
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"filtered":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
複製代碼
或者
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"constant_score":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
複製代碼
⇒ curl -XGET 'localhost:9200/_validate/query?explain&format=yaml' -d '{ "query":{{"filter":{"range":{"name":{"gt":"w"}}}}}'
---
valid: false
//緣由省略
複製代碼
使用term查詢
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { "query":{"term":{"addre":"beijing"}}}'
複製代碼
結果爲
hits:
- _index: "mytest"
_type: "external"
_id: "5"
_score: 0.30685282
_source:
addre: "beijing"
- _index: "mytest"
_type: "external"
_id: "6"
_score: 0.30685282
_source:
addre: "beijing"
name: "px"
複製代碼
轉換爲bool查詢,結果同樣
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { query:{bool:{must:{match:{addre:"beijing"}}}}}'
複製代碼
若是隻想要最後一條
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { query:{bool:{must:{match:{addre:"beijing"}},must:{match:{name:"px"}}}}}'
複製代碼
想要第一條
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { query:{bool:{must:{match:{addre:"beijing"}},must_not:{match:{name:"px"}}}}}'
複製代碼
都想要
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d ' { query:{bool:{must:{match:{addre:"beijing"}},should:{match:{name:"px"}}}}}'
複製代碼
must的意思是當前值必須是有的,must_not必須沒有,should表示數據能夠有也能夠沒有