zoukankan      html  css  js  c++  java
  • Java操作ElasticSearch

    maven引入

    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.2.3</version>
    </dependency>

    <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.2.3</version>
    <exclusions>
    <exclusion>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    </exclusion>
    </exclusions>
    </dependency>

    1、创建索引

    @Override
    public DataWrapper<Void> createIndex(String index, String mapping) {//其中mapping对应下表格中非标红的的定义
    DataWrapper<Void> dw = new DataWrapper<Void>();
    try {
    client.admin().indices().prepareCreate(index).setSource(mapping, XContentType.JSON).execute().actionGet();
    } catch (Exception e) {
    log.error(e.getMessage(), e);
    dw.setCallStatus(CallStatusEnum.FAILED);
    dw.setMessage(e.getMessage());
    }
    return dw;
    }

    创建索引的mapping:

    put test
    {
    "mappings": {
    "test": {
    "properties": {
    "name": {
    "type": "keyword"
    },
    "question": {
    "type": "text",
    "analyzer": "ik"
    }
    }
    }
    },
    "settings": {
    "number_of_shards": "5",
    "analysis": {
    "analyzer": {
    "ik": {
    "type": "standard",
    "tokenizer": "ik_max_word"
    }
    }
    },
    "number_of_replicas": "1"
    }
    }

    2、删除索引

    @Override
    public DataWrapper<Void> deleteIndex(String index) {
    DataWrapper<Void> dw = new DataWrapper<Void>();
    try {
    client.admin().indices().prepareDelete(index).execute().actionGet();
    } catch (Exception e) {
    log.error(e.getMessage(), e);
    dw.setCallStatus(CallStatusEnum.FAILED);
    dw.setMessage(e.getMessage());
    }
    return dw;
    }

    3、修改索引

    ①添加字段

    @Override
    public DataWrapper<Void> updateIndex(String index, String type, String mapping) {//其中mapping对应下表格中非标红的的定义
    DataWrapper<Void> dw = new DataWrapper<Void>();
    try {
    client.admin().indices().preparePutMapping(index).setType(type).setSource(mapping, XContentType.JSON).execute().actionGet();
    } catch (Exception e) {
    log.error(e.getMessage(), e);
    dw.setCallStatus(CallStatusEnum.FAILED);
    dw.setMessage(e.getMessage());
    }
    return dw;
    }

    put test/test/_mapping
    {
    "properties": {
    "code": {
    "type": "keyword"
    }
    }
    }

    ②修改索引配置

    @SuppressWarnings("unused")
    @Override
    public DataWrapper<Void> updateSetting(String index, String type, String mapping) {//其中mapping对应下表格中非标红的的定义
    DataWrapper<Void> dw = new DataWrapper<Void>();
    try {
    UpdateSettingsRequest request = new UpdateSettingsRequest(index);
    request.settings(mapping, XContentType.JSON);

    boolean acknowledged = client.admin().indices().updateSettings(request).actionGet().isAcknowledged();
    } catch (Exception e) {
    log.error(e.getMessage(), e);
    dw.setCallStatus(CallStatusEnum.FAILED);
    dw.setMessage(e.getMessage());
    }
    return dw;
    }

    PUT test/_settings
    {
    "index":{
    "max_result_window":50000
    }
    }

  • 相关阅读:
    Algs4-1.1.17找出以下递归函数的问题
    Algs4-1.1.16给出exR1(6)的返回值
    Algs4-1.1.15编写一个静态方法histogram()
    Algs4-1.1.14实现以2为底的对数
    Algs4-1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列)
    Algs4-1.1.12以下代码段会打印出什么结果?
    python将一个列表的元素随机打乱
    python 类似java的三目运算符
    java的三元运算符
    java 的Colections类(Java也有python类似列表的反转、排序等方法)
  • 原文地址:https://www.cnblogs.com/wangymd/p/11139856.html
Copyright © 2011-2022 走看看