1、简介
Elastic 官方网站
Elastic 中文社区
Elasticsearch: 权威指南
2、使用
2.1、创建索引

{"aliases": {"customer_remind_alias_sit": {},"customer_remind_alias_sst": {},"customer_remind_alias_test": {}},"mappings": {"properties": {"remindRuleId": {"type": "keyword"},"remindDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"carIdOwnOrg": {"type": "keyword"},"wechatFollower": {"type": "keyword"},"wechatSendState": {"type": "keyword"},"wechatSendDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"phoneSendState": {"type": "keyword"},"phoneSendDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"smsSendState": {"type": "keyword"},"smsSendDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"couponSendState": {"type": "keyword"},"picSendState": {"type": "keyword"},"operatorId": {"type": "keyword"},"state": {"type": "keyword"},"isDeleted": {"type": "keyword"},"operatorProcessState": {"type": "keyword"},"consumeAgainState": {"type": "keyword"},"consumeAgainDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"finishTime": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"customerName": {"type": "text","fields": {"raw": {"type": "keyword"}},"analyzer": "ngram_10_analyzer"},"customerCellPhone": {"type": "text","analyzer": "ngram_11_analyzer"},"fullCarNo": {"type": "text","fields": {"raw": {"type": "keyword"}},"analyzer": "ngram_8_analyzer"},"remindType": {"type": "keyword"},"idOwnOrg": {"type": "keyword"},"creationtime": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"customerId": {"type": "keyword"},"carId": {"type": "keyword"},"groupId": {"type": "keyword"},"lastServiceDate": {"type": "date","ignore_malformed": true,"format": "yyyy-MM-dd"},"lastServiceOrg": {"type": "keyword"},"operatorSendState": {"type": "keyword"},"pkId": {"type": "keyword"},"sourceType": {"type": "keyword"},"lastInsuranceCompanyId ": {"type": "keyword"},"lastInsuranceOrgId ": {"type": "keyword"},"car_exclusive_consultants_id": {"type": "keyword"}}},"settings": {"index": {"max_ngram_diff": "19","analysis": {"analyzer": {"ngram_10_analyzer": {"tokenizer": "ngram_10_tokenizer"},"ngram_11_analyzer": {"tokenizer": "ngram_11_tokenizer"},"ngram_8_analyzer": {"tokenizer": "ngram_8_tokenizer"}},"tokenizer": {"ngram_10_tokenizer": {"type": "ngram","min_gram": "1","max_gram": "20"},"ngram_11_tokenizer": {"type": "ngram","min_gram": "1","max_gram": "11"},"ngram_8_tokenizer": {"type": "ngram","min_gram": "1","max_gram": "8"}}}}}}
2.2、状态查询
2.2.1、集群的健康状态
GET _cat/health
2.2.2、节点状态
GET _cat/nodes
2.2.3、 查看所有索引
GET _cat/indices
2.3、常用查询
2.3.1、查询数量
- track_total_hits=true不然超过1万,数量只给10000
- customer_remind为索引名称
- 限制size=0,不反回结果,只返回数量
- should 相当于mysql中的 or
- filter 过滤 可以理解成mysql中的 and
- term 精确匹配 相当于mysql中的 =
terms 相当于mysql中的 in
POST customer_remind/_search?track_total_hits=true{"query": {"bool": {"should": [{"terms": {"carIdOwnOrg": ["15519724471253495810","15519724471253495811"]}},{"terms": {"idOwnOrg": ["15519724471253495810"]}}],"filter": [{"terms": {"state": ["0","2"]}},{"term": {"groupId": "15519724471253495812"}}]}},"size": 0}
2.3.2、查询列表
customer_remind为索引名称
- must 必须匹配
- multi_match 多个字段模糊查询
- query对应查询的关键词
- from、size对应分页参数
sort 排序参数
POST customer_remind/_search{"query": {"bool": {"must": [{"multi_match": {"query": "16605115059","analyzer": "ik_smart","type": "best_fields","operator": "and","fields": ["customerName","customerCellPhone"]}}]}},"from": 0,"size": 10,"sort": {"creationtime": {"order": "desc"}}}
{"query": {"bool": {"must": [{"match": {"customerName": {"analyzer": "ik_smart","query": "AAA","operator": "and"}}},{"match": {"customerCellPhone": {"analyzer": "ik_smart","query": "A","operator": "and"}}},{"match": {"fullCarNo": {"analyzer": "ik_smart","query": "A","operator": "and"}}}]}},"from": 0,"size": 10,"sort": {"creationtime": {"order": "desc"}}}
2.3.3、详情查询
_doc/{_id} 根据id查询详情
GET customer_remind/_doc/15748138447761588306
2.3.4、filter与must,term与match的区别
2.3.4.1、根据字段类型不同
对于 keyword类型的字段而言, 用 term 和 match 都是可以查询的;
但对于 text 类型的分词字段而言,只能用match才能够查询到结果;
2.3.4.2、根据嵌套类型查询(filter 与 must 是属于同一个级别的查询方式,都可以作为 query->bool 的属性)
filter: 不计算评分,查询效率高、有缓存。(推荐)
term: 精确匹配<br /> match: 模糊匹配、倒排索引
must: 要计算评分,查询效率低、无缓存
term: 精确匹配、 要评分
match:模糊匹配、 要评分2.4、删除
2.4.1、删除索引下所有的数据
POST /customer_remind/_delete_by_query{"query": {"match_all": {}}}
2.4.2、根据id删除单条数据
DELETE /customer_remind/_doc/15748138447761588306
2.5、更新
2.5.1、局部更新
POST /customer_remind/_doc/15748138447761588306/_update{"doc": {"customerName": "名"}}
2.5.2、全量更新
POST /customer_remind/_doc/15748138447761588306{"customerName": "名ss"}
3、备注
3.1、浏览器插件
3.2、配置
