ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
ES 中的概念用MySQL来做对比如下:
es 和 mysql 对比:
![](https://img.haomeiwen.com/i5728077/71e84a5b8a7bad85.png)
倒排索引
![](https://img.haomeiwen.com/i5728077/8bf24880e0747b72.png)
![](https://img.haomeiwen.com/i5728077/d90327f1fc0ade5f.png)
CRUD
# 创建 lagou 索引
PUT lagou
{
"settings": {
"index": {
"number_of_shards":5,
"number_of_replicas":1
}
}
}
GET lagou/_settings
GET _all/_settings
GET _settings
GET .kibana,lagou/_settings
# 更改 lagou 副本
PUT lagou/_settings
{
"number_of_replicas": 2
}
# 自动生成 id 增加数据
POST lagou/job/
{
"screen_name" : "混在nz",
"created_at" : "2017-09-19 17:56",
"source" : "中国建设银行自助银行",
"cmtStarLevel" : 1.0,
"text" : "",
"mongo_time" : 1545199747
}
# 增加数据数据,如果 id 存在,就覆盖
PUT lagou/job/1
# 更新数据
POST lagou/job/1/_update
{
"doc": {
"screen_name":"allens"
}
}
批量操作
![](https://img.haomeiwen.com/i5728077/691d03b2d7ced084.png)
![](https://img.haomeiwen.com/i5728077/3e493f236eb84df3.png)
![](https://img.haomeiwen.com/i5728077/ecbd13051470a52f.png)
bulk批量
批量导入可以合并多个操作,比如 index,delete,update,create
避免频繁多次建立http连接的开销
![](https://img.haomeiwen.com/i5728077/f06f6f60986db3e3.png)
格式必须和上图中相同,不能格式化。
![](https://img.haomeiwen.com/i5728077/96cefb06829f339c.png)
![](https://img.haomeiwen.com/i5728077/2ce7630eca7083d8.png)
delete只要一行
映射(mapping)
映射:创建索引时,可以预先定义字段类型及其相关属性
ES 会根据 JSON 源数据的基础类型猜测你想要的字段映射。将输入的数据转变成可搜索的索引项。Mapping就是我们自己定义的字段的数据类型,同时告诉 ES 如何索引数据以及是否可以被搜索。
作用:会让索引建立的更加细致和完善
类型:静态映射和动态映射
内置类型:
- string : text \ keyword (string 类型 在es5开始已经废弃,现在只有两种:text \ keyword)
- text : 设置为text的字段会被分析器分析,(分词、抽取词干、去除停用词),建立倒排索引
- keyword : 设置为kerword,当做字符串存取,不会进行分析和建立倒排
- 数字类型 :long、integer、short、byte、double、float
- 日期类型 :date
- bool类型 :boolean
- binary类型 : binary
- 复杂类型:object、nested
- geo类型 :geo-point、geo-shape
- 专业类型:ip、competion(做搜索建议)
object:"people" : {"name": "a", "age": 12}
nested : 对象放到list中 : "":[{}, {}]
内置类型属性
![](https://img.haomeiwen.com/i5728077/7bbc69e32f34723f.png)
demo : 指定字段类型建立一个索引
PUT lagou
{
"mappings": {
"job":{
"properties": {
"title":{
"type": "text"
},
"salary_min":{
"type": "integer"
},
"city":{
"type": "keyword"
},
"company":{
"properties": {
"name":{
"type":"text"
},
"company_addr":{
"type":"text"
},
"employee_count":{
"type":"integer"
}
}
},
"publish_date":{
"type": "date",
"format": "yyy-MM-dd"
},
"comments":{
"type": "integer"
}
}
}
}
}
索引中一旦创建好类型就不能再修改了
ES 查询
基本查询:使用 ES 内置的查询条件进行查询
组合查询:把多个查询组合在一起进行复合查询
过滤:在查询的同时,通过filter条件在不影响打分的情况下晒选数据
过滤仅仅是晒选的过程,不进行打分。
1.基本查询
match查询 : 对输入的条件进行分词,然后再做查询
term查询 : 直接做查询,视为整体的一个keyword
terms查询 :输入一个list,list中包含多个词,只要满足一个就有返回值
示例:
GET lagou/job/_search
{
"query": {
"match": {
"title": "pythonweb开发"
}
}
}
GET lagou/job/_search
{
"query": {
"term": {
"title": "python"
}
}
}
GET lagou/job/_search
{
"query": {
"terms": {
"title": [
"java",
"python"
]
}
}
}
当数据量很大时,控制查询的返回数量
GET lagou/job/_search
{
"query": {
"match": {
"title": "pythonweb开发"
}
},
"from": 1,
"size": 20
}
短语查询
GET lagou/job/_search
{
"query": {
"match_phrase": {
"title": {
"query": "python系统",
"slop": 5
}
}
}
}
短语查询会先把 query 中的词分成 list ,如:["python", "系统"],
然后会查询满足 list 中所有词的数据,slop 的作用是两个词之间最小的距离
multi_match 查询
指定多个字段包含查询条件。
例如:查询 title 和 desc 中包含 python 关键词的数据:
只要任何一个字段满足条件,就会返回数据
GET lagou/job/_search
{
"query": {
"multi_match": {
"query": "python",
"fields": ["title","desc"]
}
}
}
GET lagou/job/_search
{
"query": {
"multi_match": {
"query": "python",
"fields": ["title^3","desc"]
}
}
}
title^3 表示提高title权重
指定返回字段
GET lagou/job/_search
{
"stored_fields": ["title","desc","name"],
"query": {
"multi_match": {
"query": "python",
"fields": ["title","desc"]
}
}
}
对查询结果进行 sort
GET lagou/job/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"FIELD": {
"order": "desc"
}
}
]
}
range 查询
GET lagou/job/_search
{
"query": {
"range": {
"FIELD": {
"gte": 10,
"lte": 20,
"boost": 1
}
}
}
}
GET lagou/job/_search
{
"query": {
"range": {
"add_time": {
"gte": "2018-12-1",
"lte": "now"
}
}
}
}
时间查询,es 会自己解析 now 这个时间
这里查询和mongo中的条件一样 gte 和 lte 代表 大于等于 和 小于等于
boost 代表权重,FIELD 字段
wildcard 查询
类似模糊查询
2.组合查询
bool 查询
在旧版本中,bool查询叫做 filtered查询,在ES5之后,filtered 被 bool 查询替代了。
bool 查询包括 must、should、must_not、filter
格式:
bool : {
"filter": [],
"must": [],
"should": [],
"must_not": [],
}
filter : 做过滤,不打分
must :必须满足所有条件
should :满足一个条件即可返回数据
must_not :与 must 相反,全部不满足,就返回数据
网友评论