栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch搜索引擎笔记一

elasticsearch搜索引擎笔记一

  • spring官网地址
  • Elastics earch 的官方地址: https://www.elastic.co/cn/
  • 数据类型种类

结构化数据:数据库mysql
非结构化数据:比如音频,视频等(nosql,redis,mangdb)
半结构化数据:xml,html等(redis,mangdb)

  • 数据格式

Elasticsearcs是面向文档型数据库,一条数据在这里就是一个文档。

ES里的 Index 可以看做一个库,而 Types 相当于表, documents 则相当于表的行。
这里Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个
type Elasticsearch 7.X 中 , Type 的概念已经被删除了。

es基础操作各种请求发送url以及格式(postman)------------------------------------------------------------------------------------------------------------------------------------------

  • 创建索引

在Postman 中,向 ES 服务器发 PUT 请求 http://127.0.0.1:9200/shopping


  • 查看单个索引

  • 查看所有索引

http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES
服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下


  • 删除索引

重新访问索引时,服务器返回响应:索引不存在

  • 创建文档

向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping /_doc


此处发送请求的方式必须为POST ,不能是 PUT ,否则会发生错误

如果想要自定义唯一性标识,需要在创建时指定http://127.0.0.1:9200/shopping/_doc/ 1001

此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为PUT

  • 查看文档

向 ES 服务器发 GET 请求 http://127.0.0.1:9200/sh opping /_doc/1


  • 查询全部文档

http://127.0.0.1:9200/shopping/_search

  • 全量修改文档

向 ES 服 务器发 POST 请求 http://127.0.0.1:9200/shopping /_doc/1


  • 局部修改字段

向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping/opping/_update/1

{
	"doc":{
	"address":"北京"
	}
}

  • 删除文档

向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/sh opping /_doc/1


如果删除一个并不存在的文档


  • 条件查询文档

http://127.0.0.1:9200/shopping/_search

{"query":{
    "match":{
        "address":"重庆"
    }
}
}

  • 条件全量查询

http://127.0.0.1:9200/shopping/_search

{"query":{
    "match_all":{
        
    }
}
}

  • 条件分页查询加排序

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "match_all":{
            
        }
},
    "from":5,
    "size":5,
    "_source":["address","age"],
    "sort":{
        "age":{
            "order":"asc"
        }
    }
}

  • 多个条件组合查询(and)

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "name":"冉述保"
                    }
                }
            ]
        }
    }
}

  • 多条件组合查询(or)

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"重庆"
                    }
                }
            ]
        }
    }
}

  • 多条件范围查询

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"重庆"
                    }
                }
            ],
            "filter":{
                "range":{
                    "age":{
                        "gt":26
                    }
                }
            }
        }
    }
}


  • 不分词,完全匹配查询且高亮显示

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "match_phrase":{
            "address":"北京"
        }
    },
    "highlight":{
        "fields":{
            "address":{}
        }
    }
}

  • 分组查询

http://127.0.0.1:9200/shopping/_search

{
    "aggs":{
        "age_group":{
            "terms":{
                "field":"age"
            }
        }
    },
    "size":0
}

  • 求平均值

http://127.0.0.1:9200/shopping/_search

{
    "aggs":{
        "age_avg":{
            "avg":{
                "field":"age"
            }
        }
    },
    "size":0
}

  • 新增映射关系

http://127.0.0.1:9200/user/_mapping

{
    "properties":{
        "name":{
            "type":"text",
            "index":true
        },
        "sex":{
            "type":"keyword",
            "index":true
        },
        "phone":{
            "type":"keyword",
            "index":false
        }
    }
}

  • 查询映射关系

http://127.0.0.1:9200/user/_mapping

  • 查询text(分词)-keyword(不分词,必须精准匹配)

http://127.0.0.1:9200/user/_search

{"query":{
    "match":{
        "name":"张"
    }
}
}

有结果

{"query":{
    "match":{
        "sex":"男"
    }
}
}

无结果

JavaAPI操作es(基于代码)---------------------------------------------------------------------------------------------------------------------

  • 准备工作- maven


    
        web_demo
        com.zgs.test
        0.0.1-SNAPSHOT
    
    4.0.0

    es-test

    
        
            org.elasticsearch
            elasticsearch
            7.8.0
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.8.0
        
        
        
            org.apache.logging.log4j
            log4j-core
            2.8.2
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.9
        
        
        
            junit
            junit
            4.12
        
    


  • 新建索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;


@Slf4j
public class EsTest_Index_Create {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建索引
        CreateIndexRequest request = new CreateIndexRequest("zgs1");
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        log.info("-----------"+response.toString());
        boolean acknowledged = response.isAcknowledged();
        System.out.println("acknowledged-------"+acknowledged);
        client.close();
    }
}

  • 查询索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;


@Slf4j
public class EsTest_Index_Search {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost",9200,"http")));

        //查询索引
        GetIndexRequest request = new GetIndexRequest("zgs");
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        log.info("#########################"+response.getAliases());
        log.info("#########################"+response.getDataStreams());
        log.info("#########################"+response.getMappings());
        log.info("#########################"+response.getSettings());
        client.close();
    }
}

  • 删除索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
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.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;


@Slf4j
public class EsTest_Index_Delete {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost",9200,"http"))
        );
        //删除索引

        DeleteIndexRequest request = new DeleteIndexRequest("zgs");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        log.info("#########################"+response.isAcknowledged());
        client.close();
    }
}

  • 新增文本信息
package com.zgs.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Insert {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        IndexRequest request = new IndexRequest();
        request.index("zgs").id("1001");

        ZgsVo zgsVo = new ZgsVo();
        zgsVo.setName("张贵松");
        zgsVo.setAge(24);
        zgsVo.setSex("男");
        //转换为json
        ObjectMapper mappers = new ObjectMapper();
        String zgsJson = mappers.writevalueAsString(zgsVo);
        request.source(zgsJson, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        log.info("########################"+response.getResult());
        client.close();
    }
}

  • 修改文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
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.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Update {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        UpdateRequest request = new UpdateRequest();
        request.index("zgs").id("1001");
        request.doc(XContentType.JSON,"name","张帅");
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.getGetResult());
        client.close();
    }
}

  • 查询文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
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.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Get {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        GetRequest request = new GetRequest();
        request.index("zgs").id("1001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.getSourceAsString());
        client.close();
    }
}

  • 删除文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
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.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Delete {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        DeleteRequest request = new DeleteRequest();
        request.index("zgs").id("1001");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.toString());
        client.close();
    }
}

  • 批量新增数据
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Insert_Batch {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new IndexRequest().index("zgs").id("1001").source(XContentType.JSON,"name","张帅1"));
        bulkRequest.add(new IndexRequest().index("zgs").id("1002").source(XContentType.JSON,"name","张帅2"));
        bulkRequest.add(new IndexRequest().index("zgs").id("1003").source(XContentType.JSON,"name","张帅3"));
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("#########################"+response.getIngestTook());
        log.info("#########################"+response.getItems());
        client.close();
    }
}

  • 批量删除数据
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;


@Slf4j
public class EsTest_Doc_Delete_Batch {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new DeleteRequest().index("zgs").id("1002"));
        bulkRequest.add(new DeleteRequest().index("zgs").id("1003"));
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("#########################"+response.getIngestTook());
        log.info("#########################"+response.getItems());
        client.close();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号