Elasticsearch学习(二)————搜索

Elasticsearch
1.query string search
1.1.搜索全部
// 1.
GET http://ip:9200/test/test/_search
结果:
{
"took": 86, # 耗费的时间:ms
"timed_out": false, # 是否超时
"_shards": { # 数据存储在5个主分片上
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": { # 匹配结果
"total": 3, # 查询到三个document
"max_score": 1, # 相关度的匹配分数:分数越高越相关
"hits": [
{
"_index": "test", # 索引 index
"_type": "test", # type
"_id": "2", # id:唯一
"_score": 1, # 相关度的匹配分数:分数越高越相关
"_source": { # 存储的json数据
"first_name": "小翠", # field
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
1.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔:
// 2.
GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc,price:desc
GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": null,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [ #排序字段的值
25
]
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2.query DSL
2.1.搜索全部
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match_all":{}
}
}
结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
2.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match":{
"about":"climbing"
}
},
"sort":[
{
"age":"desc"
}
]
}

结果:
{
"took": 115,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": null,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
25
]
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": null,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
},
"sort": [
20
]
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
2.3. 分页数据
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match_all":{} # 查询所有
},
"from":0, # 从第几条数据开始 0:第一条
"size":1 # 展示几条数据
}
1
2
3
4
5
6
7
8
9
10
2.4.只展示指定的filed
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match_all":{}
},
"_source":[
"first_name",
"age"
]
}

结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "小雪",
"age": 20
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
3.query filter
3.1.多个查询条件:about字段必须包含"climbing";age大于20岁
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"bool":{
"must":{
"match":{
"about":"climbing"
}
},
"filter":{
"range":{
"age":{
"gt":20
}
}
}
}
}
}

结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.26742277,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.26742277,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
3.2. 多个查询条件:must、 should、 must_not
POST http://47.99.74.228:9200/test/test/_search
{
"query":{
"bool":{
"must":{ # 必须匹配
"match":{
"first_name":"小翠"
}
},
"should":{ # 可以匹配,也可以不匹配
"match":{
"last_name": "xue"
}
},
"must_not":{ # 必须不匹配
"match":{
"last_name": "cui"
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
4.full-test search
4.1.全文检索
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match":{
"about":"go climbing"
}
}
}
分析:es将about这个filed拆解成每个词(term),建立倒排索引,每个term对应相应的document_id
结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0.7447149,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.61562645,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.61562645,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "5",
"_score": 0.2876821,
"_source": {
"first_name": "花花",
"last_name": "huahau",
"age": 16,
"price": 20000,
"about": "climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "7",
"_score": 0.25759193,
"_source": {
"about": "go"
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 0.25759193,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
4.2.短语搜索:匹配短语
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
}
}

结果:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.9748371,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.9748371,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.6156264,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
5.highlight search
5.1.关键字高亮
// 1.
语法:
{
"query":{
"match":{
"about":"climbing" # 关键字
}
},
"highlight":{
"fields":{
"about":{http://www.my516.com} # 字段
}
}
}

结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0.48741856,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.48741856,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>" # <em> 标签html中高亮显示
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 0.37235746,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.37235746,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "5",
"_score": 0.2876821,
"_source": {
"first_name": "花花",
"last_name": "huahau",
"age": 16,
"price": 20000,
"about": "climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"<em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 0.25759193,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
},
"highlight": {
"about": [
"<em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.12820786,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
}
]
}
}
---------------------

原文地址:https://www.cnblogs.com/hyhy904/p/10992425.html

时间: 2024-10-12 07:36:36

Elasticsearch学习(二)————搜索的相关文章

Elasticsearch学习之深入搜索二 --- 搜索底层原理剖析

1. 普通match如何转换为term+should { "match": { "title": "java elasticsearch"}} 使用诸如上面的match query进行多值搜索的时候,es会在底层自动将这个match query转换为bool的语法,bool should,指定多个搜索词,同时使用term query { "bool": { "should": [ { "term&

Elasticsearch学习之深入搜索一 --- 提高查询的精准度

1. 为帖子增加标题字段 POST /forum/article/_bulk { "update": { "_id": "1"} } { "doc" : {"title" : "this is java and elasticsearch blog"} } { "update": { "_id": "2"} } { "

浅入深出ElasticSearch构建高性能搜索架构

浅入深出ElasticSearch构建高性能搜索架构  课程学习地址:http://www.xuetuwuyou.com/course/161 课程出自学途无忧网:http://www.xuetuwuyou.com 一.课程用到的软件 ElasticSearch5.0.0 Spring Tool Suite 3.8.2.RELEASE Maven3.0.5 Spring4 Netty4 Hadoop2.7.1 Kibana5.0 JDK1.8.0_111 二.课程目标 1.快速学习Elastic

elasticsearch的rest搜索--- 查询

目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档 https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html 2.  查询的rest格式 3.  介绍用过的查询方式 一般的查询 http://blog.csdn.net/dm_vincent/article/d

elasticsearch实现网站搜索

使用elasticsearch 实现网站搜索,可以支持商品搜索,筛选项过滤搜索 ,价格排序, 打分 筛选项聚合,还有其他综合排序 后续推出搜索人工干预排序,根据销量,好评率,售卖率 进行全方位的搜索实现 想要完全理解此搜索项目 需要学习以下我之前写过的知识 ElasticSearch6.0 索引模板 http://www.cnblogs.com/shoutn/p/8274893.html ElasticSearch6.0 高级应用之 多字段聚合Aggregation(二) http://www.

ElasticSearch(二):文档的基本CRUD与批量操作

ElasticSearch(二):文档的基本CRUD与批量操作 学习课程链接<Elasticsearch核心技术与实战> Create 文档 支持自动生成文档_id和指定文档_id两种方式. 通过调用POST index_name/_doc,系统会自动生成文档 _id. #create document. 自动生成 _id POST users/_doc { "user" : "Mike", "post_date" : "2

(转)ElasticSearch学习

ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引.  官方站点:http://www.elasticsearch.com/ 中文站点:http://es-cn.medcl.net/    1.安装  必须先安装Java环境,并设置 JAVA_HOME => C:\Program Files\Java\jdk1.6.0_18  elasticsea

C#中使用Redis学习二 .NET4.5中使用redis hash操作

上一篇>> 摘要 上一篇讲述了安装redis客户端和服务器端,也大体地介绍了一下redis.本篇着重讲解.NET4.0 和 .NET4.5中如何使用redis和C# redis操作哈希表.并且会将封装的一些代码贴一下.在讲解的过程中,我打算结合redis操作命令一起叙述,算是作为对比吧.这样也能让读者清楚了解,所分装的代码对应的redis的哪一些操作命令. hash哈希表简介 如何在.NET4.0/4.5中安装redis组件? 在上一篇博文中,安装好的redis服务器端,要记得开启服务.然后再

elasticsearch学习笔记——相关插件

logstash-input-jdbc学习 ES(elasticsearch缩写)的一大优点就是开源,插件众多.所以扩展起来非常的方便,这也造成了它的生态系统越来越强大.这种开源分享的思想真是与天朝格格不入啊.国内的开源社区做了也很长时间,可是也没出现什么拿的出手的东西,可能只还有阿里比较注重分享一些. ES的查询速度非常快,搜索非常快.但是呢,我们的数据还是主要存在传统的关系型数据库中的.有没有什么办法可以将数据库中的数据实时同步到ES中呢.logstash就是这么一个东西. Logstash

Python 和 Elasticsearch 构建简易搜索

Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正确,软件才能运行.如果从一种操作系统里面运行另一种操作系统,通常我们采取的策略就是引入虚拟机,比如在 Windows 系统里面运行 Linux 系统.这种方式有个很大的缺点就是资源占用多.冗余步骤多.启动慢.目前最流行的 Linux 容器解决方案之一就是Docker,它最大优点就是轻量.资源占用少.