Authored by unknown

fix编译bug

... ... @@ -17,6 +17,11 @@
<artifactId>yoho-core-dal</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-personalized</artifactId>
</dependency>
... ...
... ... @@ -14,10 +14,6 @@
<dependencies>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-consumer-service</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
... ... @@ -26,17 +22,16 @@
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-dal</artifactId>
<groupId>com.yoho.search</groupId>
<artifactId>search-es</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-consumer-dal</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.4.3</version>
<groupId>com.yoho.search</groupId>
<artifactId>search-consumer-service</artifactId>
</dependency>
</dependencies>
</project>
... ...
package com.yoho.search.consumer.index.common;
import com.yoho.search.core.es.model.ESBluk;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import java.util.List;
import java.util.Set;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import java.util.List;
import java.util.Set;
import com.yoho.search.core.es.model.ESBluk;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
/**
* 索引管理接口: 管理所有的索引和索引客户端,以及对外提供索引操作接口
... ...
... ... @@ -16,7 +16,6 @@ import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeTok
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -43,6 +42,7 @@ import com.yoho.search.core.es.impl.YohoIndexHelper;
import com.yoho.search.core.es.model.ESBluk;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
@Component
public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEventPublisherAware {
... ...
... ... @@ -34,6 +34,11 @@
<version>${search.core.version}</version>
</dependency>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-es</artifactId>
<version>${search.core.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
... ...
... ... @@ -19,6 +19,10 @@
</dependency>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-es</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.search</groupId>
<artifactId>search-consumer-dal</artifactId>
</dependency>
</dependencies>
... ...
... ... @@ -43,11 +43,6 @@
<artifactId>search-consumer-spider</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
... ...
package com.yoho.core.index.service.impl;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateAction;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;
/**
* Created by apple on 16/8/6.
*/
public class EsTest {
private TransportClient client;
@Before
public void createEsClient() throws UnknownHostException {
Settings settings = Settings.settingsBuilder()
// .put("cluster.name", "yohosearch111")
.put("cluster.name", "yohosearch_zhurui").put("client.transport.sniff", true).build();
client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}
@Test
public void deleteMyStroe() {
DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete("yoho_productindex_1470727824964_20160809153024964").get();
System.out.println(deleteIndexResponse.getHeaders());
}
@Test
public void queryAllIndex() {
ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
String[] indexs = response.getState().getMetaData().getConcreteAllIndices();
for (String index : indexs) {
System.out.println(index);//
}
}
@Test
public void createMappingTest() {
CreateIndexResponse rssp = client
.admin()
.indices()
.prepareCreate("my_store")
.addMapping(
"products",
"{\n" + " \"products\": {\n" + " \"properties\": {\n" + " \"productID\": {\n" + " \"type\": \"string\",\n"
+ " \"index\": \"not_analyzed\"\n" + " }\n" + " }\n" + " }\n" + " }").get();
System.out.println(rssp.getHeaders());
}
@Test
public void bulktest() throws IOException {
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("my_store1", "products", "1").setSource(
jsonBuilder().startObject().field("price", 10).field("productID", "XHDK-A-1293-#fJ3").endObject()));
bulkRequest.add(client.prepareIndex("my_store2", "products", "2").setSource(
jsonBuilder().startObject().field("price", 20).field("productID", "KDKE-B-9947-#kL5").endObject()));
bulkRequest.add(client.prepareIndex("my_store3", "products1", "3").setSource(
jsonBuilder().startObject().field("price", 30).field("productID", "JODL-X-1937-#pV7").endObject()));
bulkRequest.add(client.prepareIndex("my_store", "products", "4").setSource(
jsonBuilder().startObject().field("price", 40).field("productID", "QQPX-R-3956-#aD8").endObject()));
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("failure");
}
}
@Test
public void queryFilterTerm() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
System.out.println(s.toString());
}
@Test
public void basequerytest() {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
System.out.println(s.toString());
}
@Test
public void basequeryAndFiltertest() {
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(filterBuilder).get();
System.out.println(s.toString());
}
@Test
public void filterQueryTest() {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
// searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(query).get();
System.out.println(s.toString());
}
@Test
public void SearchSourceBuilderTEst() throws ExecutionException, InterruptedException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
searchSourceBuilder.query(query);
// searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
SearchRequest request = Requests.searchRequest("my_store").types("products");
request.source(searchSourceBuilder.toString());
System.out.println(searchSourceBuilder.toString());
// System.out.println(client.search(request).get().toString());
}
@Test
public void boolQueryTest() throws ExecutionException, InterruptedException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder t1 = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
QueryBuilder t2 = QueryBuilders.termQuery("price", 10);
QueryBuilder t4 = QueryBuilders.termQuery("price", 30);
searchSourceBuilder.query(QueryBuilders.boolQuery().should(t1).should(t2).mustNot(t4));
System.out.println(searchSourceBuilder);
SearchRequest request = Requests.searchRequest("my_store").types("products");
request.source(searchSourceBuilder.toString());
System.out.println(client.search(request).get().toString());
}
@Test
public void updateDocTest() throws IOException, ExecutionException, InterruptedException {
GetResponse rs = client.prepareGet("my_store", "products", "1").get();
System.out.println("before:" + rs.getSourceAsString());
UpdateRequest updateRequest = new UpdateRequest("my_store", "products", "1").doc(jsonBuilder().startObject().field("price", 12).endObject());
client.update(updateRequest).get();
System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
}
@Test
public void UpdateRequestBuilderTest() throws IOException {
GetResponse rs = client.prepareGet("my_store", "products", "1").get();
System.out.println("before:" + rs.getSourceAsString());
UpdateRequestBuilder request = new UpdateRequestBuilder(client, UpdateAction.INSTANCE, "my_store", "products", "1");
UpdateResponse re = request.setDoc(jsonBuilder().startObject().field("price", 12).endObject()).get();
System.out.println(re.toString());
System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
}
@Test
public void bulkUpdateTest() throws ExecutionException, InterruptedException {
final BulkRequest request = Requests.bulkRequest();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", i);
map.put("productId", i);
request.add(Requests.indexRequest("productindex").type("productindex").id(i + "").source(map));
if (i % 2 == 0) {
request.add(Requests.deleteRequest("productindex").type("productindex").id(i + ""));
}
}
client.bulk(request).get();
}
@Test
public void bulkdeleteTest() throws ExecutionException, InterruptedException {
final BulkRequest request = Requests.bulkRequest();
for (int i = 0; i < 10; i++) {
request.add(Requests.deleteRequest("my_index").type("zhurui").id(i + ""));
}
client.bulk(request).get();
}
@Test
public void bulkaddTest() throws ExecutionException, InterruptedException {
final BulkRequest request = Requests.bulkRequest();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", i);
map.put("productId", i);
map.put("productName", "zhurui");
map.put("nickname", "markeloff" + i);
request.add(Requests.indexRequest("my_index").type("test").id(i + "").source(map));
}
Iterator<BulkItemResponse> iterator = client.bulk(request).get().iterator();
while (iterator.hasNext()) {
BulkItemResponse bulkItemResponse = iterator.next();
System.out.println(bulkItemResponse.getFailureMessage());
}
}
}