码迷,mamicode.com
首页 > 其他好文 > 详细

es的总结

时间:2020-12-11 12:09:24      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:one   lower   ring   动态   stat   analyzer   string   case   ted   

一 索引模板(用于通过模板匹配匹配到的所有索引)  详细请点击

{
  "order": 0,                               // 模板优先级
  "template": "sample*",               // 模板匹配的名称方式
  "settings": {...},                        // 索引设置
  "mappings": {...},                        // 索引中各字段的映射定义
  "aliases": {...}                          // 索引的别名
}
技术图片

(1)模板优先级:优先级越高,越优先执行,优先级高的会覆盖优先级的索引。

(2)模板匹配:当新建的索引时,所有以sample开头索引的会自动匹配该索引模板。

(3)索引设置:一般定义的有主分片,拷贝分片,自定义分析器,刷新时间,

                           一般分析器中有过滤器、分词器、标记过滤器。

                     映射字符过滤器(mapping char filter)、HTML过滤器(HTML Strip char filter)、格式替换过滤器(Pattern Replace char filter)。html_strip 字符过滤器去除所有的 HTML 标签。

(4)索引映射定义:有动态映射,自定义映射。

(5)索引别名:利用索引别名,可以实现零停机时间重新索引。

二 传统数据库与ES之间对比

关系数据库es
DataBase index
Table type
Row Document
Column Field

 

 

 

 

 

注:type  在es 7.0以后会为es创建唯一一个映射类型_doc

三 以下的内容来源是 es的官网 

第一类:是关于index模板的API

在Index APIs 是创建索引的也就是相当于创建库,

一 Document APIs中的方法

1  CreateIndexRequest:这个请求时创建一个索引,在es中的索引相当于mysql 中创建一个数据库(create database XX)。

     settings()方法 中会设置主分区,拷贝分区,刷新时间。

     mapping()方法配置映射的。

     aliases()方法是给模板请别名。

以下是测试方法,请准备好开发环境(下载安装es及其es head可视化工具)

public final static String ES_COLLECTION_NAME="first";
public ResponseData buildIndexDB() throws IOException {
        CreateIndexRequest createIndexRequest=new CreateIndexRequest();
        createIndexRequest.index(ES_COLLECTION_NAME);
        //setting中会设置主分区,拷贝分区,刷新时间
        createIndexRequest.settings(Settings.builder().put("index.number_of_shards","3")
                          .put("index.number_of_replicas","2"));
        Map<String,Object> message=new HashMap<>();
        message.put("type","text");
        Map<String,Object> properties=new HashMap<>();
        properties.put("message",message);
        Map<String,Object> mapping=new HashMap<>();
        mapping.put("mapping",mapping);
        //动态映射
        createIndexRequest.mapping( "{\n" +
                        "  \"properties\": {\n" +
                        "    \"message\": {\n" +
                        "      \"type\": \"text\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}",
                XContentType.JSON);
        //起别名
        createIndexRequest.aliases("hanqiang");
        //可以为索引设置setting,mapping,aliases
        /*createIndexRequest.source("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 1,\n" +
                "        \"number_of_replicas\" : 0\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"properties\" : {\n" +
                "            \"message\" : { \"type\" : \"text\" }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", XContentType.JSON);*/
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        CreateIndexResponse 
createIndexResponse=esClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
        String name=createIndexResponse.index();
        System.out.println("获得创建的索引名称 "+name);
        RestClientConfig.close(esClient);
        return ResponseData.success();
    }
 public ResponseData createIndex() throws IOException {
        CreateIndexRequest createIndexRequest=new CreateIndexRequest();
        createIndexRequest.index(ES_COLLECTION_NAME);
        //对于索引设置setting属性
        createIndexRequest.settings(Settings.builder()
                //主分片的个数
                .put("index.number_of_shards",3)
                //主分片的拷贝分片个数
                .put("index.number_of_replicas",2)
        );
        Map<String,Object> message=new HashMap<>();
        message.put("type","text");
        // message.put("analyzer","lowercase_analyzer");
        // message.put("index","analyzed");
        Map<String,Object> propertise=new HashMap<>();
        propertise.put("message",message);
        Map<String,Object> mapping=new HashMap<>();
        mapping.put("propertise",propertise);
        createIndexRequest.mapping(String.valueOf(mapping));
        //以下是通过source方法直接就可以设置   settings,mappings,aliases
        /*createIndexRequest.source("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 1,\n" +
                "        \"number_of_replicas\" : 0\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"properties\" : {\n" +
                "            \"message\" : { \"type\" : \"text\" }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", XContentType.JSON);*/
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        //同步
        CreateIndexResponse createIndexResponse=esClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
        //获取索引名称
        String name=createIndexResponse.index();
        //插入一条数据
        Users user=new Users();
        user.setAge(10);
        user.setName("han");
        esClient.index(new IndexRequest(name).source(user, XContentType.JSON),RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success();
    }
技术图片

2  DeleteIndexRequest是删除索引模板。

    timeout()设置超时时间。

    masterNodeTimeout()主节点超时时间。

 public ResponseData deleteIndex() throws IOException {
        DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest(ES_COLLECTION_NAME);
        RestHighLevelClient esClient= RestClientConfig.restHighLevelClient();
        AcknowledgedResponse deleteIndexResponse=esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(deleteIndexResponse);
    }
技术图片

3   GetIndexRequest判断索引是否存在

 public ResponseData existsIndex() throws IOException {
        GetIndexRequest getIndexRequest=new GetIndexRequest(ES_COLLECTION_NAME);
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        Boolean flag=esClient.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(flag);
    }
技术图片

第二类:是关于插入文本的API

1  IndexRequest 添加文本数据

public ResponseData saveDocument() throws IOException {
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        Map<String,Object> jsonMap=new HashMap(2);
        jsonMap.put("name","han");
        jsonMap.put("age","19");
        jsonMap.put("sex","男");
        jsonMap.put("numberId","11XXXXXXX1213x");
        IndexRequest in=new IndexRequest(ES_COLLECTION_NAME).source(jsonMap, XContentType.JSON);
        IndexResponse indexResponse=esClient.index(in, RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(indexResponse);
    }
技术图片

2   BulkRequest  批量插入

  public ResponseData bulkDocument() throws IOException {
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        BulkRequest bulkRequest=new BulkRequest();
        for(int i=0;i<6;i++){
            Map<String,Object> jsonMap=new HashMap();
            jsonMap.put("name","han*"+i);
            jsonMap.put("age","19*"+i);
            jsonMap.put("sex","男*"+i);
            jsonMap.put("numberId","11XXXXXXX1213*"+i);
            bulkRequest.add(new IndexRequest(ES_COLLECTION_NAME).source(jsonMap,XContentType.JSON));
        }
        BulkResponse bulkResponse=esClient.bulk(bulkRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(bulkResponse);
    }
技术图片

2.1  批量操作

  public ResponseData bulkCustomDocument() throws IOException {
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        BulkRequest bulkRequest=new BulkRequest();
        //修改指定数据
        Map<String,Object> jsonMap=new HashMap<>(5);
        jsonMap.put("name","bulkCustomDocument方法的测试");
        jsonMap.put("age","18测试");
        jsonMap.put("sex","男测试");
        jsonMap.put("numberId","1502222XXXXX2");
        UpdateRequest updateRequest=new UpdateRequest(ES_COLLECTION_NAME,"Un0QDHUB_jnDNOQPxN3B");
        updateRequest.doc(jsonMap);
        bulkRequest.add(updateRequest);
        //删除指定数据
        DeleteRequest deleteRequest=new DeleteRequest(ES_COLLECTION_NAME,"U30QDHUB_jnDNOQPxN3B");
        bulkRequest.add(deleteRequest);

        BulkResponse bulkResponse=esClient.bulk(bulkRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(bulkResponse);
    }
技术图片

3 UpdateRequest  修改操作

  public ResponseData updateDocument() throws IOException {
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();
        //修改数据
        Map<String,Object> jsonMap=new HashMap(5);
        jsonMap.put("name","qiaa测试");
        jsonMap.put("age","20测试");
        jsonMap.put("sex","女测试");
        jsonMap.put("numberId","111222XXXXX2");
        UpdateRequest updateRequest=new UpdateRequest(ES_COLLECTION_NAME,"QX0ODHUB_jnDNOQP9t0P");
        updateRequest.doc(jsonMap);

        UpdateResponse updateResponse =esClient.update(updateRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(updateResponse);
    }
技术图片

4 DeleteRequest 删除操作

 public ResponseData deleteDocument() throws IOException{
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();

        DeleteRequest deleteRequest=new DeleteRequest(ES_COLLECTION_NAME,"OJuOv3QBEFBKZIVoeLzv");
        DeleteResponse deleteResponse=esClient.delete(deleteRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(deleteResponse);
    }
技术图片

5 GetRequest判断是否存在

  public ResponseData getDocument() throws IOException {
        //连接数据库
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();

        GetRequest getRequest=new GetRequest(ES_COLLECTION_NAME,"Un0QDHUB_jnDNOQPxN3B");
        //由于exists()仅返回true或false,因此我们建议您关闭提取功能_source和所有存储的字段,这样请求的内容会稍微减轻一些
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        //禁用获取存储的字段。
        getRequest.storedFields("_none_");
        GetResponse getResponse=esClient.get(getRequest,RequestOptions.DEFAULT);
        //用完,之后一定养成关闭数据库的好习惯。
        RestClientConfig.close(esClient);
        return ResponseData.success(getResponse);
    }
技术图片

第三类:是关于搜索文本的API

1 SearchRequest 查询全部数据

 public ResponseData searchAll() throws IOException {
        RestHighLevelClient esClient= RestClientConfig.restHighLevelClient();

        SearchRequest searchRequest=new SearchRequest(ES_COLLECTION_NAME);
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse=esClient.search(searchRequest, RequestOptions.DEFAULT);
        //关闭连接
        RestClientConfig.close(esClient);
        return ResponseData.success(searchResponse);
    }
技术图片

2 多条件查询

 public ResponseData manySearch() throws IOException {
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();

        MultiSearchRequest multiSearchRequest=new MultiSearchRequest();

        SearchRequest first=new SearchRequest(ES_COLLECTION_NAME);
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("sex","女测试"));
        first.source(searchSourceBuilder);
        multiSearchRequest.add(first);

        SearchRequest two=new SearchRequest(ES_COLLECTION_NAME);
        SearchSourceBuilder searchSourceBuilder1=new SearchSourceBuilder();
        searchSourceBuilder1.query(QueryBuilders.matchQuery("age","20"));
        two.source(searchSourceBuilder1);
        multiSearchRequest.add(two);

        MultiSearchResponse searchResponse=esClient.msearch(multiSearchRequest,RequestOptions.DEFAULT);
        //关闭连接
        RestClientConfig.close(esClient);
        return ResponseData.success(searchResponse);
    }
技术图片

3 CountRequest 计数查询

  public ResponseData countSearch() throws IOException {
        RestHighLevelClient esClient=RestClientConfig.restHighLevelClient();

        CountRequest countRequest=new CountRequest(ES_COLLECTION_NAME);
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        countRequest.source(searchSourceBuilder);

        CountResponse countResponse=esClient.count(countRequest,RequestOptions.DEFAULT);
        //关闭连接
        RestClientConfig.close(esClient);
        return ResponseData.success(countResponse);
    }
技术图片

 

这是一个公共方法连接和关闭es 

@Configuration
@Slf4j
public class RestClientConfig {

    public static RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
        //如果是集群
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")
//                        , new HttpHost("127.0.0.1", 9201, "http")
//                        , new HttpHost("127.0.0.1", 9202, "http")));
        return client;
    }
    /**
     * 关闭es连接
     * @param client es客户端
     * @throws IOException io异常
     */
    public static void close(RestHighLevelClient client) {
        try {
            client.close();
        } catch (IOException e) {
            log.error("关闭es连接失败!",e);
        }
    }
技术图片

es的总结

标签:one   lower   ring   动态   stat   analyzer   string   case   ted   

原文地址:https://www.cnblogs.com/linyu1/p/14096263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!