|
@@ -11,6 +11,7 @@ import org.elasticsearch.action.get.GetResponse;
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
|
import org.elasticsearch.action.index.IndexResponse;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
+import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
@@ -19,6 +20,7 @@ import org.elasticsearch.common.document.DocumentField;
|
|
|
import org.elasticsearch.common.text.Text;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.core.TimeValue;
|
|
|
+import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
@@ -26,6 +28,8 @@ import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
|
|
+import org.elasticsearch.search.sort.SortBuilder;
|
|
|
+import org.elasticsearch.search.sort.SortBuilders;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
@@ -60,7 +64,7 @@ public class EsDocTests {
|
|
|
@Test
|
|
|
public void putTest() throws IOException {
|
|
|
//创建数据对象
|
|
|
- EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","99.8");
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","99.8",100);
|
|
|
IndexRequest indexRequest = new IndexRequest("yg");
|
|
|
//设置请求规则,设置请求id
|
|
|
indexRequest.id("1");
|
|
@@ -81,7 +85,7 @@ public class EsDocTests {
|
|
|
*/
|
|
|
public void putbatchTest() throws IOException {
|
|
|
//创建数据对象
|
|
|
- List<EsProduct> list = ParseHtml.parse("c语言");
|
|
|
+ List<EsProduct> list = ParseHtml.parse("大数据");
|
|
|
BulkRequest bulkRequest = new BulkRequest();
|
|
|
bulkRequest.timeout("2m");
|
|
|
for(EsProduct esProduct :list){
|
|
@@ -108,7 +112,7 @@ public class EsDocTests {
|
|
|
*/
|
|
|
@Test
|
|
|
public void updateTest() throws IOException {
|
|
|
- EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8");
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8",100);
|
|
|
UpdateRequest updateRequest = new UpdateRequest("yg","1");
|
|
|
updateRequest.timeout("2s");
|
|
|
updateRequest.doc(JSON.toJSONString(esProduct),XContentType.JSON);
|
|
@@ -120,7 +124,7 @@ public class EsDocTests {
|
|
|
*/
|
|
|
@Test
|
|
|
public void delTest() throws IOException {
|
|
|
- EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8");
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8",200);
|
|
|
DeleteRequest deleteRequest = new DeleteRequest("yg","1");
|
|
|
deleteRequest.timeout("2s");
|
|
|
client.delete(deleteRequest,RequestOptions.DEFAULT);
|
|
@@ -150,9 +154,13 @@ public class EsDocTests {
|
|
|
public void searchTest2() throws IOException {
|
|
|
SearchRequest searchRequest = new SearchRequest("jd_list");
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
-
|
|
|
-// searchSourceBuilder.fetchSource("name", null);//过滤为null数据
|
|
|
-// searchSourceBuilder.sort("price", SortOrder.ASC);
|
|
|
+ String[] includes =new String[4];
|
|
|
+ includes[0]="name";
|
|
|
+ includes[1]="price";
|
|
|
+ includes[2]="url";
|
|
|
+ includes[3]="num";
|
|
|
+ searchSourceBuilder.fetchSource(includes, null);//过滤为null数据
|
|
|
+ searchSourceBuilder.sort("num",SortOrder.DESC);
|
|
|
//模糊查询
|
|
|
QueryBuilder queryBuilder = QueryBuilders.termQuery("name","java");
|
|
|
searchSourceBuilder.query(queryBuilder);
|
|
@@ -166,9 +174,9 @@ public class EsDocTests {
|
|
|
//设置超时
|
|
|
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
|
|
//设置起始位置
|
|
|
- searchSourceBuilder.from(20);
|
|
|
+ searchSourceBuilder.from(1);
|
|
|
//设置页大小
|
|
|
- searchSourceBuilder.size(15);
|
|
|
+ searchSourceBuilder.size(30);
|
|
|
//绑定build
|
|
|
searchRequest.source(searchSourceBuilder);
|
|
|
//执行搜索
|
|
@@ -194,5 +202,6 @@ public class EsDocTests {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|