elastcisearch5.1.1使用

2018-12-06  本文已影响0人  味道_cf3b

---对应关系

elastcisearch             mysql

index(索引)                  数据库

type(类型)                  表

documents(文档)         行

fields                             列

---RESTful常用接口

GET/POST/PUT/DELETE

=======================

***新建索引

---shards(分片)设置后不能修改,replicas(副本)

put my-index

{"setting":{"index":{"number_of_shards":5,"number_of_replicas":1}}}

--- index/type/id创建方式

put my-index/test/1

{"name":"demo"}

***获取setting

get my-index/_setting

get .kibana,my-index/_setting

get _all/_setting

get _setting

***更新

---覆盖式

PUT my-index/test/1

{"name":"demo","age":"18"}

---字段式

GET my-index/test/1/_update

{"doc":{"phone":"90980"}}

***获取索引

---GET or POST

GET _all

GET my-index

GET my-index/test/1

---获取某个字段

GET my-index/test/1?_source=name,age

GET my-index/test/1?_source=name

GET my-index/test/1?_source

***删除

---id

DELETE my-index/test/1

---index

DELETE my-index

***批量操作

---_mget

GET _mget

{"docs":[{"_index":"my-index","_type":"test","_id":1},{"_index":"my-index","_type":"test2","_id":1}]}

GET my-index/_mget

{"docs":[{"_type":"test","_id":1},{"_type":"test2","_id":1}]}

GET my-index/test/_mget

{"docs":[{"_id":1},{"_id":1}]}

GET my-index/test/_mget

{"ids":[1,2]}

---bulk

POST _bulk

{"index":{"_index":"my-index", "_type":"test", "_id":1}}

{"name":"demo", "age":"18"}

{"index":{"_index":"my-index", "_type":"test", "_id":2}}

{"name":"demo2", "age":"19"}

{"delete":{"_index":"my-index", "_type":"test", "_id":1}}

{"update":{"_index":"my-index", "_type":"test", "_id":2}}

{"doc":{"phone":"12580"}}

{"create":{"_index":"my-index", "_type":"test", "_id":3}}

{"name":"demo3", "age":"18"}

***映射

---内置类型

string类型、数字类型、日期类型、bool类型、binary类型、复制类型、geo类型、专业类型

---

store(all)、index(string)、null_value(all)、analyzer(all)、include_in_all(all)、format(date)

---新建映射

PUT my-index

{"mappings":{"test":{"properties":{

"name":{"store":true,"type":"text","analyzer":"ik_max_word"},

"age":{"type":"integer"},"phone":{"store":true,"type":"keyword"},

"birth":{"type":"date","format":"yyyy-MM-dd"},

"addr":{"properties":{"province":{"type":"text"},"city":{"type":"text"}}}

}}}}

---获取映射

GET my-index/_mapping/test

GET _all/_mapping/test

***查询

---基本查询、组合查询、过滤

---match:会分词

GET my-index/test/_search

{"query":{"match":{"name":"demo"}}}

---term:不会分词

GET my-index/_search

{"query":{"term":{"name":"demo"}}}

---terms:

GET my-index/_search

{"query":{"terms:{"name":["demo","demo2"]}}}

---控制返回数量:from(从第几个开始),size(取多少条数据)

GET my-index/test/_search

{"query":{"match":{"name":"demo"}},"from":1,"size":2}

---match_all

GET my-index/test/_search

{"query":{"match_all":{}}}

---match_phrase(短语查询):demo拆分后若为demo和名,则需要满足demo与名都存在则返回,slop是拆词后最小距离

GET my-index/test/_search

{"query":{"match_phrase":{"name":{"query":"demo名","slop":6}}}}

---multi_match(多字段查询):

GET my-index/test/_search

{"query":{"multi_match":{"query":"demo","fields":["name","addr"]}}}

---指定返回字段:

GET my-index/_search

{"stored_fields":["name","age"],"query":{"match":{"name":"demo"}}}

---排序asc(升序)/desc(降序)

GET my-index/_search

{"sort":[{"age":{"order":"asc"}}],"query":{"match":{"name":"demo"}}}

---范围查询(range):gte(>=)/gt(>)lte(<=),boost权重

GET my-index/_search

{"query":{"range":{"age":{"gte":18,"lte":30,"boost":2.0}}}}

GET my-index/_search

{"query":{"range":{"birth":{"gte":"208-1-1","lte":"now","boost":2.0}}}}

---wildcard查询:模糊

GET my-index/_search

{"query":{"wildcard":{"name":{"value":"demo*","boost":2.0}}}}

---组合查询

---bool查询:filtered已被bool替换,must/should/must_not/filter

---fliter:过滤,must:所有必须满足,should:至少满足一个,must_not:一个都不满足

---fliter:

=查询年龄为20

GET my-index/_search

{"query":{"bool":{"must":{"match_all":{}},"filter":{"term":{"age":20}}}}}

=查询年龄为10与20

GET my-index/_search

{"query":{"bool":{"must":{"match_all":{}},"filter":{"terms":{"age":[18,20]}}}}}

---查看分析器结果

GET _analyze

{"analyzer": "ik_max_word","text": "demo12中文"}

=查询姓名为demo或者联系方式为12580,排除年龄为20

GET my-index/_search

{"query":{"bool":{"should":{{"term":{"name":"demo"}},{"term":{"phone":"12580"}}},"must_not":{{"term":{"age":20}}}}}}

===查询name=demo 或者 (age=20并且phone=12580)

GET my-index/_search

{"query":{"bool":{"should":[{"term":{"name":"demo"}},{

"bool":{"must":[{"term":{"age":20}},{"term":{"phone":"12580"}}]}}]}}}

===查询不为null

GET my-index/_search

{"query":{"bool":{"filter":{"exists"{"field":"addr"}}}}}

ok,接下来可以结合语言使用了。可以查看官方文档,但是要对应版本。

上一篇下一篇

猜你喜欢

热点阅读