Elasticsearch
Elasticsearch
索引操作
创建索引
对比关系型数据库,创建索引就等同于创建数据库。
在Apipost中,向ES服务器发送PUT请求: http://localhost:9200/索引名称
发送请求可能出现如下错误:
{
"error": {
"root_cause": [
{
"type": "security_exception",
"reason": "missing authentication credentials for REST request [/shopping]",
"header": {
"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
}
}
],
"type": "security_exception",
"reason": "missing authentication credentials for REST request [/shopping]",
"header": {
"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
}
},
"status": 401
}
解决方法:
Apipost:
其他工具解决方法自行搜索。
创建成功后返回结果如下:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
如果索引已存在则返回结果如下:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [shopping/AhX2GQxcS0mGoueCDaoHAQ] already exists",
"index_uuid": "AhX2GQxcS0mGoueCDaoHAQ",
"index": "shopping"
}
],
"type": "resource_already_exists_exception",
"reason": "index [shopping/AhX2GQxcS0mGoueCDaoHAQ] already exists",
"index_uuid": "AhX2GQxcS0mGoueCDaoHAQ",
"index": "shopping"
},
"status": 400
}
获取索引
向ES服务器发送GET请求: http://localhost:9200/索引名
获取成功后返回结果如下:
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "shopping",
"creation_date": "1683371656550",
"number_of_replicas": "1",
"uuid": "AhX2GQxcS0mGoueCDaoHAQ",
"version": {
"created": "7170999"
}
}
}
}
}
查看ES中所有的索引:
向ES服务器发送GET请求: http://localhost:9200/_cat/indices?v
返回结果如下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .security-7 hwDpNvCHTBOaEm6xPZd41Q 1 0 60 0 257.3kb 257.3kb
green open .apm-custom-link dKb_OH1nRbWH92ZujTbVyQ 1 0 0 0 226b 226b
green open .kibana_7.17.9_001 X5hBUMoFSsGRmJTFqKBuog 1 0 22 1 2.3mb 2.3mb
green open .kibana_task_manager_7.17.9_001 R9RZBB0GTwGyC-gwmhxvKA 1 0 18 17926 2.1mb 2.1mb
yellow open book Vh8kwNewTRW7Yh5Ys56s2Q 1 1 107 0 125.1kb 125.1kb
green open .apm-agent-configuration ASYv5XIrThKW8izq_yX7Qg 1 0 0 0 226b 226b
green open .tasks KOMoiR2mQS-YTbJE1yvtJA 1 0 6 0 35kb 35kb
yellow open shopping AhX2GQxcS0mGoueCDaoHAQ 1 1 0 0 226b 226b
不使用参数vhttp://localhost:9200/_cat/indices
返回结果如下:
green open .security-7 hwDpNvCHTBOaEm6xPZd41Q 1 0 60 0 257.3kb 257.3kb
green open .apm-custom-link dKb_OH1nRbWH92ZujTbVyQ 1 0 0 0 226b 226b
green open .kibana_7.17.9_001 X5hBUMoFSsGRmJTFqKBuog 1 0 22 1 2.3mb 2.3mb
green open .kibana_task_manager_7.17.9_001 R9RZBB0GTwGyC-gwmhxvKA 1 0 18 17926 2.1mb 2.1mb
green open .apm-agent-configuration ASYv5XIrThKW8izq_yX7Qg 1 0 0 0 226b 226b
yellow open book Vh8kwNewTRW7Yh5Ys56s2Q 1 1 107 0 125.1kb 125.1kb
yellow open shopping AhX2GQxcS0mGoueCDaoHAQ 1 1 0 0 226b 226b
green open .tasks KOMoiR2mQS-YTbJE1yvtJA 1 0 6 0 35kb 35kb
删除索引
向ES服务器发送DELETE请求: http://localhost:9200/索引名
删除成功返回结果如下:
{
"acknowledged": true
}
文档操作
文档创建
向ES服务器发送POST请求: http://localhost:9200/索引名/_doc
例:http://localhost:9200/shopping/_doc
创建成功后返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",//可以自定义id
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
自定义id: http://localhost:9200/索引名/_doc/id
自定义id后返回结果如下:
// http://localhost:9200/shopping/_doc/1001,如果不传id参数,会产生默认id,如果传递的id已存在,会先进行删除再创建,版本会增加
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
自定义id:http://localhost:9200/索引名/_create/id
id为必传参数,如果id已存在会报错。
文档查询-id
向ES服务器发送GET请求: http://localhost:9200/索引名/_doc/id
例:http://localhost:9200/shopping/_doc/1001
查询成功返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
文档查询-字段
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search?q=字段:值
例:http://localhost:9200/shopping/_search?q=title:菠萝
查询成功后返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1.3862942,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
]
}
}
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match": {
"title": "苹果"
}
}
}
查询成功后返回结果如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.3862942,
"_source": {
"title": "苹果手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4599
}
}
]
}
}
文档查询-分页
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match_all": {}
},
"from": 0,//当前页
"size": 1,//每页数据
}
分页成功后返回结果如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
]
}
}
文档查询-字段
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match_all": {}
},
"_source": ["title"]
}
查询成功后返回结果如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1,
"_source": {
"title": "菠萝手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"title": "苹果手机"
}
}
]
}
}
文档查询-排序
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match_all": {}
},
"_source": ["title"],
"sort": {
"price": {
"order": "desc"
}
}
}
查询成功后返回结果如下:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": null,
"_source": {
"title": "菠萝手机"
},
"sort": [
4999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": null,
"_source": {
"title": "苹果手机"
},
"sort": [
4599
]
}
]
}
}
文档查询-条件
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
//条件
"bool": {
//必须满足
"must": [
{
"match": {
"title": "苹果"
}
}
]
}
}
}
查询成功后返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.3862942,
"_source": {
"title": "苹果手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4599
}
}
]
}
}
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下raw:
{
"query": {
"bool": {
"should": [
{
"match": {
"category": "电脑"
}
},
{
"match": {
"category": "鞋子"
}
}
]
}
}
}
查询成功后返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.4079456,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 2.4079456,
"_source": {
"title": "拯救者R7000",
"category": "电脑",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 7999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 2.4079456,
"_source": {
"title": "鸿星尔克男鞋",
"category": "鞋子",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 139
}
}
]
}
}
文档查询-范围
向ES服务器发送GET请求:http://localhost:9200/索引值/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"bool": {
"filter": {
"range": {
"price": {
"gt": 5000
}
}
}
}
}
}
查询成功后返回结果如下:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0,
"_source": {
"title": "拯救者R7000",
"category": "电脑",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 7999
}
}
]
}
}
文档查询-完全匹配
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match_phrase": {
"title": "菠萝手机"
}
}
}
查询成功后返回结果如下:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.974918,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 3.974918,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
]
}
}
如果不使用完全匹配
请求体body下的raw:
{
"query": {
"match": {
"title": "菠萝手机"
}
}
}
查询成功后返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 3.974918,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 3.974918,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.4523083,
"_source": {
"title": "苹果手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4599
}
}
]
}
}
文档查询-高亮显示
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match": {
"title": "拯救者"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
查询成功后返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.7839146,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 3.7839146,
"_source": {
"title": "拯救者R7000",
"category": "电脑",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 7999
},
"highlight": {
"title": [
"<em>拯</em><em>救</em><em>者</em>R7000"
]
}
}
]
}
}
查询文档-聚合操作
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
//聚合操作
"aggs": {
//名称,随意起
"price_group": {
//分组
"terms": {
//分组字段
"field": "price"
}
}
}
}
查询成功后返回结果如下:
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"title": "苹果手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4599
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 1,
"_source": {
"title": "拯救者R7000",
"category": "电脑",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 7999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1,
"_source": {
"title": "鸿星尔克男鞋",
"category": "鞋子",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 139
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1004",
"_score": 1,
"_source": {
"title": "华为mate50 pro",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 4999,
"doc_count": 2
},
{
"key": 139,
"doc_count": 1
},
{
"key": 4599,
"doc_count": 1
},
{
"key": 7999,
"doc_count": 1
}
]
}
}
}
不查看原始数据
请求体body下的raw:
{
//聚合操作
"aggs": {
//名称,随意起
"price_group": {
//分组
"terms": {
//分组字段
"field": "price"
}
}
},
"size": 0
}
查询成功后的返回结果如下:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 4999,
"doc_count": 2
},
{
"key": 139,
"doc_count": 1
},
{
"key": 4599,
"doc_count": 1
},
{
"key": 7999,
"doc_count": 1
}
]
}
}
}
- 查询所有价格的平均值
请求体body下的raw:
{
//聚合操作
"aggs": {
//名称,随意起
"price_avg": {
//平均值
"avg": {
//分组字段
"field": "price"
}
}
},
"size": 0
}
查询成功后返回结果如下:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 4547
}
}
}
查询全部文档
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search
例: http://localhost:9200/shopping/_search
查询成功返回结果如下:
{
"took": 37,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
}
]
}
}
- 向ES服务器发送GET请求:
http://localhost:9200/索引名/_search
例:http://localhost:9200/shopping/_search
请求体body下的raw:
{
"query": {
"match_all": {}
}
}
查询成功后返回结果如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "O0dk8YcBmL0ibc0IqiTa",
"_score": 1,
"_source": {
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"title": "苹果手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 4599
}
}
]
}
}
更新文档-全量更新
向ES服务器发送PUT请求:http://localhost:9200/索引名/_doc/id
例:http://localhost:9200/shopping/_doc/1001
更新成功后返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
{
"title": "菠萝手机",
"category": "手机",
"images": "https://img.alicdn.com/bao/uploaded/O1CN01qm88hO1vW26DNcpgR_!!6000000006179-0-yinhe.jpg",
"price": 5999
}
更新文档-局部更新
向ES服务器发送POST请求:http://localhost:9200/索引名/_doc/id
例:http://localhost:9200/shopping/_doc/1001
修改成功后返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
删除文档
向ES服务器发送DELETE请求:http://localhost:9200/索引名/_doc/id
例:http://localhost:9200/shopping/_doc/1001
删除成功后返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
映射
创建映射
- 向ES服务器发送PUT请求:
http://localhost:9200/索引名/_mapping
例:http://localhost:9200/user/_mapping
请求体body下的raw:
{
"properties": {
"name": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"tel": {
"type": "keyword",
"index": false
}
}
}
查询成功后返回结果如下:
{
"acknowledged": true
}
查询全部映射
向ES服务器发送GET请求:http://localhost:9200/索引名/_mapping
例:http://localhost:9200/user/_mapping
查询成功后返回结果如下:
{
"user": {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"sex": {
"type": "keyword"
},
"tel": {
"type": "keyword",
"index": false
}
}
}
}
}
创建映射关系
向ES服务器发送POST请求:http://localhost:9200/索引名/_create/id
例:http://localhost:9200/user/_create/1001
请求体body下的raw:
{
"name": "张三",
"sex": "先生",
"tel": "111"
}
创建成功后返回结果如下:
{
"_index": "user_create",
"_type": "1001",
"_id": "DSCeCYgBDP8jTGSM_NLl",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询映射关系
向ES服务器发送GET请求:http://localhost:9200/索引名/_search
例:http://localhost:9200/user/_search
请求体body下的raw:
{
"query": {
"match": {
//因为name字段的type为text,可以进行分词且name字段可以被索引,查询name字段中的任一字符均可
"name": "张"
}
}
}
查询成功后的返回结果如下:
{
"took": 214,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "1001",
"_score": 0.2876821,
"_source": {
"name": "张三",
"sex": "先生",
"tel": "111"
}
}
]
}
}
Tips:type为text时,字段的值会进行分词,查询其中任一字符均可,type为keyword时,字段的值不会进行分词,必须输入完整的值才可。index为true时该字段会被索引可查询,index为false时该字段不可被索引,不会被查询。
Java操作ES
导入依赖
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
连接ES客户端
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
public class EsClientTest {
private final String USERNAME="用户名";
private final String PASSWORD = "密码";
@Test
public void testClient() throws IOException {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
// 创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
//关闭ES客户端
esClient.close();
}
}
索引操作
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsIndexTest {
private RestHighLevelClient esClient;
private final String USERNAME="用户名";
private final String PASSWORD="密码";
@Before
public void init(){
System.out.println("初始化客户端");
//elastic认证
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
//创建es客户端
esClient= new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
}
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest userIndex = new DeleteIndexRequest("user");
AcknowledgedResponse response = esClient.indices().delete(userIndex, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
@Test
public void testSearchIndex() throws IOException {
//查询索引 user
GetIndexRequest userIndex = new GetIndexRequest("user");
//查询索引
GetIndexResponse getIndexResponse = esClient.indices().get(userIndex, RequestOptions.DEFAULT);
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
System.out.println(getIndexResponse.getDataStreams());
System.out.println(getIndexResponse.getIndices());
}
@Test
public void testCreateIndex() throws IOException {
//创建索引 user
CreateIndexRequest userIndex = new CreateIndexRequest("user");
// 创建索引
CreateIndexResponse createIndexResponse = esClient.indices().create(userIndex, RequestOptions.DEFAULT);
// 响应状态,是否创建成功
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("是否创建成功:"+acknowledged);
}
@After
public void destroy() throws IOException {
System.out.println("关闭客户端");
esClient.close();
}
}
文档操作
package com.example;
import com.example.entiry.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocTest {
private final String USERNAME = "用户名";
private final String PASSWORD = "密码";
private RestHighLevelClient client;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化完成");
}
@Test
public void testDeleteDoc() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.index("user").id("1001");
DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
@Test
public void testQueryDoc() throws IOException {
GetRequest getRequest = new GetRequest();
getRequest.index("user").id("1001");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
}
@Test
public void testUpdateDoc() throws IOException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user")
.id("1001")
.doc(XContentType.JSON,"sex","未知");
UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
@Test
public void testInsertDoc() throws IOException {
// 插入数据
IndexRequest userIndex = new IndexRequest();
userIndex.index("user").id("1001");
User user = User.builder().name("张三").age(24).sex("男").build();
// 向ES插入数据,必须将数据转换为JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJSON = mapper.writeValueAsString(user);
userIndex.source(userJSON, XContentType.JSON);
IndexResponse response = client.index(userIndex, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
@After
public void destroy() throws IOException {
client.close();
System.out.println("客户端关闭成功");
}
}
文档的批处理操作
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocBatchTest {
private final String USERNAME="用户名";
private final String PASSWORD = "密码";
private RestHighLevelClient client;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
client=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
}
/**
* 批量查询
* @throws IOException
*/
@Test
public void testBatchQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
System.out.println(searchResponse.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
/**
* 批量删除
* @throws IOException
*/
@Test
public void testBatchDeleteDoc() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest().index("user").id("1001"));
bulkRequest.add(new DeleteRequest().index("user").id("1002"));
bulkRequest.add(new DeleteRequest().index("user").id("1003"));
bulkRequest.add(new DeleteRequest().index("user").id("1004"));
bulkRequest.add(new DeleteRequest().index("user").id("1005"));
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkResponse.getTook());
System.out.println(bulkResponse.getItems());
}
/**
* 批量插入数据
* @throws IOException
*/
@Test
public void testBatchInsertDoc() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","张三","sex","男","age",24));
bulkRequest.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","李四","sex","女","age",26));
bulkRequest.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","王五","sex","男","age",28));
bulkRequest.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","赵六","sex","女","age",30));
bulkRequest.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","孙七","sex","男","age",32));
BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(responses.getTook());
System.out.println(responses.getItems());
}
@After
public void destroy() throws IOException {
client.close();
System.out.println("客户端关闭成功");
}
}
条件查询
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocConditionQuery {
private final String USERNAME="用户名";
private final String PASSWORD = "密码";
private RestHighLevelClient esClient;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
esClient=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化成功");
}
/**
* 条件查询
* @throws IOException
*/
@Test
public void testConditionQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
@After
public void destroy() throws IOException {
System.out.println("客户端关闭成功");
esClient.close();
}
}
分页查询
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocPagingQuery {
private final String USERNAME="用户名";
private final String PASSWORD="密码";
private RestHighLevelClient esClient;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
esClient=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化成功");
}
/**
* 分页查询
* @throws IOException
*/
@Test
public void testPagingQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
searchSourceBuilder.from(2);//起始数据下标,也可看做偏移量,起始值为0
searchSourceBuilder.size(2);//每页显式几条数据
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
@After
public void destroy() throws IOException {
System.out.println("客户端关闭完成");
esClient.close();
}
}
排序
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocSort {
private final String USERNAME="用户名";
private final String PASSWORD="密码";
private RestHighLevelClient esClient;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
esClient=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化完成");
}
/**
* 排序
* @throws IOException
*/
@Test
public void testSortDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
searchSourceBuilder.sort("age", SortOrder.DESC);//降序排序
// searchSourceBuilder.sort("age",SortOrder.ASC);//升序排序
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
@After
public void destroy() throws IOException {
System.out.println("客户端关闭成功");
esClient.close();
}
}
过滤字段
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocFilterFieldTest {
private final String USERNAME = "用户名";
private final String PASSWORD="密码";
private RestHighLevelClient esClient;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
esClient=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化成功");
}
/**
* 过滤字段
* @throws IOException
*/
@Test
public void testFilterFieldDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//过滤字段
String[] excludes={};
String[] includes={"name"};
searchSourceBuilder.fetchSource(includes,excludes);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
@After
public void destroy() throws IOException {
System.out.println("客户端关闭完成");
esClient.close();
}
}
高级查询
package com.example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class EsDocAdvancedQueryTest {
private final String USERNAME="用户名";
private final String PASSWORD="密码";
private RestHighLevelClient esClient;
@Before
public void init(){
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(USERNAME,PASSWORD));
esClient=new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
System.out.println("客户端初始化成功");
}
/**
* 聚合查询
* @throws IOException
*/
@Test
public void testAggregateQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询年龄最大的用户
// MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
//根据年龄进行分组
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
sourceBuilder.aggregation(aggregationBuilder);
searchRequest.source(sourceBuilder);
SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
}
/**
* 高亮查询
* @throws IOException
*/
@Test
public void testHighlightQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery("name.keyword", "张三");
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>");//高亮字段前缀
highlightBuilder.postTags("</font>");//高亮字段后缀
highlightBuilder.field("name");//高亮显示字段
searchSourceBuilder.highlighter(highlightBuilder);
searchSourceBuilder.query(queryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 模糊查询
* @throws IOException
*/
@Test
public void testFuzzyQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//模糊查询-偏差一个字符
FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("name", "张").fuzziness(Fuzziness.ONE);
searchSourceBuilder.query(queryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 范围查询
* @throws IOException
*/
@Test
public void testRangeQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
rangeQuery.gt(28);//大于
rangeQuery.lt(32);//小于
searchSourceBuilder.query(rangeQuery);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 组合查询
* @throws IOException
*/
@Test
public void testCombinedQueryDoc() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//必须满足该条件
boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
boolQueryBuilder.must(QueryBuilders.matchQuery("sex","女"));
searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
@After
public void destroy() throws IOException {
System.out.println("客户端关闭成功");
esClient.close();
}
}
Windows集群部署
解压三份elasticsearch放到elasticsearch-cluster文件夹下,如图所示:
配置node-1001/config/elasticsearch.yml
,详情如下:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1001
# 当前节点可以是master节点,也可以是data(数据)节点
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
network.host: localhost
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
http.port: 9201
# tcp监听端口
transport.tcp.port: 9301
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
#
# ---------------------------------- Security ----------------------------------
#
# *** WARNING ***
#
# Elasticsearch security features are not enabled by default.
# These features are free, but require configuration changes to enable them.
# This means that users don’t have to provide credentials and can get full access
# to the cluster. Network connections are also not encrypted.
#
# To protect your data, we strongly encourage you to enable the Elasticsearch security features.
# Refer to the following documentation for instructions.
#
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.html
配置node-1002/config/elasticsearch.yml
,详情如下:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1002
# 当前节点可以是master节点,也可以是data(数据)节点
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
network.host: localhost
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
http.port: 9202
# tcp监听端口
transport.tcp.port: 9302
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
# 查询主节点
discovery.seed_hosts: ["localhost:9301"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
#
# ---------------------------------- Security ----------------------------------
#
# *** WARNING ***
#
# Elasticsearch security features are not enabled by default.
# These features are free, but require configuration changes to enable them.
# This means that users don’t have to provide credentials and can get full access
# to the cluster. Network connections are also not encrypted.
#
# To protect your data, we strongly encourage you to enable the Elasticsearch security features.
# Refer to the following documentation for instructions.
#
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.html
配置node-1003/config/elasticsearch.yml
,详情如下:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1003
# 当前节点可以是master节点,也可以是data(数据)节点
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
network.host: localhost
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
http.port: 9203
# tcp监听端口
transport.tcp.port: 9303
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
# 查询主节点
discovery.seed_hosts: ["localhost:9301","localhost:9302"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
#
# ---------------------------------- Security ----------------------------------
#
# *** WARNING ***
#
# Elasticsearch security features are not enabled by default.
# These features are free, but require configuration changes to enable them.
# This means that users don’t have to provide credentials and can get full access
# to the cluster. Network connections are also not encrypted.
#
# To protect your data, we strongly encourage you to enable the Elasticsearch security features.
# Refer to the following documentation for instructions.
#
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.html
分别启动三个elasticsearch,向ES服务器发送GET请求:localhost:9201/_cluster/health
返回结果如下:
{
"cluster_name": "my-application",
"status": "green",
"timed_out": false,
"number_of_nodes": 3,
"number_of_data_nodes": 3,
"active_primary_shards": 3,
"active_shards": 6,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 100
}
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!