项目需要,引入了elastic search(后续简称es),后面将介绍本地对es的安装,使用以及java连接es查询的整个过程。
1、es索引字段建立与修改,以curl新增一个索引字段示例
curl -X PUT http://127.0.0.1:9200/idx_your_entity/_mapping/your_entity -H 'Content-Type: application/json' -H 'Postman-Token: 326dd921-4f7e-498b-866a-2de5173a908f' -H 'cache-control: no-cache' -d '{ "properties": { "is_enable":{ "type":"integer" } } }
2、使用logstash向es里面增量推数据,需要有update_date字段
input { jdbc { jdbc_driver_library => "D:/logstash-6.7.1/lib/sqljdbc4.jar" jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver" jdbc_connection_string => "jdbc:sqlserver://database_link;DatabaseName=HIM_bak;" jdbc_user => "username" jdbc_password => "password" schedule => "* * * * *" jdbc_default_timezone => "Asia/Shanghai" use_column_value => false tracking_column => "update_date" statement => "select [fields you need] from table WHERE update_date > :sql_last_value" } } output { elasticsearch { index => "idx_workhelp" document_type => "workhelp" document_id => "%{helpno}" hosts => ["127.0.0.1:9200"] } }
3、es查询jar包,pom配置
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.7.1</version> </dependency>
4、es数据源,查询client配置
@Bean public TransportClient client() throws Exception { // init client TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), port); Settings settings = Settings.builder().put("cluster.name", nodeName).build(); client = new PreBuiltTransportClient(settings); client.addTransportAddress(transportAddress); logger.info("use es and elastic search connect successful"); return client; }
5、es查询条件的构建:这里介绍两种查询条件的构建,must表示且,should表示或;同时用到了两种匹配方式:termQuery代表完全匹配,配合索引类型 keywords 使用,matchQuery代表模糊匹配,配合text使用。
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); //示例 A=a and B like %b% and (C like %c% or D like %d%) boolQueryBuilder.filter(QueryBuilders.termQuery(fieldA, a)); boolQueryBuilder.must(QueryBuilders.matchQuery(fieldB, b)); boolQueryBuilder.must( QueryBuilders.boolQuery() .should(QueryBuilders.matchQuery(fieldC, c)) .should(QueryBuilders.matchQuery(fieldD, d)));
6、设置关键词高亮显示
highlightBuilder.preTags("<span class='highLight'>"); highlightBuilder.postTags("</span>"); highlightBuilder.field(field);
7、把条件带入并且执行查询,hits即查询结构,需要的数据类型SearchHits结构不复杂,这里不再赘叙。
SearchRequestBuilder searchRequestBuilder = client .prepareSearch(INDEX) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(boolQueryBuilder); searchRequestBuilder.highlighter(problemDescHiBuilder); // 分页 searchRequestBuilder.setFrom(startIndex); searchRequestBuilder.setSize(pageSize); // 排序 searchRequestBuilder .addSort(SortBuilders.fieldSort(CREATEDATE)) .addSort(SortBuilders.fieldSort(READCOUNT) .order(SortOrder.DESC)); SearchResponse response = searchRequestBuilder.get(); SearchHits hits = searchRequestBuilder.get().getHits();
8、查询结果。查询结果即对SearchHits进行处理,转化为自己项目的视图对象,一般为 业务实体VO 的命名方式进行表达,传到控制层。
9、最后:写到这里,大家有问题可以联系我共同学习探讨~