項目須要,引入了elastic search(後續簡稱es),後面將介紹本地對es的安裝,使用以及java鏈接es查詢的整個過程。java
一、es索引字段創建與修改,以curl新增一個索引字段示例node
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" } } }
二、使用logstash向es裏面增量推數據,須要有update_date字段sql
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"] } }
三、es查詢jar包,pom配置json
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.7.1</version> </dependency>
四、es數據源,查詢client配置app
@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; }
五、es查詢條件的構建:這裏介紹兩種查詢條件的構建,must表示且,should表示或;同時用到了兩種匹配方式:termQuery表明徹底匹配,配合索引類型 keywords 使用,matchQuery表明模糊匹配,配合text使用。curl
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)));
六、設置關鍵詞高亮顯示elasticsearch
highlightBuilder.preTags("<span class='highLight'>"); highlightBuilder.postTags("</span>"); highlightBuilder.field(field);
七、把條件帶入而且執行查詢,hits即查詢結構,須要的數據類型SearchHits結構不復雜,這裏再也不贅敘。sqlserver
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();
八、查詢結果。查詢結果即對SearchHits進行處理,轉化爲本身項目的視圖對象,通常爲 業務實體VO 的命名方式進行表達,傳到控制層。post
九、最後:寫到這裏,你們有問題能夠聯繫我共同窗習探討~學習