ElasticSearch6.0 Java API 使用 排序,分组 ,创建索引,添加索引数据,打分等
如果此文章对你有帮助,请关注一下哦
1.1 搭建maven 工程 创建web工程
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>com.elastic</groupId>
5 <artifactId>search-service</artifactId>
6 <packaging>war</packaging>
7 <version>1.0-SNAPSHOT</version>
8 <name>search-service Maven Webapp</name>
9 <url>http://maven.apache.org</url>
10 <properties>
11 <junit.version>4.5</junit.version>
12 <spring.version>4.2.4.RELEASE</spring.version>
13 <mybatis.version>3.2.8</mybatis.version>
14 <mybatis.spring.version>1.2.2</mybatis.spring.version>
15 <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
16 <mysql.version>5.1.32</mysql.version>
17 <slf4j.version>1.6.4</slf4j.version>
18 <jackson.version>2.4.2</jackson.version>
19 <druid.version>1.0.9</druid.version>
20 <jstl.version>1.2</jstl.version>
21 <servlet-api.version>2.5</servlet-api.version>
22 <jsp-api.version>2.0</jsp-api.version>
23 <joda-time.version>2.5</joda-time.version>
24 <commons-lang3.version>3.3.2</commons-lang3.version>
25 <commons-io.version>1.3.2</commons-io.version>
26 <commons-net.version>3.3</commons-net.version>
27 <jsqlparser.version>0.9.1</jsqlparser.version>
28 <commons-fileupload.version>1.3.1</commons-fileupload.version>
29 <jedis.version>2.7.2</jedis.version>
30 <activemq.version>5.11.2</activemq.version>
31 <quartz.version>2.2.2</quartz.version>
32 <slf4j.version>1.7.21</slf4j.version>
33 <log4j.version>1.2.12</log4j.version>
34 <solr.version>6.1.0</solr.version>
35 <fastjson.version>1.2.13</fastjson.version>
36 <diamond.version>0.0.1-SNAPSHOT</diamond.version>
37 <cors.filter>2.5</cors.filter>
38 <java.property.utils>1.10</java.property.utils>
39 <jackson.version>2.4.2</jackson.version>
40 </properties>
41 <dependencies>
42 <dependency>
43 <groupId>junit</groupId>
44 <artifactId>junit</artifactId>
45 <version>3.8.1</version>
46 <scope>test</scope>
47 </dependency>
48 <dependency>
49 <groupId>org.elasticsearch.client</groupId>
50 <artifactId>transport</artifactId>
51 <version>6.0.0</version>
52 </dependency>
53 <dependency>
54 <groupId>org.slf4j</groupId>
55 <artifactId>jcl-over-slf4j</artifactId>
56 <version>1.7.21</version>
57 <scope>runtime</scope>
58 </dependency>
59 <dependency>
60 <groupId>org.slf4j</groupId>
61 <artifactId>slf4j-api</artifactId>
62 <version>1.7.21</version>
63 </dependency>
64 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
65 <dependency>
66 <groupId>org.slf4j</groupId>
67 <artifactId>slf4j-log4j12</artifactId>
68 <scope>runtime</scope>
69 </dependency>
70 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
71 <dependency>
72 <groupId>log4j</groupId>
73 <artifactId>log4j</artifactId>
74 <version>1.2.17</version>
75 </dependency>
76 <dependency>
77 <groupId>commons-logging</groupId>
78 <artifactId>commons-logging</artifactId>
79 <version>1.2</version>
80 </dependency>
81 <dependency>
82 <groupId>junit</groupId>
83 <artifactId>junit</artifactId>
84 <scope>test</scope>
85 </dependency>
86 <dependency>
87 <groupId>javax.servlet</groupId>
88 <artifactId>servlet-api</artifactId>
89 <version>2.5</version>
90 <scope>provided</scope>
91 </dependency>
92 <dependency>
93 <groupId>com.alibaba</groupId>
94 <artifactId>fastjson</artifactId>
95 </dependency>
96 <!-- spring dependencies -->
97 <dependency>
98 <groupId>org.springframework</groupId>
99 <artifactId>spring-core</artifactId>
100 <version>${spring.version}</version>
101 <!--<exclusions>-->
102 <!--<exclusion>-->
103 <!--<groupId>commons-logging</groupId>-->
104 <!--<artifactId>commons-logging</artifactId>-->
105 <!--</exclusion>-->
106 <!--</exclusions>-->
107 </dependency>
108 <dependency>
109 <groupId>org.springframework</groupId>
110 <artifactId>spring-test</artifactId>
111 <version>${spring.version}</version>
112 </dependency>
113 <dependency>
114 <groupId>org.springframework</groupId>
115 <artifactId>spring-context</artifactId>
116 <version>${spring.version}</version>
117 </dependency>
118 <dependency>
119 <groupId>org.springframework</groupId>
120 <artifactId>spring-context-support</artifactId>
121 <version>${spring.version}</version>
122 </dependency>
123 <dependency>
124 <groupId>org.springframework</groupId>
125 <artifactId>spring-aop</artifactId>
126 <version>${spring.version}</version>
127 </dependency>
128 <dependency>
129 <groupId>org.springframework</groupId>
130 <artifactId>spring-aspects</artifactId>
131 <version>${spring.version}</version>
132 </dependency>
133 <dependency>
134 <groupId>org.springframework</groupId>
135 <artifactId>spring-tx</artifactId>
136 <version>${spring.version}</version>
137 </dependency>
138 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
139 <dependency>
140 <groupId>org.springframework</groupId>
141 <artifactId>spring-jdbc</artifactId>
142 <version>${spring.version}</version>
143 </dependency>
144
145 <dependency>
146 <groupId>org.springframework</groupId>
147 <artifactId>spring-webmvc</artifactId>
148 <version>${spring.version}</version>
149 </dependency>
150 <!-- mysql dependencies -->
151 <dependency>
152 <groupId>mysql</groupId>
153 <artifactId>mysql-connector-java</artifactId>
154 <version>5.1.32</version>
155 </dependency>
156 <!-- https://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter -->
157 <dependency>
158 <groupId>com.thetransactioncompany</groupId>
159 <artifactId>cors-filter</artifactId>
160 <version>${cors.filter}</version>
161 </dependency>
162 <!-- https://mvnrepository.com/artifact/com.thetransactioncompany/java-property-utils -->
163 <dependency>
164 <groupId>com.thetransactioncompany</groupId>
165 <artifactId>java-property-utils</artifactId>
166 <version>${java.property.utils}</version>
167
168 </dependency>
169 <dependency>
170 <groupId>com.alibaba</groupId>
171 <artifactId>fastjson</artifactId>
172 <version>${fastjson.version}</version>
173 </dependency>
174 </dependencies>
175 <build>
176 <finalName>search-service</finalName>
177 </build>
178 </project>
1.2 创建web项目
2.1 编写Java elasticsearch 客户端连接公共类 (这里使用elasticsearch单机版)
1 package com.elastic.common.conn;
2
3 import org.elasticsearch.client.transport.TransportClient;
4 import org.elasticsearch.common.settings.Settings;
5 import org.elasticsearch.common.transport.TransportAddress;
6 import org.elasticsearch.transport.client.PreBuiltTransportClient;
7 import org.springframework.stereotype.Component;
8
9 import java.net.InetAddress;
10
11 /**
12 * Created by xiaotian on 2017/12/1.
13 */
14 @Component
15 public class EsClient {
16
17 //private EsClient client = new EsClient();
18 TransportClient client = null;
19 public EsClient(){
20 try{
21 Settings settings = Settings.builder()
22 .put("client.transport.sniff", true)
23 .put("cluster.name", "elasticsearch").build();
24 client = new PreBuiltTransportClient(Settings.EMPTY)
25 .addTransportAddress(new TransportAddress(InetAddress.getByName("eshost"), 9300));
26
27 }catch (Exception ex){
28 client.close();
29 }finally {
30
31 }
32 }
33 public TransportClient getConnection(){
34
35 if (client==null){
36 synchronized (EsClient.class){
37 if (client==null){
38 new EsClient();
39 }
40 }
41 }
42 return client;
43
44 }
45
46 }
2.2 service层 接口设计
1. IndexService
1 package com.elastic.service.inter;
2
3 /**
4 * Created by xiaotian on 2017/12/1.
5 */
6 public interface IndexService {
7
8 public void index(String id);
9
10 public void get();
11 public void del(String id);
12 public void update(String id) throws Exception;
13 public void multiGet(String ... ids) throws Exception;
14 public void bulk(String ... ids) throws Exception;
15 public void bulkProcesstor(String index,String type,String... ids) throws Exception;
16
17 }
2. SearchService
1 package com.elastic.service.inter;
2
3 /**
4 * Created by xiaotian on 2017/12/2.
5 */
6 public interface SearchService {
7 public void search();
8 public void searchByCondition() throws Exception;
9 public void multiSearch();
10 public void aggsearch();
11 public void metricsAgg();
12
13 }
3. service层 接口实现
IndexServiceImpl
1 package com.elastic.service.impl;
2
3 import com.alibaba.fastjson.JSONObject;
4 import com.elastic.common.conn.EsClient;
5 import com.elastic.service.inter.IndexService;
6 import org.elasticsearch.action.ActionListener;
7 import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
8 import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
9 import org.elasticsearch.action.bulk.*;
10 import org.elasticsearch.action.get.GetResponse;
11 import org.elasticsearch.action.get.MultiGetItemResponse;
12 import org.elasticsearch.action.get.MultiGetResponse;
13 import org.elasticsearch.action.index.IndexRequest;
14 import org.elasticsearch.action.index.IndexRequestBuilder;
15 import org.elasticsearch.action.index.IndexResponse;
16 import org.elasticsearch.client.Requests;
17 import org.elasticsearch.common.document.DocumentField;
18 import org.elasticsearch.common.unit.ByteSizeUnit;
19 import org.elasticsearch.common.unit.ByteSizeValue;
20 import org.elasticsearch.common.unit.TimeValue;
21 import org.elasticsearch.common.xcontent.XContentBuilder;
22 import org.elasticsearch.common.xcontent.XContentType;
23 import org.elasticsearch.index.query.QueryBuilders;
24 import org.elasticsearch.index.reindex.BulkByScrollResponse;
25 import org.elasticsearch.index.reindex.DeleteByQueryAction;
26 import org.elasticsearch.rest.RestStatus;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29
30 import java.io.IOException;
31 import java.util.*;
32
33 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
34
35 /**
36 * Created by xiaotian on 2017/12/1.
37 */
38 @Service
39 public class IndexServiceImpl implements IndexService {
40
41 @Autowired
42 private EsClient client;
43
44 public void index(String id) {
45 // String json = "{" +
46 // "\"user\":\"kimchy\"," +
47 // "\"postDate\":\"2013-01-30\"," +
48 // "\"message\":\"trying out Elasticsearch\"" +
49 // "}";
50 try {
51
52
53 Map<String, Object> jsonMap = new HashMap<String, Object>();
54 jsonMap.put("name","jim"+id);
55 jsonMap.put("age",20+id);
56 jsonMap.put("date",new Date());
57 jsonMap.put("message","测试"+id);
58 jsonMap.put("tel","1234567");
59 //IndexResponse indexResponse = client.getConnection().prepareIndex("twitter", "tweet").setSource(JSONObject.toJSON(jsonMap), XContentType.JSON).get();
60 IndexResponse indexResponse = client.getConnection().prepareIndex("xiaot", "test",id).setSource(jsonMap).get();
61 // Index name
62 String _index = indexResponse.getIndex();
63 // Type name
64 String _type = indexResponse.getType();
65 // Document ID (generated or not)
66 String _id = indexResponse.getId();
67 // Version (if it's the first time you index this document, you will get: 1)
68 long _version = indexResponse.getVersion();
69 // status has stored current instance statement.
70 RestStatus status = indexResponse.status();
71 System.out.println(_index+"_"+_type+"_"+_id+"_"+_version+"_"+status);
72 }catch (Exception ex){
73 ex.printStackTrace();
74 }
75
76 }
77
78 public void get() {
79 GetResponse response = client.getConnection().prepareGet("twitter", "tweet", "94pKEWABJOgzR6sJVCCV").get();
80 Map<String, DocumentField> fields = response.getFields();
81 System.out.println("map:"+fields);
82 String index = response.getIndex();
83 Map<String, Object> source = response.getSource();
84 String id = response.getId();
85 System.out.println(source);
86
87 }
88
89 public void del(String id) {
90 DeleteByQueryAction.INSTANCE.newRequestBuilder(client.getConnection())
91 .filter(QueryBuilders.matchQuery("_id",id))
92 .source("twitter")
93 .execute(new ActionListener<BulkByScrollResponse>() {
94 public void onResponse(BulkByScrollResponse response) {
95 long deleted = response.getDeleted();
96 System.out.println("delete"+deleted);
97 }
98
99 public void onFailure(Exception e) {
100
101 }
102 });
103 }
104
105 public void update(String id) throws Exception{
106 client.getConnection().prepareUpdate("twitter","tweet",id)
107 .setDoc(jsonBuilder()
108 .startObject()
109 .field("name", "tom")
110 .endObject()).get();
111 }
112
113 public void multiGet(String... ids) throws Exception {
114 MultiGetResponse multiGetResponse = client.getConnection().prepareMultiGet()
115 .add("twitter", "tweet", ids[0])
116 .add("twitter", "tweet", ids[1], ids[2], ids[3])
117 .get();
118 for (MultiGetItemResponse multiGetItemResponse : multiGetResponse) {
119 GetResponse response = multiGetItemResponse.getResponse();
120 if (response.isExists()){
121 System.out.println(response.getSourceAsString());
122 }
123 }
124 }
125
126 public void bulk(String... ids) throws Exception {
127 BulkRequestBuilder prepareBulk = client.getConnection().prepareBulk();
128 for (String id : ids) {
129 prepareBulk.add(client.getConnection().prepareIndex("twitter","tweet",id)
130 .setSource(jsonBuilder().startObject().field("name","肖添"+id).endObject()));
131
132 }
133 BulkResponse responses = prepareBulk.get();
134 System.out.println(responses.hasFailures());
135 for (BulkItemResponse response : responses) {
136 System.out.println(response.getResponse().getId() + "," + response.getResponse().getIndex() + "," + response.getResponse().getResult());
137 }
138
139 }
140
141 public void bulkProcesstor(String index,String type,String... ids) throws Exception {
142 try {
143
144 //IndexResponse indexResponse = client.getConnection().prepareIndex(index, type).setSource(getMapping()).get();
145 IndexResponse indexResponse = client.getConnection().prepareIndex(index, type).setSource().get();
146
147 PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(getMapping());
148 PutMappingResponse putMappingResponse = client.getConnection().admin().indices().putMapping(mappingRequest).actionGet();
149 //client.getConnection().prepareIndex("temp1","test").
150 BulkProcessor bulkProcessor = BulkProcessor.builder(client.getConnection(), new BulkProcessor.Listener() {
151 public void beforeBulk(long executionId, BulkRequest bulkRequest) {
152 System.out.println("beforeBulk:" + executionId + "," + bulkRequest.getDescription() + "," + bulkRequest.numberOfActions());
153 }
154
155 public void afterBulk(long executionId, BulkRequest bulkRequest, BulkResponse bulkResponse) {
156 System.out.println("afterBulk:" + executionId + "," + bulkRequest.getDescription() + "," + bulkRequest.numberOfActions());
157 System.out.println("afterBulk:" + executionId + "," + bulkResponse.getItems() + "," + bulkResponse.getTook());
158 }
159
160 public void afterBulk(long executionId, BulkRequest bulkRequest, Throwable throwable) {
161 System.out.println("afterBulk:" + executionId + "," + bulkRequest.getParentTask() + "," + bulkRequest.getDescription() + "," + throwable);
162 }
163 })
164 .setBulkActions(10)
165 .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
166 .setConcurrentRequests(1)
167 .setFlushInterval(TimeValue.timeValueMillis(1))
168 .setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3)).build();
169
170 for (String id : ids) {
171 Map<String, Object> jsonMap = new HashMap<String, Object>();
172 jsonMap.put("name","*"+id);
173 jsonMap.put("age",30+Integer.parseInt(id));
174 jsonMap.put("date",new Date());
175 jsonMap.put("message","程序设计"+id);
176 jsonMap.put("tel","18612855433");
177 jsonMap.put("attr_name",new String[]{"品牌_sku_attr"+id,"商品类别_sku_attr"+id,"面料_sku_attr"+id});
178 jsonMap.put("create_date",new Date());
179 bulkProcessor.add(new IndexRequest(index,type,id).source(jsonMap));
180
181 }
182 bulkProcessor.close();
183 } catch (Exception e) {
184 e.printStackTrace();
185 System.out.println(e);
186 }finally {
187
188 }
189
190 }
191
192 private XContentBuilder getMapping() throws Exception{
193 XContentBuilder mapping = null;
194 try {
195
196 mapping = jsonBuilder().startObject()//.startObject("_ttl").field("enabled",false).endObject()
197 .startObject("properties").startObject("name").field("type","text").field("analyzer","ik_max_word")
198 .field("search_analyzer","ik_max_word").endObject()
199 .startObject("age").field("type","long").endObject()
200 .startObject("date").field("type","date").endObject()
201 .startObject("message").field("type","keyword").field("index","true").endObject()
202 .startObject("tel").field("type","keyword").endObject()
203 .startObject("attr_name").field("type","keyword").field("index","true").endObject()
204 .endObject()
205 .endObject();
206 } catch (IOException e) {
207 e.printStackTrace();
208 }
209
210 return mapping;
211 }
212 }
SearchServiceImpl
package com.elastic.service.impl; import com.elastic.common.conn.EsClient;
import com.elastic.service.inter.SearchService;
import org.apache.lucene.index.Term;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.functionscore.*;
import org.elasticsearch.join.aggregations.Children;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.*;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.min.Min;
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.rescore.RescoreBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery; /**
* Created by xiaotian on 2017/12/2.
*/
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private EsClient client; private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SearchServiceImpl.class); public void search() {
SearchResponse searchResponse = client.getConnection().prepareSearch("twitter")
.setTypes("tweet")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.matchQuery("name","三胖子"))
.setPostFilter(QueryBuilders.rangeQuery("age").from(19).to(400))
.setFrom(0).setSize(20).setExplain(true)
//.addAggregation(AggregationBuilder.CommonFields.FIELD.match(""))
.get(); SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsMap());
}
} public void searchByCondition() throws Exception{ SearchRequestBuilder searchRequestBuilder = client.getConnection().prepareSearch("red89")
.setTypes("test"); Map<String, Object> params = new HashMap<>();
params.put("num1", 1);
params.put("num2", 2); String inlineScript = "long age;if (doc['age'].value < 45) age = doc['age'].value + 50; return age * params.num1;";
// + "return (diff +num1+num2)";
Script script = new Script(ScriptType.INLINE,"painless",inlineScript , params);
ScriptScoreFunctionBuilder scriptScoreFunctionBuilder = ScoreFunctionBuilders.scriptFunction(script); //MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "中华"); searchRequestBuilder.setQuery(functionScoreQuery(QueryBuilders.matchQuery("name","中华").operator(Operator.AND),scriptScoreFunctionBuilder));
// searchRequestBuilder.setQuery(QueryBuilders.matchQuery("name","中华").operator(Operator.AND));
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
getFilterQuery("tel",new String[]{"18612855433"},"AND",queryBuilder);
// getFilterQuery("name",new String[]{"*"},"AND",queryBuilder); //getFilterQuery("age",new String[]{"40"},"AND",queryBuilder);
//getFilterQuery("message",new String[]{"程序设计19"},"AND",queryBuilder);
boolQueryBuilder.must(queryBuilder);
//boolQueryBuilder.must();
// boolQueryBuilder.must(getRangeFilterQuery("age",new Integer[]{null,45}));
//new_score = old_score * log(1 + factor * number_of_votes) FieldValueFactorFunctionBuilder age = ScoreFunctionBuilders.fieldValueFactorFunction("age").modifier(FieldValueFactorFunction.Modifier.LN1P).factor(10);
FieldValueFactorFunctionBuilder age1 = ScoreFunctionBuilders.fieldValueFactorFunction("age").modifier(FieldValueFactorFunction.Modifier.LN1P).factor(4);
FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders =
new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{new FunctionScoreQueryBuilder.FilterFunctionBuilder(age),new FunctionScoreQueryBuilder.FilterFunctionBuilder(age1)};
//FunctionScoreQueryBuilder scoreQueryBuilder = functionScoreQuery(boolQueryBuilder, filterFunctionBuilders).boostMode(CombineFunction.SUM); // FunctionScoreQueryBuilder functionScoreQueryBuilder = functionScoreQuery(boolQueryBuilder, scriptScoreFunctionBuilder);
searchRequestBuilder.setPostFilter(boolQueryBuilder);
searchRequestBuilder.setFrom(0);
searchRequestBuilder.setSize(20);
searchRequestBuilder.setExplain(true); TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("agg").field("attr_name");//.subAggregation(AggregationBuilders.topHits("top").from(0).size(10)).size(100);
RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("range").field("age").addRange(0, 30).addRange(30,50).addRange(50,100); aggregationBuilder.size(100); AggregationBuilder filter = AggregationBuilders.filter("agg", boolQueryBuilder)
.subAggregation(aggregationBuilder); searchRequestBuilder.addAggregation(filter);
searchRequestBuilder.addAggregation(rangeAggregationBuilder);
// searchRequestBuilder.addAggregation(filter);
//searchRequestBuilder.addSort("age",SortOrder.DESC); //打分
//searchRequestBuilder.addRescorer(RescoreBuilder.queryRescorer(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.fieldValueFactorFunction("age")))); SearchResponse searchResponse1 = searchRequestBuilder.get(); System.out.println("param:"+searchRequestBuilder.toString()); System.out.println("aaaa"+searchResponse1.getAggregations().getAsMap()); InternalFilter aggFilter = searchResponse1.getAggregations().get("agg");
System.out.println(aggFilter.toString());
Terms agg = aggFilter.getAggregations().get("agg"); agg.getBuckets().forEach(bucket ->{
System.out.println(bucket.getKey()+":"+bucket.getDocCount());
} );
System.out.println("--------------------------------");
Range range = searchResponse1.getAggregations().get("range"); range.getBuckets().forEach(bucket ->{
//System.out.println(bucket.getKey()+":"+bucket.getDocCount());
//logger.info("key [{}], from [{}], to [{}], doc_count [{}]", bucket.getKey(), bucket.getFrom(),bucket.getTo(),bucket.getDocCount());
System.out.println(String.format("key [%s], from [%s], to [%s], doc_count [%d]", bucket.getKey(), bucket.getFrom(),bucket.getTo(),bucket.getDocCount())); }); SearchHits hits = searchResponse1.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsMap());
} System.out.println("---------------------"); //
// SearchRequestBuilder searchRequestBuilder1 = client.getConnection().prepareSearch("red88")
// .setTypes("test");
//// QueryBuilder tel = QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("tel","18612855433")).should(QueryBuilders.matchQuery("message","程序设计"));
//// BoolQueryBuilder filter1 = QueryBuilders.boolQuery()//.must(QueryBuilders.matchQuery("age", "40").operator(Operator.AND))
//// .must(QueryBuilders.rangeQuery("age").from(0).to(40))
//// .must(tel);
// searchRequestBuilder.setQuery(QueryBuilders.matchQuery("name","三胖子").operator(Operator.AND));
// BoolQueryBuilder boolQueryBuilder1 = QueryBuilders.boolQuery();
// BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery();
// getFilterQuery("tel",new String[]{"18612855433"},"OR",queryBuilder1);
// getFilterQuery("message",new String[]{"程序设计"},"OR",queryBuilder1);
// boolQueryBuilder.must(queryBuilder);
// //boolQueryBuilder.must();
// boolQueryBuilder.must(getRangeFilterQuery("age",new Integer[]{null,40}));
// searchRequestBuilder.setPostFilter(boolQueryBuilder1);
// searchRequestBuilder.setFrom(0);
// searchRequestBuilder.setSize(20);
// searchRequestBuilder.setExplain(true);
// TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("agg").field("attr_name");
//
// aggregationBuilder.size(100);
// searchRequestBuilder.addAggregation(aggregationBuilder);
// searchRequestBuilder.addSort("age",SortOrder.DESC);
// } public FunctionScoreQueryBuilder sortByFucntion(QueryBuilder queryBuilder) {
FunctionScoreQueryBuilder query = functionScoreQuery(queryBuilder,
ScoreFunctionBuilders.fieldValueFactorFunction("age").modifier(FieldValueFactorFunction.Modifier.LN1P).factor(1f)).boostMode(CombineFunction.SUM);
//.add(ScoreFunctionBuilders.fieldValueFactorFunction(查询字段).modifier(Modifier.RECIPROCAL).factor(1)).boostMode(“sum”);
return query;
}
private QueryBuilder getFilterQuery(String fieldName, Object[] fieldValues,String andor,BoolQueryBuilder queryBuilder) {
//BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); for (int i = 0; i < fieldValues.length; i++) {
if ("OR".equals(andor)){
queryBuilder.should(QueryBuilders.matchQuery(fieldName,fieldValues[i]).operator(Operator.OR));
}else if ("AND".equals(andor)){
queryBuilder.must(QueryBuilders.matchQuery(fieldName,fieldValues[i]).operator(Operator.AND));
}
} return queryBuilder; }
private QueryBuilder getFilterQuery(String fieldName, Object[] fieldValues,String andor) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); for (int i = 0; i < fieldValues.length; i++) {
if ("OR".equals(andor)){
boolQueryBuilder.should(QueryBuilders.matchQuery(fieldName,fieldValues[i]));
}else if ("AND".equals(andor)){
boolQueryBuilder.must(QueryBuilders.matchQuery(fieldName,fieldValues[i]));
}
} return boolQueryBuilder; } private RangeQueryBuilder getRangeFilterQuery(String fieldName, Integer[] values) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldName);
rangeQueryBuilder.from(values[0]==null?0:values[0]);
rangeQueryBuilder.to((values.length<2||values[1]==null)?99999999:values[1]);
return rangeQueryBuilder;
} public void multiSearch() { SearchRequestBuilder srb1 = client.getConnection().prepareSearch().setQuery(QueryBuilders.matchQuery("name", "tom")).setSize(1);
SearchRequestBuilder srb2 = client.getConnection().prepareSearch().setQuery(QueryBuilders.matchQuery("name", "三胖子")).setSize(1);
MultiSearchResponse multiSearchResponse = client.getConnection().prepareMultiSearch()
.add(srb1)
.add(srb2)
.get(); for (MultiSearchResponse.Item item : multiSearchResponse.getResponses()) {
for (SearchHit hit : item.getResponse().getHits().getHits()) {
System.out.println(hit.getSourceAsMap()); }
} } public void aggsearch() {
SearchRequestBuilder requestBuilder = client.getConnection().prepareSearch("twitter").setTypes("tweet")
.setFrom(0).setSize(100);
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("agg").field("name").subAggregation(AggregationBuilders.terms("add").field("age"));
SearchResponse response = requestBuilder.setQuery(QueryBuilders.matchQuery("name", "三胖"))
.addAggregation(aggregationBuilder)
.addSort("age", SortOrder.DESC)
.setExplain(true).execute().actionGet();
SearchHits searchHits = response.getHits(); Terms agg = response.getAggregations().get("agg");
Children children = response.getAggregations().get("agg");
System.out.println(agg.getBuckets());
for (Terms.Bucket bucket : agg.getBuckets()) {
System.out.println(bucket.getKey() + ":" + bucket.getDocCount());
}
System.out.println(children.getAggregations().getAsMap()); } public void metricsAgg() { SearchRequestBuilder searchRequestBuilder = client.getConnection().prepareSearch("hello").setTypes("test").setFrom(0).setSize(100);
MinAggregationBuilder minAggregationBuilder = AggregationBuilders.min("agg").field("age");
SearchResponse response = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(minAggregationBuilder).setExplain(true).execute().actionGet();
Min min = response.getAggregations().get("agg");
System.out.println("min:"+min.getValue());
MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("max_age").field("age");
SearchResponse response1 = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(maxAggregationBuilder).setExplain(true).execute().actionGet();
Max max_age = response1.getAggregations().get("max_age");
System.out.println("max:"+max_age.getValue()); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_age").field("age");
SearchResponse response2 = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(avgAggregationBuilder).setExplain(true).execute().actionGet();
Avg avg_age = response2.getAggregations().get("avg_age");
System.out.println("avg_age:"+avg_age.getValue()); SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("sum_age").field("age");
SearchResponse searchResponse = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()).addAggregation(sumAggregationBuilder)
.setExplain(true).execute().actionGet();
Sum sum_age = searchResponse.getAggregations().get("sum_age");
System.out.println("sum_age:"+sum_age.getValue()); System.out.println("---------------------------"); StatsAggregationBuilder statsAggregationBuilder = AggregationBuilders.stats("agg_status").field("age");
SearchResponse searchResponse1 = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()).addAggregation(statsAggregationBuilder)
.setExplain(true).execute().actionGet(); Stats agg_status = searchResponse1.getAggregations().get("agg_status");
double min1 = agg_status.getMin();
double max = agg_status.getMax();
long count = agg_status.getCount();
double sum = agg_status.getSum();
double avg = agg_status.getAvg();
System.out.println("---------------------------------");
System.out.println("min1="+min1);
System.out.println("max="+max);
System.out.println("count="+count);
System.out.println("sum="+sum);
System.out.println("avg="+avg); ExtendedStatsAggregationBuilder extendedStatsAggregationBuilder = AggregationBuilders.extendedStats("extend_status").field("age"); SearchResponse searchResponse2 = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()).addAggregation(extendedStatsAggregationBuilder).setExplain(true).execute().actionGet();
ExtendedStats extend_status = searchResponse2.getAggregations().get("extend_status");
double extend_min = extend_status.getMin();
double extend_max = extend_status.getMax();
long extend_count = extend_status.getCount();
double extend_sum = extend_status.getSum();
double extend_avg = extend_status.getAvg();
System.out.println("---------------------------------");
System.out.println("extend_min="+extend_min);
System.out.println("extend_max="+extend_max);
System.out.println("extend_count="+extend_count);
System.out.println("extend_sum="+extend_sum);
System.out.println("extend_avg="+extend_avg); }
}
3.1 controller层实现
IndexController
1 package com.elastic.controller;
2
3 import com.elastic.service.inter.IndexService;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.RequestMapping;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import java.io.IOException;
11
12 /**
13 * Created by xiaotian on 2017/12/1.
14 */
15 @Controller
16 public class IndexController {
17
18 @Autowired
19 private IndexService indexService;
20
21 @RequestMapping("/index")
22 public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
23
24 indexService.index(request.getParameter("id"));
25 }
26 @RequestMapping("/get")
27 public void get(){
28
29 indexService.get();
30 }
31
32 @RequestMapping("/del")
33 public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
34
35 indexService.del(request.getParameter("id"));
36 }
37 @RequestMapping("/update")
38 public void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
39
40 indexService.update(request.getParameter("id"));
41 }
42 @RequestMapping("/multiGet")
43 public void multiGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
44
45 indexService.multiGet(request.getParameter("id").split(","));
46 }
47 @RequestMapping("/bulk")
48 public void bulk(HttpServletRequest request, HttpServletResponse response) throws Exception {
49
50 indexService.bulk(request.getParameter("id").split(","));
51 }
52 @RequestMapping("/bulkP")
53 public void bulkProcesstor(HttpServletRequest request, HttpServletResponse response) throws Exception {
54 String index = request.getParameter("index");
55 String type = request.getParameter("type");
56 String[] ids = request.getParameter("id").split(",");
57 indexService.bulkProcesstor(index,type,ids);
58 }
59 }
SearchController
1 package com.elastic.controller;
2
3 import com.elastic.service.inter.SearchService;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.RequestMapping;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import java.io.IOException;
11
12 /**
13 * Created by xiaotian on 2017/12/2.
14 */
15 @Controller
16 public class SearchController {
17
18 @Autowired
19 private SearchService searchService;
20 @RequestMapping("/search")
21 public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
22
23 searchService.search();
24 }
25 @RequestMapping("/multiSearch")
26 public void multiSearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
27
28 searchService.multiSearch();
29 }
30 @RequestMapping("/aggsearch")
31 public void aggsearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
32
33 searchService.aggsearch();
34 }
35
36 @RequestMapping("/metricsAgg")
37 public void metricsAgg(HttpServletRequest request, HttpServletResponse response) throws IOException {
38
39 searchService.metricsAgg();
40 }
41 @RequestMapping("/searchFilter")
42 public void searchByCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
43
44 searchService.searchByCondition();
45 }
46 }
4. 通过kibana进行索引建立和查询等
1 GET red89/test/_search
2
3 {
4 "from" : 0,
5 "size" : 20,
6 "query" : {
7 "match" : {
8 "name" : {
9 "query" : "中华",
10 "operator" : "AND",
11 "prefix_length" : 0,
12 "max_expansions" : 50,
13 "fuzzy_transpositions" : true,
14 "lenient" : false,
15 "zero_terms_query" : "NONE",
16 "boost" : 1.0
17 }
18 }
19 },
20 "post_filter" : {
21 "bool" : {
22 "must" : [
23 {
24 "bool" : {
25 "should" : [
26 {
27 "match" : {
28 "tel" : {
29 "query" : "18612855433",
30 "operator" : "OR",
31 "prefix_length" : 0,
32 "max_expansions" : 50,
33 "fuzzy_transpositions" : true,
34 "lenient" : false,
35 "zero_terms_query" : "NONE",
36 "boost" : 1.0
37 }
38 }
39 }
40 ],
41 "adjust_pure_negative" : true,
42 "boost" : 1.0
43 }
44 },
45 {
46 "range" : {
47 "age" : {
48 "from" : 0,
49 "to" : 45,
50 "include_lower" : true,
51 "include_upper" : true,
52 "boost" : 1.0
53 }
54 }
55 }
56 ],
57 "adjust_pure_negative" : true,
58 "boost" : 1.0
59 }
60 },
61 "explain" : true,
62 "aggregations" : {
63 "agg" : {
64 "filter" : {
65 "bool" : {
66 "must" : [
67 {
68 "bool" : {
69 "should" : [
70 {
71 "match" : {
72 "tel" : {
73 "query" : "18612855433",
74 "operator" : "OR",
75 "prefix_length" : 0,
76 "max_expansions" : 50,
77 "fuzzy_transpositions" : true,
78 "lenient" : false,
79 "zero_terms_query" : "NONE",
80 "boost" : 1.0
81 }
82 }
83 }
84 ],
85 "adjust_pure_negative" : true,
86 "boost" : 1.0
87 }
88 },
89 {
90 "range" : {
91 "age" : {
92 "from" : 0,
93 "to" : 45,
94 "include_lower" : true,
95 "include_upper" : true,
96 "boost" : 1.0
97 }
98 }
99 }
100 ],
101 "adjust_pure_negative" : true,
102 "boost" : 1.0
103 }
104 },
105 "aggregations" : {
106 "agg" : {
107 "terms" : {
108 "field" : "attr_name",
109 "size" : 100,
110 "min_doc_count" : 1,
111 "shard_min_doc_count" : 0,
112 "show_term_doc_count_error" : false,
113 "order" : [
114 {
115 "_count" : "desc"
116 },
117 {
118 "_key" : "asc"
119 }
120 ]
121 }
122 }
123 }
124 },
125 "range" : {
126 "range" : {
127 "field" : "age",
128 "ranges" : [
129 {
130 "from" : 0.0,
131 "to" : 30.0
132 },
133 {
134 "from" : 30.0,
135 "to" : 50.0
136 },
137 {
138 "from" : 50.0,
139 "to" : 100.0
140 }
141 ],
142 "keyed" : false
143 }
144 }
145 }
146 }
查询结果:
1 {
2 "took": 1,
3 "timed_out": false,
4 "_shards": {
5 "total": 5,
6 "successful": 5,
7 "skipped": 0,
8 "failed": 0
9 },
10 "hits": {
11 "total": 21,
12 "max_score": 1,
13 "hits": [
14 {
15 "_index": "red89",
16 "_type": "test",
17 "_id": "14",
18 "_score": 1,
19 "_source": {
20 "date": "2017-12-11T09:55:42.261Z",
21 "attr_name": [
22 "品牌_sku_attr14",
23 "商品类别_sku_attr14",
24 "面料_sku_attr14"
25 ],
26 "name": "*14",
27 "tel": "18612855433",
28 "message": "程序设计14",
29 "create_date": "2017-12-11T09:55:42.261Z",
30 "age": 44
31 }
32 },
33 {
34 "_index": "red89",
35 "_type": "test",
36 "_id": "19",
37 "_score": 1,
38 "_source": {
39 "date": "2017-12-11T09:55:42.261Z",
40 "attr_name": [
41 "品牌_sku_attr19",
42 "商品类别_sku_attr19",
43 "面料_sku_attr19"
44 ],
45 "name": "*19",
46 "tel": "18612855433",
47 "message": "程序设计19",
48 "create_date": "2017-12-11T09:55:42.261Z",
49 "age": 49
50 }
51 },
52 {
53 "_index": "red89",
54 "_type": "test",
55 "_id": "5",
56 "_score": 1,
57 "_source": {
58 "date": "2017-12-11T09:55:42.257Z",
59 "attr_name": [
60 "品牌_sku_attr5",
61 "商品类别_sku_attr5",
62 "面料_sku_attr5"
63 ],
64 "name": "*5",
65 "tel": "18612855433",
66 "message": "程序设计5",
67 "create_date": "2017-12-11T09:55:42.257Z",
68 "age": 35
69 }
70 },
71 {
72 "_index": "red89",
73 "_type": "test",
74 "_id": "8",
75 "_score": 1,
76 "_source": {
77 "date": "2017-12-11T09:55:42.257Z",
78 "attr_name": [
79 "品牌_sku_attr8",
80 "商品类别_sku_attr8",
81 "面料_sku_attr8"
82 ],
83 "name": "*8",
84 "tel": "18612855433",
85 "message": "程序设计8",
86 "create_date": "2017-12-11T09:55:42.257Z",
87 "age": 38
88 }
89 },
90 {
91 "_index": "red89",
92 "_type": "test",
93 "_id": "9",
94 "_score": 1,
95 "_source": {
96 "date": "2017-12-11T09:55:42.257Z",
97 "attr_name": [
98 "品牌_sku_attr9",
99 "商品类别_sku_attr9",
100 "面料_sku_attr9"
101 ],
102 "name": "*9",
103 "tel": "18612855433",
104 "message": "程序设计9",
105 "create_date": "2017-12-11T09:55:42.257Z",
106 "age": 39
107 }
108 },
109 {
110 "_index": "red89",
111 "_type": "test",
112 "_id": "10",
113 "_score": 1,
114 "_source": {
115 "date": "2017-12-11T09:55:42.257Z",
116 "attr_name": [
117 "品牌_sku_attr10",
118 "商品类别_sku_attr10",
119 "面料_sku_attr10"
120 ],
121 "name": "*10",
122 "tel": "18612855433",
123 "message": "程序设计10",
124 "create_date": "2017-12-11T09:55:42.257Z",
125 "age": 40
126 }
127 },
128 {
129 "_index": "red89",
130 "_type": "test",
131 "_id": "12",
132 "_score": 1,
133 "_source": {
134 "date": "2017-12-11T09:55:42.261Z",
135 "attr_name": [
136 "品牌_sku_attr12",
137 "商品类别_sku_attr12",
138 "面料_sku_attr12"
139 ],
140 "name": "*12",
141 "tel": "18612855433",
142 "message": "程序设计12",
143 "create_date": "2017-12-11T09:55:42.261Z",
144 "age": 42
145 }
146 },
147 {
148 "_index": "red89",
149 "_type": "test",
150 "_id": "ZraQK2ABrThwqnonhOO3",
151 "_score": 1,
152 "_source": {}
153 },
154 {
155 "_index": "red89",
156 "_type": "test",
157 "_id": "2",
158 "_score": 1,
159 "_source": {
160 "date": "2017-12-11T09:55:42.256Z",
161 "attr_name": [
162 "品牌_sku_attr2",
163 "商品类别_sku_attr2",
164 "面料_sku_attr2"
165 ],
166 "name": "*2",
167 "tel": "18612855433",
168 "message": "程序设计2",
169 "create_date": "2017-12-11T09:55:42.256Z",
170 "age": 32
171 }
172 },
173 {
174 "_index": "red89",
175 "_type": "test",
176 "_id": "4",
177 "_score": 1,
178 "_source": {
179 "date": "2017-12-11T09:55:42.257Z",
180 "attr_name": [
181 "品牌_sku_attr4",
182 "商品类别_sku_attr4",
183 "面料_sku_attr4"
184 ],
185 "name": "*4",
186 "tel": "18612855433",
187 "message": "程序设计4",
188 "create_date": "2017-12-11T09:55:42.257Z",
189 "age": 34
190 }
191 }
192 ]
193 }
194 }