
ElasticSearch总结
ES官网
IK分词器Git地址
Kibana地址
elasticsearch-head
在kibana中的命令
#使用ik分词器搜索
GET _analyze
{
"analyzer":"ik_max_word",
"text": "今天天气真好呢"
}
#为fangyuan索引创建数据类型
PUT /fangyuan
{
"mappings": {
"properties": {
"age":{
"type":"long"
},
"name":{
"type":"text"
},
"tags":{
"type":"text"
}
}
}
}
#添加信息,添加一条索引类型为fangyuan,类型为doc,id设置为1的的内容,可以用PUT也可以用POST
PUT /fangyuan/_doc/1
{
"name":"地瓜",
"age":2,
"tags":"地瓜在睡觉"
}
POST /fangyuan/_doc/2
{
"name":"大黄",
"age":2,
"tags":"吃了就躺着"
}
#修改索引为fangyuan,类型为doc,id为2的内容
POST fangyuan/_doc/2/_update
{
"doc":{
"age":"2",
"tags":"吃饭睡觉打地瓜"
}
}
#这种方式如果没有id为2的就是添加,如果有就是修改,这种形式的修改相当于覆盖,没有的字段就是空
POST fangyuan/_doc/2
{
"doc":{
"age":"2",
"tags":"吃饭睡觉打地瓜"
}
}
#指定删除索引为fangyuan,类型为doc,id为2的信息
DELETE fangyuan/_doc/2
#删除fangyuan的索引
DELETE fangyuan
#删除所有索引
DELETE all
#获取以fangyuan为索引的信息
GET fangyuan
#获取索引为fangyuan,类型为doc,id为1的信息
GET fangyuan/_doc/1
#查询fangyuan,类型为doc,字段tags 包含’睡觉、睡、觉’的信息
GET /fangyuan/_doc/_search
{
"query": {
"match": {
"tags": "睡觉"
}
}
}
#查询索引为fangyuan的,如果age有关2的或tags是有关打架的
GET /fangyuan/_search
{
"query":{
"bool": {
"should":[
{"match":{
"age":"2"
}},
{"match":{
"tags":"打架"
}}
]
}
}
}
#查询索引为fangyuan的,如果age有关2的并且tags是有关打架的
GET /dog/_search
{
"query":{
"bool": {
"must":[
{"match":{
"age":"2"
}},
{"match":{
"tags":"打架"
}}
]
}
}
}
#查询索引为fangyuan的,如果age有关2的或tags是有关打架的,返回结果只有name字段
GET /dog/_search
{
"query":{
"bool": {
"should":[
{"match":{
"age":"2"
}},
{"match":{
"tags":"打架"
}}
]
}
},
"_source": ["name"]
}
#排序查询--age字段倒序
GET /fangyuan/_search
{
"query": {
"match": {
"tags": "睡觉"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
#查询结果高亮显示
GET /fangyuan/_search
{
"query": {
"match": {
"tags": "睡觉"
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {"tags":{}}
}
}
java配置文件
@Bean("restHighLevelClient")
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
return restHighLevelClient;
}
@GetMapping("/createIndexES")
@ApiOperation("创建索引")
public Object createIndexES(@RequestParam("index") String index) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
CreateIndexResponse response = null;
try {
response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@GetMapping("/dropIndexES")
@ApiOperation("删除索引")
public Object dropIndexES(@RequestParam("index") String ...index) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
AcknowledgedResponse response = null;
try {
response = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@GetMapping("/deleteIndex/{index}/{id}")
@ApiOperation("根据索引以及id删除内容")
public Object deleteIndex(@PathVariable("index") String index,@PathVariable("id") String id) {
DeleteRequest deleteRequest = new DeleteRequest(index, id);
DeleteResponse response = null;
try {
response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@PostMapping("/insertIndex")
@ApiOperation("创建索引内容")
public Object insertIndex(@RequestParam("index") String index,@RequestBody Map map) {
IndexRequest indexRequest = new IndexRequest(index.toLowerCase());//创建索引
indexRequest.timeout(Timevalue.timevalueSeconds(10));//设置超时时间
indexRequest.source(map,XContentType.JSON);
IndexResponse response = null;
try {
response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@GetMapping("/searchES")
@ApiOperation("查询es内容--分页")
public Object searchES(@RequestParam("index") String index,
@RequestParam("text") String text,
@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize) {
SearchRequest searchRequest = new SearchRequest(index);
SearchResponse response = null;
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("like", text);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(matchQuery);//查询条件
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("").postTags("").field("like");
builder.highlighter(highlightBuilder);
builder.from(pageNum);//页数
builder.size(pageSize);//每页条数
searchRequest.source(builder);
try {
response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
SearchHits searchHits = response.getHits();
return searchHits;
}
@PostMapping("/updateES/{index}/{id}")
@ApiOperation("根据索引以及id修改es内容")
public Object updateES(@RequestBody Map map,
@PathVariable("index")String index,
@PathVariable("id")String id) {
UpdateRequest request = new UpdateRequest(index, id);
request.doc(map,XContentType.JSON);
UpdateResponse response = null;
try {
response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@PostMapping("/bulkSavaES/{index}")
@ApiOperation("批量添加es内容")
public Object bulkSavaES(@PathVariable("index") String index,@RequestBody List欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)