在java应用中使用es简单介绍-1

索引模板设置

ES提供索引新文档时自动自动新增索引的能力,但是由于其动态映射的结果可能不符合预期, 因此采用设置映射模板,通过索引名匹配进行映射设置。

    分片设置
  1. 主分片:默认为5,直接采用
  2. 副分片:提供故障转移能力、读能力,设置成1,过大回到只写效率较低
    编写模板
  1. template:用于匹配index
  2. settings:由于设置索引的分片等信息
  3. mapping:用于设置映射,关于类型type,由于es6默认写_doc即可。之后不再推荐多type设置,由于es中每个索引内部相同名称字段其类型是一样的,因此如果有多个type,对于使用者来说,往往容易将不同type中的相同字段设置成不同的问题。
  4. doc_values:代表是否用于聚合,会生成doc_values数据,用于聚合。es的倒排索引用于指导关键词匹配到文档,而doc_value相当于反向的倒排索引,用于高效的将文档按照设定字段进行聚合。
    索引模板设置
PUT _template/template1
{
  "template": "repeater_replay_*",
  "settings": {
    "index.number_of_shards": 5,
    "number_of_replicas": 1
  },
  "mappings": {
    "_doc": {
      "properties": {
	    "record_id": {   
            "type": "keyword"      
        },
        "task_id": {   
            "type": "keyword"      
        },
        "task_run_id": {
          "type": "keyword", 
		  "doc_values": true
        },
        "app_id": {
          "type": "keyword"
        },
        "app_name": {
          "type": "keyword"
        },
        "environment": {
          "type": "keyword"
        },
        "host": {
          "type": "keyword"
        },
        "trace_id": {
          "type": "keyword"
        },
        "status": {
          "type": "byte"
        },
        "cost": {
          "type": "long"
        },
        "diff_result": {
          "type": "text", 
		  "index": false
        },
        "response": {
          "type": "text", 
		  "index": false
        },
		"mock_invocation": {
          "type": "text", 
		  "index": false
        }
      }
    }
  }
}

客户端

选择

根据官网建议使用Rest High Level的client。为了防止被springboot父工程的项目覆盖,故采用重复依赖来解决。在选择client版本时需要注意,Rest High Level的版本需要和es服务器版本对应,至少大版本是要对应的。

<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-high-level-client</artifactId>
	<version>6.3.2</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>6.3.2</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-client</artifactId>
	<version>6.3.2</version>
</dependency>

使用

    初始化
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHosts));
    索引(新增数据)
IndexRequest request = new IndexRequest(realIndex, type).source(docSource, XContentType.JSON).timeout("1s").opType("index");
client.index(request);
    搜索
SearchRequest searchRequest = new SearchRequest("index-*").preference("_local");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("price", 20)));
searchRequest.source(sourceBuilder);
client.search(searchRequest);

这种属于精确查询,请求体如下:

{
          
   
    "query" : {
          
   
        "constant_score" : {
          
    
            "filter" : {
          
   
                "term" : {
          
    
                    "price" : 20
                }
            }
        }
    }
}
    其他 还有聚合、删除等操作,都是比较类似的,详细参考

总结

已经学会设置索引模板设置,客户端也学会了如何编写,在后续使用过程中,如果有别的新的,再继续总结。

经验分享 程序员 微信小程序 职场和发展