[关闭]
@ruoli 2017-03-18T07:45:07.000000Z 字数 8956 阅读 3588

ElasticSearch_1.7.1

其他开源框架


此处对应版本ElasticSearch_1.7.1,windows 平台。

1、ES安装

系统准备:
ES1.7.1,需要Jdk1.7.55+ 或Jdk1.8.20+ 支持。
从官方下载ES1.7.1,地址如下:

https://www.elastic.co/downloads/past-releases/elasticsearch-1-7-1

下载后,解压,双击 elasticsearch-1.7.1\bin\elasticsearch.bat即可运行。
启动完成后,访问 http://localhost:9200/,若出现ElasticSearch基础状态信息,则说明安装成功。

ElasticSearch 默认不允许跨域访问,如需跨域,则需要如下配置:
修改配置文件 elasticsearch-1.7.1\config\elasticsearch.yml
在最后顶格添加配置:http.cors.enabled: true

2、Head插件安装

Head插件是一个elasticsearch的集群管理工具,是elasticsearch的一个可视化客户端。

在elasticsearch-1.7.1\bin目录下开启命令行,运行如下命令:

plugin -install mobz/elasticsearch-head

即可自动安装
安装完成后,即可访问Head插件主页:

http://localhost:9200/_plugin/head

3、BigDesk插件安装

bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看集群的各种状态,如:CPU,内存使用情况,索引数据,搜索情况,http连接数等,安装方法如下:

在elasticsearch-1.7.1\bin目录下开启命令行,运行如下命令:

plugin -install lukas-vlcek/bigdesk/2.5.0

即可自动安装
安装完成后,即可访问Head插件主页:

http://localhost:9200/_plugin/bigdesk

4、Marvel插件的安装

Marvel也是elasticsearch的一个管理监控工具,集head和bigdesk的优点与一身,Marvel是收费软件,安装方法如下:
在elasticsearch-1.7.1\bin目录下开启命令行,运行如下命令:

plugin -install elasticsearch/marvel/latest

即可自动安装
安装完成后,即可访问Head插件主页:

http://localhost:9200/_plugin/marvel

也可离线安装,下载安装包:

marvel-1.3.1.zip1954.5kB

下载后,解压到elasticsearch-1.7.1\plugins,并且将文件夹重命名为:marvel,重启 elasticsearch即可。

5、索引的CRUD操作

1、索引的增加

以下所有操作均使用Marvel插件来操作。
初始化索引,在创建索引时需要同时指定shareds碎片数量和replicas备份数量,创建library索引代码如下:

  1. PUT http://localhost:9200/library/
  2. {
  3. "setting":{
  4. "index":{
  5. "number_of_shards":5,
  6. "number_of_replicas":1
  7. }
  8. }
  9. }

2、索引的查询

可以通过GET,带上_settings可以获取该索引的详细配置信息

  1. GET /library/_settings

同时获取两个索引的信息

  1. GET /library,library1/_settings

获取所有索引的信息

  1. GET /_all/_settings

删除指定索引

  1. DELETE /library

6、对文档的操作

1、创建一条文档记录

  1. # -----索引名称
  2. # | -----type名称
  3. # | | ----文档ID
  4. # | | |
  5. # | | |
  6. # | | |
  7. # | | |
  8. # | | |
  9. # | | |
  10. PUT /library/books/1
  11. {
  12. "title":"Elasticsearch:the definitive guide",
  13. "name":{
  14. "first":"zachary",
  15. "last":"tong"
  16. },
  17. "publish_date":"2017-02-19",
  18. "price":"49.99"
  19. }

注意:如果创建文档时,不指定id,则ES会自动分配一个UUID。

2、查询一条文档记录

  1. GET /library/books/1

3、通过 _source 获取指定字段

  1. #获取一个字段
  2. GET /library/books/1?_source=title
  3. #获取两个字段
  4. GET /library/books/1?_source=title,price
  5. #获取所有字段
  6. GET /library/books/1?_source

4、对文档进行更新操作

  1. POST /library/books/1/_update
  2. {
  3. "doc":{
  4. "price":10
  5. }
  6. }

5、删除一个文档

  1. DELETE /library/books/1
  2. #删除类型
  3. DELETE /library/books

7、批量获取多个文档

Multi GET API
可以更快更迅速的检索多个文档。
Mget API 参数是一个 docs数组,数组的每一个节点定义一个文档的 _index、_type、_id元数据。如下所示,其中 _source 为可选字段,用于指定要查询的列信息。

1、获取不同Index的信息

  1. GET /_mget
  2. {
  3. "docs":[
  4. {
  5. "_index":"library",
  6. "_type":"books",
  7. "_id":1
  8. },
  9. {
  10. "_index":"library",
  11. "_type":"books",
  12. "_id":2,
  13. "_source":"title"
  14. }
  15. ,
  16. {
  17. "_index":"library",
  18. "_type":"books",
  19. "_id":2,
  20. "_source":["title","price"]
  21. }
  22. ]
  23. }

2、获取相同Index下,相同Type下,不同ID的文档

  1. GET /library/books/_mget
  2. {
  3. "docs":[
  4. {"_id":1},
  5. {"_type":"books","_id":2}
  6. ]
  7. }

简便方法:

  1. GET /library/books/_mget
  2. {
  3. "ids":["1","2"]
  4. }

8、对文档内容的批量操作

批量操作的API:bulk
可以实现同时对多个文档的 create,update,delete。

请求体格式:

  1. {action:{metadata}}\n
  2. {request body}\n
  3. {action:{metadata}}\n
  4. {request body}\n

其中 action包括如下内容:
create:当文档不存在时进行创建
index:创建新文档或替换旧文档
update:局部更新一个文档
delete:删除一个文档

注意:对文档进行批量操作时,不能进行美化代码操作,其是根据换行来区分数据的。

具体示例代码如下:

  1. POST /library/books/_bulk
  2. {"index":{"_id":1}}
  3. {"title":"Elasticsearch the definitive guide1","price":49}
  4. {"index":{"_id":2}}
  5. {"title":"Elasticsearch the definitive guide2","price":59}

当 action 为 delete时,没有request body信息,如下

  1. POST /library/books/_bulk
  2. {"delete":{"_id":1}}
  3. {"delete":{"_id":2}}

update 操作,示例如下:

  1. POST /library/books/_bulk
  2. {"update":{"_id":1}}
  3. {"doc":{"price":18}}
  4. {"update":{"_id":2}}
  5. {"doc":{"price":28}}

所有 create、index、update、delete 可以组合一起使用

9、搜索查询入门

1、简单查询

指定Index、指定 Type 的搜索

  1. GET /library/books/_search?q=title:definitive

指定Index、不指定 Type 的搜索

  1. GET /library/_search?q=title:definitive

不指定Index、不指定 Type 的搜索

  1. GET /_search?q=title:definitive

2、Term查询和Terms查询

Term查询某个字段中有某个关键词的文档

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "term": {
  5. "title": "definitive"
  6. }
  7. }
  8. }

Terms 查询某个字段中有多个关键字的文档,minimun_match:最小匹配集,若值为1,则代表关键词里最少出现一个,若值为2,则代表关键词里最少出现两个

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "title": [
  6. "definitive",
  7. "guide2"
  8. ],
  9. "minimum_match": 2
  10. }
  11. }
  12. }

3、match、match_all、match_phrase 查询

match 查询可接收文字、日期、时间 类型的数据
match 和 term 查询的区别是:match查询时,ES会给定合适的分析器,term查询则不会。

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "match": {
  5. "price": "18"
  6. }
  7. }
  8. }

match_all 搜索指定索引下所有的文档

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

match_phrase 短语查询,slop 定义 关键词之间隔多少未知单词

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "the,guide",
  7. "slop":1
  8. }
  9. }
  10. }
  11. }

4、multi_match 查询

multi_match 可同时查询多个字段,如下代码:查询 title 和 preview 两个字段中 包含 elasticsearch字段的文档

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "preview",
  6. "fields":["title","preview"]
  7. }
  8. }
  9. }

5、prefix 前置匹配查询

查询列数据为固定前缀的文档信息

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "prefix": {
  5. "title":{
  6. "value": "el"
  7. }
  8. }
  9. }
  10. }

6、range 范围查询

有 from、to、include_lower、include_upper、boost这些参数,
include_lower:是否包含范围的左边界,默认为true
include_upper:是否包含范围的右边界,默认为true

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "range": {
  5. "price":{
  6. "from": 19.99,
  7. "to":30,
  8. "include_lower":false,
  9. "include_lower":false
  10. }
  11. }
  12. }
  13. }

7、wildcard 查询

允许使用通配符* 和 ? 进行查询
*:一个或者多个字符
?:仅代表一个字符
wildcard 查询 很耗性能

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "title":"el?s*"
  6. }
  7. }
  8. }

8、fuzzy 模糊查询

fuzzy查询
value:查询的关键字
boost:设置查询的权值,默认为1.0
min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串值为0-1,对于数值取值可能大于1,对于日期型,取值为1d、2d、1m 分别代表一天两天一月。
prefix_length:指明区分词项的共同前缀长度,默认值为0
max_expansions:指明查询中的词项可扩展的数目。
此查询比较耗性能。

  1. GET /library/books/_search
  2. {
  3. "query":{
  4. "fuzzy": {
  5. "title":{
  6. "value": "guideb",
  7. "min_similarity": 0.5
  8. }
  9. }
  10. }
  11. }

更多查询还有:
fuzzy_like_this 查询
fuzzy_like_this_field查询,功能与fuzzy_like_this一致,但fuzzy_like_this_field只可作用于一个字段。
more_like_this 查询
more_like_this_field 查询,类似于fuzzy_like_this_field。

10、查询的其他配置

1、指定查询返回的列

fields 代表返回的结果字段,不指明代表返回全部

  1. GET /library/books/_search
  2. {
  3. "fields": ["title"],
  4. "query": {
  5. "term": {
  6. "title": "definitive"
  7. }
  8. }
  9. }

2、partial_fields 指定和排除字段

partial_fields,可以指定和排除返回的字段,如下:

  1. GET /library/books/_search
  2. {
  3. "partial_fields": {
  4. "partial": {
  5. "include": ["title"],
  6. "exclude": ["preview"]
  7. }
  8. },
  9. "query": {
  10. "term": {
  11. "title": "definitive"
  12. }
  13. }
  14. }

可以配合通配符使用:

  1. GET /library/books/_search
  2. {
  3. "partial_fields": {
  4. "partial": {
  5. "include": ["ti*"],
  6. "exclude": ["pr*"]
  7. }
  8. },
  9. "query": {
  10. "term": {
  11. "title": "definitive"
  12. }
  13. }
  14. }

3、sort 对结果进行排序

sort :asc 升序,desc

  1. GET /library/books/_search
  2. {
  3. "query": {
  4. "term": {
  5. "title": "definitive"
  6. }
  7. },
  8. "sort":{
  9. "price":{
  10. "order":"desc"
  11. }
  12. }
  13. }

4、指定返回结果数量

from 代表返回结果的开始下标,size 代表返回的结果数量

  1. GET /library/books/_search
  2. {
  3. "from": 1,
  4. "size": 20,
  5. "query": {
  6. "term": {
  7. "title": "definitive"
  8. }
  9. }
  10. }

11、filter 过滤查询

filter 查询 可以实现 基本查询的功能,语法类似与 Sql 语法,而且自带 cache缓存,提升二次查询速度,推荐使用 filter查询。

建立测试数据,建立store索引,products类型,新增四条文档:

  1. PUT http://localhost:9200/store/
  2. {
  3. "setting":{
  4. "index":{
  5. "number_of_shards":5,
  6. "number_of_replicas":1
  7. }
  8. }
  9. }
  10. POST /store/products/_bulk
  11. {"index":{"_id":1}}
  12. {"price":10,"productID":"SD100"}
  13. {"index":{"_id":2}}
  14. {"price":20,"productID":"SD200"}
  15. {"index":{"_id":3}}
  16. {"price":30,"productID":"SD300"}
  17. {"index":{"_id":4}}
  18. {"price":40,"productID":"SD400"}

1、简单filter查询

模拟SQL:select * from products where price=20
filered 查询商品价格是20的商品

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "query": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "term": {
  10. "price": 20
  11. }
  12. }
  13. }
  14. }
  15. }

此处查询内容可以指定多个值:

"price": [10,20]

2、bool 过滤查询

可以进行组合查询,类似于 SQL中的 and 、or、not,格式如下:
{
"bool":{
"must":{},
"shoulld":{},
"must_not":{}
}
}
must:里面的条件必须满足,类似于 and
should:里面的条件至少满足一个,类似于 or
must_not:里面的条件必须不能满足,类似于 not

代码示例:
模拟SQL:select * from products where (price=20 or productID="SD100") and price != 30

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "bool": {
  7. "should":[
  8. {"term": {"price": 20}},
  9. {"term": {"productID": "sd100"}}
  10. ],
  11. "must_not": [
  12. {"term": {"price": 30}}
  13. ]
  14. }
  15. }
  16. }
  17. }
  18. }

3、嵌套查询

模拟SQL:select * from products where productID='sd100' or (productID='SD200' and price=20)

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "bool": {
  7. "should":[
  8. {"term": {"productID": "sd100"}},
  9. {"bool": {
  10. "must": [
  11. {"term": {"productID": "sd200"}},
  12. {"term": {"price":20}}
  13. ]
  14. }}
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }

4、 另一种 and、or、not 查询

没有bool,直接使用 and、or、not
此种查询,不带缓存

模拟SQL:
select * from products where productID='sd100' or (productID='SD200' and price=20)

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "or":[
  7. {"term": {"productID": "sd100"}},
  8. {"and": [
  9. {"term": {"productID": "sd200"}},
  10. {"term": {"price":20}}
  11. ]
  12. }
  13. ]
  14. }
  15. }
  16. }
  17. }

136 0969 7310

5、range 范围查询

模拟SQL:SLECT * FROM products where price BETWEEN 20 AND 40
gt:> 大于
lt:< 小于
gte: >= 大于等于
lte: <= 小于等于

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "range":
  7. {"price":
  8. {"gt":20,"lt":40}}
  9. }
  10. }
  11. }
  12. }

6、过滤空非空

使用关键词:missing、exists
初始化数据:
POST /store/products/_bulk
{"index":{"_id":5}}
{"price":50,"productID":null}

模拟SQL:SELECT * FROM products where productID is null

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "missing": {
  7. "field": "productID"
  8. }
  9. }
  10. }
  11. }
  12. }

模拟SQL:SELECT * FROM products where productID is not null

  1. GET /store/products/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "exists": {
  7. "field": "productID"
  8. }
  9. }
  10. }
  11. }
  12. }

12、组合查询

1、bool查询

类似于SQL 中的 where
1、must、should、must_not
2、minmum_should_match:表示一个文档中 至少 匹配多少个关键词才算匹配成功,一般作用于should。
3、disable_coord:启用和禁用一个文档中所包含所有关键词的分数得分计算,默认值是 false。

2、boosting 查询

将两个查询封装在一起,降低一个查询结果返回的分值

positive部分:查询的查询结果分值不变
negative部分:查询的结果分值会被降低
negative_boost部分:设置negative中要降低的分数。

3、constant_score查询

可以让一个查询得到一个恒定的分值。

4、indices 查询

可以查询其他索引里面的数据。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注