ES使用心得
Posted kxhbd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ES使用心得相关的知识,希望对你有一定的参考价值。
目录
启动 2
检测启动是否成功 2
Post 192.168.2.29:9200/twitter/_search?routing=kimchy 2
Post 192.168.2.29:9200/twitter/_search 3
192.168.2.29:9200/bp_view/_search 5
POST /cool-user-index/_delete_by_query 7
封装工具介绍 8
服务 8
Elasticsearch 全文检索引擎
启动
检测启动是否成功
索引/请求方式/类型(指定类别)
Post 192.168.2.29:9200/twitter/_search?routing=kimchy
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "twitter",
"index_uuid": "_na_",
"index": "twitter"
}
],
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "twitter",
"index_uuid": "_na_",
"index": "twitter"
},
"status": 404
}
Post 192.168.2.29:9200/twitter/_search
{
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "some query string here"
}
},
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}
}
未命中时返回
{
"took": 101,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
Post 192.168.2.29:9200/twitter/_search
{
"query": {
"bool" : {
"must" : {
"query_string" : {
}
}
}
}
}
不能没有条件
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[query_string] must be provided with a [query]",
"line": 8,
"col": 17
}
],
"type": "parsing_exception",
"reason": "[query_string] must be provided with a [query]",
"line": 8,
"col": 17
},
"status": 400
}
命中
192.168.2.29:9200/bp_view/_search
{
"from" : 2, //请求页数
"size": 1, //每页数量
"query": {
"match_all" : {} //全匹配
}
}
{
"took": 38,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10140, //总条数
"max_score": 1.0,
"hits": [ //数据列表
{。。。}
]
}
}
模糊查询
Post 192.168.2.29:9200/bp_view/_search
{
"query": {
"bool": {
"must":{"match":{"title":"战"}} //模糊字段
}
}
}
删除其中不包含的id
POST /cool-user-index/_delete_by_query
{
"query": {
"bool": {
"must_not": [
{
"ids": {
"values": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,26,27,29,30,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,54,58,59,60,61,63,64,75,76,77,78,79,80,81,82,83,84,85,86,100,101,102,103,104,105,106,107,110,111,129,134,135,136,140,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184]
}
}
]
}
}
}
删除其中包含的id
POST /cool-user-index/_delete_by_query
{
"query": {
"bool": {
"must": [
{
"ids": {
"values": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,26,27,29,30,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,54,58,59,60,61,63,64,75,76,77,78,79,80,81,82,83,84,85,86,100,101,102,103,104,105,106,107,110,111,129,134,135,136,140,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184]
}
}
]
}
}
}
RequestReturnUtil 接口调用工具类
Delate 192.168.2.29:9200/bp_view/1 //删除数据
POST 192.168.2.29:9200/bp_view/_delete_by_query?refresh&slices=5&pretty { "query": { "match_all": {} } } //清空数据
封装工具介绍
void getParmOfdeleteCataByID(List<String> list)
参数传入ids删除编目数据
void getParmOfdeleteCollectionByID(List<String> list)
参数传入ids删除典藏数据
void addCollection(String id ,String json)
参数传入id和典藏参数
void addCata(String id ,String json)
参数传入id和典藏参数
服务
Windows
启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch
这样直接启动的话集群名称会默认为elasticsearch,节点名称会随机生成。
停止就直接在cmd界面按Ctrl+C
其实我们也可以将elasticsearch设置为windows系统服务:
elasticsearch的bin目录下有一个elasticsearch-service.bat
进入bin目录下执行:
elasticsearch-service.bat install
1
然后在系统服务中可以看到Elasticsearch已成为系统服务。
elasticsearch-service.bat后面还可以执行这些命令
install: 安装Elasticsearch服务
remove: 删除已安装的Elasticsearch服务
start: 启动Elasticsearch服务
stop: 停止服务
以上是关于ES使用心得的主要内容,如果未能解决你的问题,请参考以下文章