Skip to content

Commit 80ae6f1

Browse files
committed
ES 深入搜索
1 parent 47fb0a8 commit 80ae6f1

1 file changed

Lines changed: 277 additions & 0 deletions

File tree

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
---
2+
title: 全文搜索ElasticSearch(二)深入搜索
3+
date: 2018-04-27 15:14:17
4+
url: lucene/elasticsearch-query
5+
tags:
6+
- Elasticsearch
7+
categories:
8+
- Elasticsearch
9+
---
10+
11+
![全文搜索](https://cdn.jsdelivr.net/gh/niumoo/cdn-assets/2019/534fa087bb57ff07a93d9766d1a1b8fa.jpg)
12+
现在,我们已经学会了如何使用 Elasticsearch 作为一个简单的 NoSQL 风格的分布式文档存储系统。我们可以将一个 JSON 文档扔到 Elasticsearch 里,然后根据 ID 检索。但 Elasticsearch 真正强大之处在于可以从无规律的数据中找出有意义的信息——从“大数据”到“大信息”。
13+
14+
Elasticsearch 不只会存储(stores) 文档,为了能被搜索到也会为文档添加索引(indexes) ,这也是为什么我们使用结构化的 JSON 文档,而不是无结构的二进制数据。
15+
16+
文档中的每个字段都将被索引并且可以被查询 。不仅如此,在简单查询时,Elasticsearch 可以使用 所有(all) 这些索引字段,以惊人的速度返回结果。这是你永远不会考虑用传统数据库去做的一些事情。
17+
<!-- more -->
18+
19+
笔者使用的ElasticSearch版本:**elasticsearch-5.5.1**
20+
## 数据准备
21+
```json
22+
{"title":"超级玛丽秘籍","author":"王五","word_count":5000,"publish_date":"1994-11-01"}
23+
{"title":"三国志","author":"陈寿","word_count":30000,"publish_date":"1995-11-01"}
24+
{"title":"三国演绎","author":"罗贯中","word_count":30000,"publish_date":"1994-03-01"}
25+
{"title":"玛丽与管道工","author":"玛丽","word_count":10000,"publish_date":"1994-03-01"}
26+
{"title":"史记","author":"司马迁","word_count":20000,"publish_date":"1994-08-01"}
27+
{"title":"如何蹦的更高","author":"玛丽","word_count":20000,"publish_date":"1994-08-01"}
28+
```
29+
30+
## 子条件查询
31+
32+
### Query context
33+
在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的**有多好**
34+
35+
常用查询:
36+
全文本查询,针对**文本类型数据**
37+
38+
#### 全文本查询-模糊查询
39+
40+
```json
41+
POST http://localhost:9200/book/_search
42+
{
43+
"query":{
44+
"match":{
45+
"title":"三国志"
46+
}
47+
}
48+
}
49+
// 响应
50+
{
51+
"took": 4,
52+
"timed_out": false,
53+
"_shards": {
54+
"total": 5,
55+
"successful": 5,
56+
"failed": 0
57+
},
58+
"hits": {
59+
"total": 2,
60+
"max_score": 0.99938464,
61+
"hits": [
62+
{
63+
"_index": "book",
64+
"_type": "novel",
65+
"_id": "AWLOAB8bQcKhhHA2xN60",
66+
"_score": 0.99938464,
67+
"_source": {
68+
"title": "三国志",
69+
"author": "陈寿",
70+
"word_count": 30000,
71+
"publish_date": "1995-11-01"
72+
}
73+
},
74+
{
75+
"_index": "book",
76+
"_type": "novel",
77+
"_id": "AWLOADU4QcKhhHA2xN61",
78+
"_score": 0.34450945,
79+
"_source": {
80+
"title": "三国演绎",
81+
"author": "罗贯中",
82+
"word_count": 30000,
83+
"publish_date": "1994-03-01"
84+
}
85+
}
86+
]
87+
}
88+
}
89+
```
90+
91+
#### 全文本查询-短语查询
92+
查询书籍名字就是”三国志”的书籍信息。
93+
```json
94+
POST http://localhost:9200/book/_search
95+
{
96+
"query":{
97+
"match_phrase":{
98+
"title":"三国志"
99+
}
100+
}
101+
}
102+
103+
```
104+
105+
#### 全文本查询-多字段查询
106+
```json
107+
POST http://localhost:9200/book/_search
108+
{
109+
"query":{
110+
"multi_match":{
111+
"query":"玛丽",
112+
"fields":["author","title"]
113+
}
114+
}
115+
}
116+
```
117+
#### 全文本查询-语法查询
118+
查询“三国”和”演绎”同时匹配的书籍信息或者可以匹配“超级玛丽”的书籍信息。
119+
120+
```json
121+
POST http://localhost:9200/book/_search
122+
{
123+
"query":{
124+
"query_string":{
125+
"query":"(三国 AND 演绎) OR 超级玛丽"
126+
}
127+
}
128+
}
129+
```
130+
131+
指定字段的语法查询,查询标题中包含三国或者玛丽的书籍信息。
132+
```json
133+
POST http://localhost:9200/book/_search
134+
{
135+
"query":{
136+
"query_string":{
137+
"query":"三国 OR 玛丽",
138+
"fields":["title"]
139+
}
140+
}
141+
}
142+
```
143+
144+
字段级别查询,针对结构化数据,如数字,日期等
145+
#### 字段级别查询-精确查询
146+
查询书籍字数为10000的书籍信息。
147+
```json
148+
POST http://localhost:9200/book/_search
149+
{
150+
"query":{
151+
"term":{
152+
"word_count":10000
153+
}
154+
}
155+
}
156+
```
157+
158+
#### 字段级别查询-范围查询
159+
查询书籍字数大于等于10000且小于等于20000的书籍信息,gte中的e是equals,等于的意思。
160+
```json
161+
POST http://localhost:9200/book/_search
162+
{
163+
"query":{
164+
"range":{
165+
"word_count":{
166+
"gte":10000,
167+
"lte":20000
168+
}
169+
}
170+
}
171+
}
172+
```
173+
174+
175+
### Filter context
176+
在查询的过程中,只判断该文档是否满足条件,只有YES或者NO的结果。
177+
使用Filter查询书籍字数为10000的书籍信息。
178+
```json
179+
POST http://localhost:9200/book/_search
180+
{
181+
"query":{
182+
"bool":{
183+
"filter":{
184+
"term":{
185+
"word_count":10000
186+
}
187+
}
188+
}
189+
}
190+
}
191+
```
192+
193+
## 复合条件查询
194+
195+
### 固定分数查询
196+
"boost":2用于指定查询出来的信息评分都为2。
197+
```json
198+
POST http://localhost:9200/book/_search
199+
{
200+
"query":{
201+
"constant_score":{
202+
"filter":{
203+
"match":{
204+
"title":"三国"
205+
}
206+
},
207+
"boost":2
208+
}
209+
}
210+
}
211+
```
212+
### 组合或关系查询
213+
查询作者是陈寿或者标题为三国志的书籍信息。
214+
should表示条件之间是或的关系。
215+
```json
216+
POST http://localhost:9200/book/_search
217+
{
218+
"query":{
219+
"bool":{
220+
"should":[
221+
{
222+
"match":{
223+
"author":"陈寿"
224+
}
225+
},
226+
{
227+
"match":{
228+
"title":"三国志"
229+
}
230+
}
231+
]
232+
}
233+
}
234+
}
235+
```
236+
### 组合与关系查询
237+
查询作者是陈寿或者标题为三国志的书籍信息。
238+
Must表示条件之间是且的关系。
239+
```json
240+
POST http://localhost:9200/book/_search
241+
{
242+
"query":{
243+
"bool":{
244+
"must":[
245+
{
246+
"match":{
247+
"author":"陈寿"
248+
}
249+
},
250+
{
251+
"match":{
252+
"title":"三国志"
253+
}
254+
}
255+
]
256+
}
257+
}
258+
}
259+
```
260+
261+
### 组合非关系查询
262+
查询作者不是陈寿的书籍信息。
263+
```json
264+
POST http://localhost:9200/book/_search
265+
{
266+
"query":{
267+
"bool":{
268+
"must_not":{
269+
"match":{
270+
"author":"陈寿"
271+
}
272+
}
273+
}
274+
}
275+
}
276+
```
277+

0 commit comments

Comments
 (0)