Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解

Elasticsearch JAVA操作有三种客户端:

1、TransportClient

2、JestClient

3、RestClient

还有种是2.3中有的NodeClient,在5.5.1中好像没有了。还有种是spring-data-elasticsearch,这里先以TransportClient来讲解CRUD,所用环境为:

JDK1.8

ES5.5.1

TransportClient5.5.1

POM.XML文件如下:

  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.fendo</groupId>
  5. <artifactId>TransportClient</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>TransportClient Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 初始化框架的版本号 -->
  11. <properties>
  12. <spring.version>4.2.7.RELEASE</spring.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.12</version>
  19. </dependency>
  20. <!-- 加入ServletAPI -->
  21. <dependency>
  22. <groupId>javax.servlet</groupId>
  23. <artifactId>javax.servlet-api</artifactId>
  24. <version>3.1.0</version>
  25. <scope>provided</scope>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.hibernate</groupId>
  29. <artifactId>hibernate-validator</artifactId>
  30. <version>4.2.0.Final</version>
  31. </dependency>
  32. <!-- MySQL依赖 start -->
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. <version>5.1.38</version>
  37. </dependency>
  38. <!-- MySQL依赖 end -->
  39. <!-- 加入MyBatis 依赖 start -->
  40. <dependency>
  41. <groupId>org.mybatis</groupId>
  42. <artifactId>mybatis</artifactId>
  43. <version>3.2.5</version>
  44. </dependency>
  45. <!-- 加入MyBatis 依赖 end -->
  46. <!-- Log4j start -->
  47. <dependency>
  48. <groupId>log4j</groupId>
  49. <artifactId>log4j</artifactId>
  50. <version>1.2.17</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.apache.logging.log4j</groupId>
  54. <artifactId>log4j-1.2-api</artifactId>
  55. <version>2.7</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.apache.logging.log4j</groupId>
  59. <artifactId>log4j-slf4j-impl</artifactId>
  60. <version>2.7</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.apache.logging.log4j</groupId>
  64. <artifactId>log4j-core</artifactId>
  65. <version>2.7</version>
  66. </dependency>
  67. <!-- Log4j end -->
  68. <!-- 引用c3p0 依赖 start-->
  69. <dependency>
  70. <groupId>com.mchange</groupId>
  71. <artifactId>c3p0</artifactId>
  72. <version>0.9.2.1</version>
  73. </dependency>
  74. <!-- 引用c3p0 依赖 end-->
  75. <!-- 引用插件依赖:MyBatis整合Spring -->
  76. <dependency>
  77. <groupId>org.mybatis</groupId>
  78. <artifactId>mybatis-spring</artifactId>
  79. <version>1.3.0</version>
  80. </dependency>
  81. <!-- JSTL -->
  82. <dependency>
  83. <groupId>javax.servlet</groupId>
  84. <artifactId>jstl</artifactId>
  85. <version>1.2</version>
  86. </dependency>
  87. <!--  -->
  88. <dependency>
  89. <groupId>com.fasterxml.jackson.core</groupId>
  90. <artifactId>jackson-core</artifactId>
  91. <version>2.9.0</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>com.fasterxml.jackson.core</groupId>
  95. <artifactId>jackson-databind</artifactId>
  96. <version>2.9.0</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>com.fasterxml.jackson.core</groupId>
  100. <artifactId>jackson-annotations</artifactId>
  101. <version>2.9.0</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>com.alibaba</groupId>
  105. <artifactId>fastjson</artifactId>
  106. <version>1.2.12</version>
  107. </dependency>
  108. <!-- Elasticserach5 -->
  109. <dependency>
  110. <groupId>org.elasticsearch</groupId>
  111. <artifactId>elasticsearch</artifactId>
  112. <version>5.5.1</version>
  113. </dependency>
  114. <!--  <dependency>
  115. <groupId>org.elasticsearch.module</groupId>
  116. <artifactId>reindex</artifactId>
  117. <version>2.4.3</version>
  118. </dependency> -->
  119. <dependency>
  120. <groupId>com.google.collections</groupId>
  121. <artifactId>google-collections</artifactId>
  122. <version>1.0</version>
  123. </dependency>
  124. <!-- transport客户端 -->
  125. <dependency>
  126. <groupId>org.elasticsearch.client</groupId>
  127. <artifactId>transport</artifactId>
  128. <version>5.5.1</version>
  129. </dependency>
  130. <!-- IK分词器 -->
  131. <dependency>
  132. <groupId>org.elasticsearch</groupId>
  133. <artifactId>elasticsearch-analysis-ik</artifactId>
  134. <version>5.5.1</version>
  135. </dependency>
  136. </dependencies>
  137. <repositories>
  138. <repository>
  139. <id>spring-libs-snapshot</id>
  140. <name>Spring Snapshot Repository</name>
  141. <url>http://repo.spring.io/libs-snapshot</url>
  142. </repository>
  143. <repository>
  144. <id>elasticsearch-releases</id>
  145. <url>https://artifacts.elastic.co/maven</url>
  146. <releases>
  147. <enabled>true</enabled>
  148. </releases>
  149. <snapshots>
  150. <enabled>false</enabled>
  151. </snapshots>
  152. </repository>
  153. </repositories>
  154. <build>
  155. <finalName>TransportClient</finalName>
  156. <plugins>
  157. <plugin>
  158. <groupId>org.apache.maven.plugins</groupId>
  159. <artifactId>maven-compiler-plugin</artifactId>
  160. <configuration>
  161. <source>1.8</source>
  162. <target>1.8</target>
  163. </configuration>
  164. </plugin>
  165. </plugins>
  166. </build>
  167. </project>

详细代码如下:

  1. package com.fendo.temp;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.StringReader;
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Set;
  20. import java.util.concurrent.ExecutionException;
  21. import org.elasticsearch.action.get.GetResponse;
  22. import org.elasticsearch.action.get.MultiGetItemResponse;
  23. import org.elasticsearch.action.get.MultiGetResponse;
  24. import org.elasticsearch.index.query.MatchQueryBuilder;
  25. import org.elasticsearch.action.ActionFuture;
  26. import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
  27. import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
  28. import org.elasticsearch.script.Script;
  29. import org.elasticsearch.script.ScriptType;
  30. import org.elasticsearch.index.query.MultiMatchQueryBuilder;
  31. import org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder;
  32. import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
  33. import org.elasticsearch.action.search.MultiSearchResponse;
  34. import org.apache.lucene.analysis.TokenStream;
  35. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  36. import org.apache.lucene.search.join.ScoreMode;
  37. import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
  38. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  39. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  40. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
  41. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
  42. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  43. import org.elasticsearch.action.bulk.BulkResponse;
  44. import org.elasticsearch.action.delete.DeleteResponse;
  45. import org.elasticsearch.action.index.IndexRequest;
  46. import org.elasticsearch.action.index.IndexRequestBuilder;
  47. import org.elasticsearch.action.index.IndexResponse;
  48. import org.elasticsearch.action.search.SearchRequestBuilder;
  49. import org.elasticsearch.action.search.SearchResponse;
  50. import org.elasticsearch.action.search.SearchType;
  51. import org.elasticsearch.action.update.UpdateRequest;
  52. import org.elasticsearch.action.update.UpdateResponse;
  53. import org.elasticsearch.client.Response;
  54. import org.elasticsearch.client.transport.TransportClient;
  55. import org.elasticsearch.common.settings.Settings;
  56. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  57. import org.elasticsearch.common.unit.Fuzziness;
  58. import org.elasticsearch.common.unit.TimeValue;
  59. import org.elasticsearch.common.xcontent.XContentBuilder;
  60. import org.elasticsearch.common.xcontent.XContentFactory;
  61. import org.elasticsearch.index.query.BoolQueryBuilder;
  62. import org.elasticsearch.index.query.QueryBuilder;
  63. import org.elasticsearch.index.query.QueryBuilders;
  64. import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
  65. import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
  66. import org.elasticsearch.search.SearchHit;
  67. import org.elasticsearch.search.SearchHits;
  68. import org.elasticsearch.search.aggregations.AggregationBuilder;
  69. import org.elasticsearch.search.aggregations.AggregationBuilders;
  70. import org.elasticsearch.search.aggregations.Aggregations;
  71. import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
  72. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  73. import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
  74. import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
  75. import org.elasticsearch.search.aggregations.metrics.sum.Sum;
  76. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  77. import org.elasticsearch.search.sort.FieldSortBuilder;
  78. import org.elasticsearch.search.sort.SortOrder;
  79. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  80. import org.elasticsearch.action.bulk.BackoffPolicy;
  81. import org.elasticsearch.action.bulk.BulkItemResponse;
  82. import org.elasticsearch.action.bulk.BulkProcessor;
  83. import org.elasticsearch.action.bulk.BulkRequest;
  84. import org.elasticsearch.action.bulk.BulkResponse;
  85. import org.elasticsearch.common.unit.ByteSizeUnit;
  86. import org.elasticsearch.common.unit.ByteSizeValue;
  87. import org.elasticsearch.common.unit.TimeValue;
  88. import org.wltea.analyzer.lucene.IKAnalyzer;
  89. import org.joda.time.DateTime;
  90. import org.junit.Before;
  91. import org.junit.Test;
  92. import com.alibaba.fastjson.JSONObject;
  93. public class TransportClientTest {
  94. private TransportClient client;
  95. private final static String article="article";
  96. private final static String content="content";
  97. @Before
  98. public void getClient() throws Exception{
  99. //设置集群名称
  100. Settings settings = Settings.builder().put("cluster.name", "my-application").build();// 集群名
  101. //创建client
  102. client  = new PreBuiltTransportClient(settings)
  103. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
  104. }
  105. /**
  106. * -----------------------------------------增(创建索引,增加映射,新增文档)
  107. */
  108. /**
  109. * 创建索引的四种方法
  110. */
  111. @Test
  112. public void JSON(){
  113. String json = "{" +
  114. "\"id\":\"kimchy\"," +
  115. "\"postDate\":\"2013-01-30\"," +
  116. "\"message\":\"trying out Elasticsearch\"" +
  117. "}";
  118. }
  119. /**
  120. * 创建索引并添加映射
  121. * @throws IOException
  122. */
  123. @Test
  124. public void CreateIndexAndMapping() throws Exception{
  125. CreateIndexRequestBuilder  cib=client.admin().indices().prepareCreate(article);
  126. XContentBuilder mapping = XContentFactory.jsonBuilder()
  127. .startObject()
  128. .startObject("properties") //设置之定义字段
  129. .startObject("author")
  130. .field("type","string") //设置数据类型
  131. .endObject()
  132. .startObject("title")
  133. .field("type","string")
  134. .endObject()
  135. .startObject("content")
  136. .field("type","string")
  137. .endObject()
  138. .startObject("price")
  139. .field("type","string")
  140. .endObject()
  141. .startObject("view")
  142. .field("type","string")
  143. .endObject()
  144. .startObject("tag")
  145. .field("type","string")
  146. .endObject()
  147. .startObject("date")
  148. .field("type","date")  //设置Date类型
  149. .field("format","yyyy-MM-dd HH:mm:ss") //设置Date的格式
  150. .endObject()
  151. .endObject()
  152. .endObject();
  153. cib.addMapping(content, mapping);
  154. CreateIndexResponse res=cib.execute().actionGet();
  155. System.out.println("----------添加映射成功----------");
  156. }
  157. /**
  158. *  创建索引并添加文档
  159. * @throws Exception
  160. */
  161. @Test
  162. public void addIndexAndDocument() throws Exception{
  163. Date time = new Date();
  164. IndexResponse response = client.prepareIndex(article, content)
  165. .setSource(XContentFactory.jsonBuilder().startObject()
  166. .field("id","447")
  167. .field("author","fendo")
  168. .field("title","192.138.1.2")
  169. .field("content","这是JAVA有关的书籍")
  170. .field("price","20")
  171. .field("view","100")
  172. .field("tag","a,b,c,d,e,f")
  173. .field("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
  174. .endObject())
  175. .get();
  176. System.out.println("添加索引成功,版本号:"+response.getVersion());
  177. }
  178. /**
  179. * -------------------------------------Bulk---------------------------------
  180. */
  181. /**
  182. * bulkRequest
  183. * @throws Exception
  184. */
  185. @Test
  186. public void bulkRequest() throws Exception {
  187. BulkRequestBuilder bulkRequest = client.prepareBulk();
  188. Date time = new Date();
  189. // either use client#prepare, or use Requests# to directly build index/delete requests
  190. bulkRequest.add(client.prepareIndex(article, content, "199")
  191. .setSource(XContentFactory.jsonBuilder()
  192. .startObject()
  193. .field("id","199")
  194. .field("author","fendo")
  195. .field("title","BULK")
  196. .field("content","这是BULK有关的书籍")
  197. .field("price","40")
  198. .field("view","300")
  199. .field("tag","a,b,c")
  200. .field("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
  201. .endObject()
  202. )
  203. );
  204. bulkRequest.add(client.prepareIndex(article,content, "101")
  205. .setSource(XContentFactory.jsonBuilder()
  206. .startObject()
  207. .field("id","101")
  208. .field("author","fendo")
  209. .field("title","ACKSE")
  210. .field("content","这是ACKSE有关的书籍")
  211. .field("price","50")
  212. .field("view","200")
  213. .field("tag","a,b,c")
  214. .field("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
  215. .endObject()
  216. )
  217. );
  218. BulkResponse bulkResponse = bulkRequest.get();
  219. if (bulkResponse.hasFailures()) {
  220. // process failures by iterating through each bulk response item
  221. //System.out.println(bulkResponse.getTook());
  222. }
  223. }
  224. /**设置自动提交文档
  225. * BulkProcessor
  226. * @throws Exception
  227. */
  228. @Test
  229. public void autoBulkProcessor() throws Exception {
  230. BulkProcessor bulkProcessor = BulkProcessor.builder(client,
  231. new BulkProcessor.Listener() {
  232. @Override
  233. public void beforeBulk(long executionId,
  234. BulkRequest request) {
  235. //提交前调用
  236. }
  237. @Override
  238. public void afterBulk(long executionId,
  239. BulkRequest request,
  240. BulkResponse response) {
  241. //提交结束后调用(无论成功或失败)
  242. System.out.println( "提交" + response.getItems().length + "个文档,用时"+ response.getTookInMillis() + "MS" + (response.hasFailures() ? " 有文档提交失败!" : ""));
  243. }
  244. @Override
  245. public void afterBulk(long executionId,
  246. BulkRequest request,
  247. Throwable failure) {
  248. //提交结束且失败时调用
  249. System.out.println( " 有文档提交失败!after failure=" + failure);
  250. }
  251. })
  252. //当请求超过10000个(default=1000)或者总大小超过1GB(default=5MB)时,触发批量提交动作。
  253. .setBulkActions(10000)//文档数量达到1000时提交
  254. .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))//总文档体积达到5MB时提交
  255. .setFlushInterval(TimeValue.timeValueSeconds(5))//每5S提交一次(无论文档数量、体积是否达到阈值)
  256. .setConcurrentRequests(1)//加1后为可并行的提交请求数,即设为0代表只可1个请求并行,设为1为2个并行
  257. .setBackoffPolicy(
  258. BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
  259. .build();
  260. //提交单个
  261. //String json = "{\"id\":\"66\",\"author\":\"ckse\",\"title\":\"windows编程\",\"content\":\"windows 32 API编程\",\"price\":\"99\",\"view\":\"222\",\"date\":\"2017-08-01 17:21:18\"}";
  262. //bulkProcessor.add(new IndexRequest("设置的index name", "设置的type name","要插入的文档的ID").source(json));//添加文档,以便自动提交
  263. for(int i=0;i<80080;i++){
  264. //业务对象
  265. String json = "{\"id\":\""+i+"\",\"author\":\"ckse\",\"title\":\"windows编程\",\"content\":\"windows 32 API编程\",\"price\":\"99\",\"view\":\"222\",\"date\":\"2017-08-01 17:21:18\"}";
  266. System.out.println(json);
  267. bulkProcessor.add(new IndexRequest("article", "content",""+i).source(json));//添加文档,以便自动提交
  268. }
  269. System.out.println("创建成功!!!");
  270. }
  271. //手动 批量更新
  272. @Test
  273. public void multipleBulkProcessor() throws Exception {
  274. BulkRequestBuilder bulkRequest = client.prepareBulk();
  275. for(int i=500;i<1000;i++){
  276. //业务对象
  277. String jsons = "{\"id\":\""+i+"\",\"author\":\"ckse\",\"title\":\"windows编程\",\"content\":\"windows 32 API编程\",\"price\":\"99\",\"view\":\"222\",\"date\":\"2017-08-01 17:21:18\"}";
  278. IndexRequestBuilder indexRequest = client.prepareIndex("article", "content")
  279. //指定不重复的ID
  280. .setSource(jsons).setId(String.valueOf(i));
  281. //添加到builder中
  282. bulkRequest.add(indexRequest);
  283. }
  284. BulkResponse bulkResponse = bulkRequest.execute().actionGet();
  285. if (bulkResponse.hasFailures()) {
  286. // process failures by iterating through each bulk response item
  287. System.out.println(bulkResponse.buildFailureMessage());
  288. }
  289. System.out.println("创建成功!!!");
  290. }
  291. /**
  292. * 使用Bulk批量添加导入数据
  293. *
  294. */
  295. @Test
  296. public void ImportBulk(){
  297. FileReader fr = null;
  298. BufferedReader bfr = null;
  299. String line=null;
  300. try {
  301. File file = new File("F:\\Source\\Elasticsearch\\TransportClient\\src\\main\\resources\\bulk.txt");
  302. fr=new FileReader(file);
  303. bfr=new BufferedReader(fr);
  304. BulkRequestBuilder bulkRequest=client.prepareBulk();
  305. int count=0;
  306. while((line=bfr.readLine())!=null){
  307. bulkRequest.add(client.prepareIndex(article,content).setSource(line));
  308. if (count%10==0) {
  309. bulkRequest.execute().actionGet();
  310. }
  311. count++;
  312. }
  313. bulkRequest.execute().actionGet();
  314. System.out.println("导入成功!!!");
  315. } catch (Exception e) {
  316. e.printStackTrace();
  317. }finally {
  318. try {
  319. bfr.close();
  320. fr.close();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. }
  326. /**
  327. * 使用Bulk批量导出数据
  328. * @throws ExecutionException
  329. * @throws InterruptedException
  330. */
  331. @Test
  332. public void ExportBulk() throws Exception{
  333. QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
  334. SearchResponse response = client.prepareSearch("article").setQuery(queryBuilder).get();
  335. SearchHits resultHits = response.getHits();
  336. System.out.println(JSONObject.toJSON(resultHits));
  337. FileWriter fw=null;
  338. BufferedWriter bfw =null;
  339. try {
  340. File file = new File("F:\\Source\\Elasticsearch\\TransportClient\\src\\main\\resources\\bulk.txt");
  341. fw = new FileWriter(article);
  342. bfw = new BufferedWriter(fw);
  343. if (resultHits.getHits().length == 0) {
  344. System.out.println("查到0条数据!");
  345. } else {
  346. for (int i = 0; i < resultHits.getHits().length; i++) {
  347. String jsonStr = resultHits.getHits()[i]
  348. .getSourceAsString();
  349. System.out.println(jsonStr);
  350. bfw.write(jsonStr);
  351. bfw.write("\n");
  352. }
  353. }
  354. } catch (Exception e) {
  355. e.printStackTrace();
  356. }finally {
  357. try {
  358. bfw.close();
  359. fw.close();
  360. } catch (IOException e) {
  361. e.printStackTrace();
  362. }
  363. }
  364. }
  365. /**
  366. * -----------------------------------------删(删除索引,删除文档)
  367. */
  368. /**
  369. * 删除整个索引库
  370. */
  371. @Test
  372. public void deleteAllIndex(){
  373. String indexName="article";
  374. /**
  375. * 两种方式如下:
  376. */
  377. //1)
  378. //可以根据DeleteIndexResponse对象的isAcknowledged()方法判断删除是否成功,返回值为boolean类型.
  379. DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
  380. .execute().actionGet();
  381. System.out.println("是否删除成功:"+dResponse.isAcknowledged());
  382. //2)
  383. //如果传人的indexName不存在会出现异常.可以先判断索引是否存在:
  384. IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);
  385. IndicesExistsResponse inExistsResponse = client.admin().indices()
  386. .exists(inExistsRequest).actionGet();
  387. //根据IndicesExistsResponse对象的isExists()方法的boolean返回值可以判断索引库是否存在.
  388. System.out.println("是否删除成功:"+inExistsResponse.isExists());
  389. }
  390. /**
  391. * 通过ID删除
  392. */
  393. @Test
  394. public void deleteById(){
  395. DeleteResponse dResponse = client.prepareDelete(article,content, "AV49wyfCWmWw7AxKFxeb").execute().actionGet();
  396. if ("OK".equals(dResponse.status())) {
  397. System.out.println("删除成功");
  398. } else {
  399. System.out.println("删除失败");
  400. }
  401. }
  402. /**
  403. * 通过Query delete删除
  404. */
  405. @Test
  406. public void queryDelete() {
  407. //         String guid="AV49wyfCWmWw7AxKFxeb";
  408. //         String author="kkkkk";
  409. //         DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  410. //               .source(article)
  411. //               .filter(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("id", guid)).must(QueryBuilders.termQuery("author", author)).must(QueryBuilders.typeQuery(content)))
  412. //               .get();
  413. }
  414. /**
  415. * 使用matchAllQuery删除所有文档
  416. */
  417. @Test
  418. public void deleteAll(){
  419. //         DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  420. //           .source(article)
  421. //           .filter(QueryBuilders.matchAllQuery())
  422. //           .get();
  423. }
  424. /**
  425. * bulk批量通过指定id删除方法
  426. */
  427. @Test
  428. public void batchUndercarriageFamilies() {
  429. List<String> publishIds=new ArrayList<>();
  430. publishIds.add("AV49wyfCWmWw7AxKFxeY");
  431. publishIds.add("AV49wyfCWmWw7AxKFxea");
  432. BulkRequestBuilder builder=client.prepareBulk();
  433. for(String publishId:publishIds){
  434. System.out.println(publishId);
  435. builder.add(client.prepareDelete(article, content, publishId).request());
  436. }
  437. BulkResponse bulkResponse = builder.get();
  438. System.out.println(bulkResponse.status());
  439. }
  440. /**
  441. * -----------------------------------------改()
  442. */
  443. /**
  444. * 更新文档
  445. * @throws Exception
  446. */
  447. @Test
  448. public void updateDocument() throws Exception{
  449. Date time = new Date();
  450. //创建修改请求
  451. UpdateRequest updateRequest = new UpdateRequest();
  452. updateRequest.index(article);
  453. updateRequest.type(content);
  454. updateRequest.id("AV4xv5gAZLX8AvCc6ZWZ");
  455. updateRequest.doc(XContentFactory.jsonBuilder()
  456. .startObject()
  457. .field("author","FKSE")
  458. .field("title","JAVA思想")
  459. .field("content","注意:这是JAVA有关的书籍")
  460. .field("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
  461. .endObject());
  462. UpdateResponse response = client.update(updateRequest).get();
  463. System.out.println("更新索引成功");
  464. }
  465. /**
  466. * -----------------------------有问题:要引入:reindex
  467. */
  468. /**
  469. * UpdateByQueryRequestBuilder
  470. * @throws Exception
  471. */
  472. @Test
  473. public void updateByQueryRequestBuilder() throws Exception {
  474. //        UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  475. //        updateByQueryRequestBuilder
  476. //                .script(new Script(ScriptType.INLINE,"painless","ctx_source.likes++",null))
  477. //                .source()
  478. //                .setQuery(QueryBuilders.termQuery("author","kkkkk"))
  479. //                .setIndices(article)
  480. //                .get();
  481. }
  482. /**
  483. * updateByQueryRequestBuilders
  484. */
  485. @Test
  486. public void updateByQueryRequestBuilders(){
  487. //      Map<String, Object> maps=new HashMap<>();
  488. //      maps.put("orgin_session_id", 10);
  489. //      maps.put("orgin_session_id", 11);
  490. //      maps.put("orgin_session_id", 12);
  491. //      maps.put("orgin_session_id", 13);
  492. //
  493. //      Set<Map<String, Object>> docs = new HashSet<>();
  494. //      docs.add(maps);
  495. //
  496. //      UpdateByQueryRequestBuilder  ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  497. //        for (Map<String, Object> doc : docs) {
  498. //            if (doc==null || doc.isEmpty()){
  499. //                return;
  500. //            }
  501. //            Script script = new Script("ctx._source.price = ctx._version");
  502. //
  503. //            System.out.println(doc.get("orgin_session_id"));
  504. //
  505. //            //BulkIndexByScrollResponse
  506. //            BulkByScrollResponse scrollResponse = ubqrb.source(article).script(script)
  507. //                            .filter(QueryBuilders.matchAllQuery()).get();
  508. //            for (BulkItemResponse.Failure failure : scrollResponse.getBulkFailures()) {
  509. //              System.out.println(failure.getMessage());
  510. //            }
  511. //        }
  512. }
  513. /**
  514. * prepareUpdate
  515. * @throws Exception
  516. */
  517. @Test
  518. public void prepareUpdate() throws Exception {
  519. XContentBuilder endObject = XContentFactory.jsonBuilder().startObject().field("author","AAAAAAAAAAAAAAA").endObject();
  520. UpdateResponse response = client.prepareUpdate(article, content, "AV49wyfCWmWw7AxKFxeb").setDoc(endObject).get();
  521. System.out.println(response.getVersion());
  522. }
  523. /**
  524. * -----------------------------------------查()
  525. */
  526. /**
  527. * 根据index、type、id进行查询
  528. */
  529. @Test
  530. public void searchByIndex(){
  531. GetResponse response = client.prepareGet(article,content,"AV49wyfCWmWw7AxKFxec").execute()
  532. .actionGet();
  533. String json = response.getSourceAsString();
  534. if (null != json) {
  535. System.out.println(json);
  536. } else {
  537. System.out.println("未查询到任何结果!");
  538. }
  539. }
  540. /**
  541. * 查询article索引下的所有数据
  542. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-query-dsl-match-all-query.html'>
  543. * @throws Exception
  544. */
  545. @Test
  546. public  void matchAllQuery() throws Exception{
  547. QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
  548. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  549. for (SearchHit searchHit : response.getHits()) {
  550. println(searchHit);
  551. }
  552. }
  553. /**
  554. * 查询article索引下的articledate的所有数据
  555. * @throws Exception
  556. */
  557. @Test
  558. public void searchmethod1() throws Exception{
  559. SearchResponse response = client.prepareSearch(article).setTypes(content).get();
  560. println(response);
  561. for (SearchHit  searchHit: response.getHits()) {
  562. println(searchHit);
  563. }
  564. }
  565. /**
  566. * spanFirstQuery
  567. */
  568. @Test
  569. public void spanFirstQuery(){
  570. // Span First
  571. QueryBuilder queryBuilder =QueryBuilders.spanFirstQuery(
  572. QueryBuilders.spanTermQuery("title", "C"),  // Query
  573. 30000                                             // Max查询范围的结束位置
  574. );
  575. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  576. for (SearchHit searchHit : response.getHits()) {
  577. println(searchHit);
  578. }
  579. }
  580. /**
  581. * spanNearQuery
  582. */
  583. @Test
  584. public void spanNearQuery(){
  585. // Span Near TODO NotSolved
  586. QueryBuilder queryBuilder =QueryBuilders.spanNearQuery(QueryBuilders.spanTermQuery("title", "C"),1000)
  587. .addClause(QueryBuilders.spanTermQuery("name", "葫芦580娃")) // Span Term Queries
  588. .addClause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
  589. .addClause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"));
  590. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  591. for (SearchHit searchHit : response.getHits()) {
  592. println(searchHit);
  593. }
  594. }
  595. /**
  596. * spanNotQuery
  597. */
  598. @Test
  599. public void spanNotQuery(){
  600. // Span Not TODO NotSolved
  601. QueryBuilder queryBuilder =QueryBuilders.spanNotQuery(QueryBuilders.spanTermQuery("title", "C"), null);
  602. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  603. for (SearchHit searchHit : response.getHits()) {
  604. println(searchHit);
  605. }
  606. }
  607. /**
  608. * spanOrQuery
  609. */
  610. @Test
  611. public void spanOrQuery(){
  612. QueryBuilder queryBuilder =QueryBuilders.spanOrQuery(QueryBuilders.spanTermQuery("title", "C"));
  613. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  614. for (SearchHit searchHit : response.getHits()) {
  615. println(searchHit);
  616. }
  617. }
  618. public void moreLikeThisQuery(){
  619. }
  620. /**
  621. * 指定单查询条件
  622. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html#java-query-dsl-simple-query-string-query'>
  623. * @throws Exception
  624. */
  625. @Test
  626. public  void matchQuery() throws Exception{
  627. QueryBuilder qb = QueryBuilders.matchQuery(
  628. "title" ,
  629. "C"
  630. );
  631. SearchResponse response = client.prepareSearch(article).setQuery(qb).get();
  632. for (SearchHit searchHit : response.getHits()) {
  633. println(searchHit);
  634. }
  635. }
  636. /**
  637. * termQuery 查询
  638. * @throws Exception
  639. */
  640. @Test
  641. public void termQuery() throws Exception{
  642. QueryBuilder queryBuilder = QueryBuilders.termQuery("id","11");
  643. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  644. for (SearchHit  searchHit: response.getHits()) {
  645. println(searchHit);
  646. }
  647. }
  648. /**
  649. * termQuery 查询
  650. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  651. * @throws Exception
  652. */
  653. @Test
  654. public  void termsQuerys() throws Exception{
  655. QueryBuilder queryBuilder = QueryBuilders.termsQuery("id","1","2");
  656. SearchResponse response = client.prepareSearch("article").setQuery(queryBuilder).get();
  657. for (SearchHit  searchHit: response.getHits()) {
  658. println(searchHit);
  659. }
  660. }
  661. /**
  662. * 范围查询RangeQuery
  663. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  664. * @throws Exception
  665. */
  666. @Test
  667. public void rangeQuery() throws Exception{
  668. QueryBuilder queryBuilder = QueryBuilders.rangeQuery("price")
  669. .from(1)
  670. .to(100)
  671. .includeLower(false)
  672. .includeUpper(false);
  673. // A simplified form using gte, gt, lt or lte
  674. QueryBuilder _qb = QueryBuilders.rangeQuery("price")
  675. .gte("10")
  676. .lt("20");
  677. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  678. for (SearchHit  searchHit: response.getHits()) {
  679. println(searchHit);
  680. }
  681. }
  682. /**
  683. * multiMatchQuery 查询
  684. * multiMatchQuery针对的是多个field,当fieldNames有多个参数时,如field1和field2,那查询的结果中,要么field1中包含text,要么field2中包含text。
  685. */
  686. @Test
  687. public void multiMatchQuery(){
  688. QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("JAVA编程思想","title", "content");
  689. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  690. for (SearchHit  searchHit: response.getHits()) {
  691. println(searchHit);
  692. }
  693. }
  694. /**
  695. * MultiMatchQueryBuilder
  696. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html#java-query-dsl-simple-query-string-query'>
  697. * @throws Exception
  698. */
  699. @Test
  700. public void MultiMatchQueryBuilder() throws Exception {
  701. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("JAVA编程思想","title", "content");
  702. multiMatchQueryBuilder.analyzer("standard");
  703. multiMatchQueryBuilder.cutoffFrequency(0.001f);
  704. multiMatchQueryBuilder.field("title",20);
  705. multiMatchQueryBuilder.fuzziness(Fuzziness.TWO);
  706. multiMatchQueryBuilder.maxExpansions(100);
  707. multiMatchQueryBuilder.prefixLength(10);
  708. multiMatchQueryBuilder.tieBreaker(20);
  709. multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.BEST_FIELDS);
  710. multiMatchQueryBuilder.boost(20);
  711. SearchResponse searchResponse =  client.prepareSearch()
  712. .setIndices(article)
  713. .setTypes(content)
  714. .setQuery(multiMatchQueryBuilder)
  715. .execute()
  716. .actionGet();
  717. for (SearchHit  searchHit: searchResponse.getHits()) {
  718. println(searchHit);
  719. }
  720. }
  721. /**
  722. * MatchQueryBuilder
  723. * @throws Exception
  724. */
  725. @Test
  726. public void MatchQueryBuilder() throws Exception {
  727. MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title","JAVA编程思想");
  728. matchQueryBuilder.fuzziness(Fuzziness.AUTO);
  729. SearchResponse searchResponse = client.prepareSearch()
  730. .setIndices(article)
  731. .setTypes(content)
  732. .setQuery(matchQueryBuilder)
  733. .execute()
  734. .actionGet();
  735. println(searchResponse);
  736. }
  737. /**
  738. * 和matchQuery一样
  739. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html#java-query-dsl-simple-query-string-query'>
  740. * @throws Exception
  741. */
  742. @Test
  743. public  void commonTermsQuery() throws Exception{
  744. QueryBuilder queryBuilder = QueryBuilders.commonTermsQuery("id",
  745. "1");
  746. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  747. for (SearchHit  searchHit: response.getHits()) {
  748. println(searchHit);
  749. }
  750. }
  751. /**
  752. * MultiGetResponse  查询多个xxx的值
  753. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-multi-get.html'>
  754. * @throws Exception
  755. */
  756. @Test
  757. public void MultiGetResponse() throws Exception {
  758. MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
  759. .add(article, content, "526")
  760. .add(article, content, "572", "582", "613")
  761. .get();
  762. for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
  763. GetResponse response = itemResponse.getResponse();
  764. if (response.isExists()) {
  765. String json = response.getSourceAsString();
  766. System.out.println(json);
  767. }
  768. }
  769. }
  770. /**
  771. * +包含 -除外
  772. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html#java-query-dsl-simple-query-string-query'>
  773. * @throws Exception
  774. */
  775. @Test
  776. public void queryStringQuery() throws Exception{
  777. QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("*:*");
  778. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  779. for (SearchHit  searchHit: response.getHits()) {
  780. println(searchHit);
  781. }
  782. }
  783. /**
  784. * +包含 -除外
  785. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html#java-query-dsl-simple-query-string-query'>
  786. * @throws Exception
  787. */
  788. @Test
  789. public void simpleQueryStringQuery() throws Exception{
  790. QueryBuilder queryBuilder = QueryBuilders.simpleQueryStringQuery("+id:1");
  791. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  792. for (SearchHit  searchHit: response.getHits()) {
  793. println(searchHit);
  794. }
  795. }
  796. /**
  797. * existsQuery
  798. * 匹配含有id字段的记录
  799. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  800. * @throws Exception
  801. */
  802. @Test
  803. public void existsQuery() throws Exception{
  804. QueryBuilder queryBuilder = QueryBuilders.existsQuery("id");
  805. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  806. for (SearchHit  searchHit: response.getHits()) {
  807. println(searchHit);
  808. }
  809. }
  810. /**
  811. * prefixQuery  匹配包含具有指定前缀的术语的文档的查询
  812. * 匹配title中前缀为JAVA的记录
  813. * 匹配分词前缀 如果字段没分词,就匹配整个字段前缀
  814. * 前缀匹配(比如我要查询的是192.168.1.12,但是当输入192.168、192.168.1、192.168.1.1等的情况都会有相应结果返回,只不过是个范围)
  815. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  816. * @throws Exception
  817. */
  818. @Test
  819. public void prefixQuery() throws Exception{
  820. QueryBuilder queryBuilder = QueryBuilders.prefixQuery(
  821. "title",
  822. "192.138"
  823. );
  824. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  825. for (SearchHit  searchHit: response.getHits()) {
  826. println(searchHit);
  827. }
  828. }
  829. /**
  830. * MatchPhrasePrefixQueryBuilder  为提供的字段名称和文本创建一个类型为“PHRASE_PREFIX”的匹配查询。
  831. * @throws Exception
  832. */
  833. @Test
  834. public void MatchPhrasePrefixQueryBuilder() throws Exception {
  835. String key = "C++";
  836. MatchPhrasePrefixQueryBuilder matchPhrasePrefixQueryBuilder = QueryBuilders.matchPhrasePrefixQuery("title",key);
  837. matchPhrasePrefixQueryBuilder.boost(10);
  838. matchPhrasePrefixQueryBuilder.analyzer("standard");
  839. matchPhrasePrefixQueryBuilder.slop(2);
  840. matchPhrasePrefixQueryBuilder.maxExpansions(100);
  841. SearchResponse searchResponse = client.prepareSearch()
  842. .setIndices(article)
  843. .setTypes(content)
  844. .setQuery(matchPhrasePrefixQueryBuilder)
  845. .execute()
  846. .actionGet();
  847. for (SearchHit  searchHit: searchResponse.getHits()) {
  848. println(searchHit);
  849. }
  850. }
  851. /**
  852. * wildcardQuery
  853. * 通配符
  854. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  855. * @throws Exception
  856. */
  857. @Test
  858. public void wildcardQuery() throws Exception{
  859. QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("author", "*e");//J?V*
  860. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  861. for (SearchHit  searchHit: response.getHits()) {
  862. println(searchHit);
  863. }
  864. }
  865. /**
  866. * fuzzyQuery  使用模糊查询匹配文档的查询
  867. * @throws Exception
  868. */
  869. @Test
  870. public  void fuzzyQuery() throws Exception{
  871. QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery(
  872. "author",
  873. "e"
  874. );
  875. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  876. for (SearchHit  searchHit: response.getHits()) {
  877. println(searchHit);
  878. }
  879. }
  880. /**
  881. * boolQuery 匹配与其他查询的布尔组合匹配的文档的查询。
  882. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-compound-queries.html'>
  883. * @throws Exception
  884. */
  885. @Test
  886. public void BoostQuery() throws Exception{
  887. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
  888. .should(QueryBuilders.termQuery("author","eeee")).boost(100) //设置此查询的权重。 匹配此查询的文件(除正常权重之外)的得分乘以提供的提升。
  889. .should(QueryBuilders.termQuery("id","AV5NF_Dbhqf-jFOFkksT").boost(1));
  890. SearchResponse response=client.prepareSearch("article").setQuery(boolQueryBuilder).execute().get();
  891. for (SearchHit  searchHit: response.getHits()) {
  892. println(searchHit);
  893. }
  894. }
  895. /**
  896. * boolQuery
  897. * @throws Exception
  898. */
  899. @Test
  900. public void boolQuery() throws Exception {
  901. QueryBuilder qb = QueryBuilders.boolQuery()
  902. .must(QueryBuilders.termQuery("author", "eeee"))
  903. .must(QueryBuilders.termQuery("title", "JAVA思想"))
  904. .mustNot(QueryBuilders.termQuery("content", "C++")) //添加不得出现在匹配文档中的查询。
  905. .should(QueryBuilders.termQuery("id", "AV5NF_Dbhqf-jFOFkksT"))//添加应该与返回的文档匹配的子句。 对于具有no的布尔查询,子句必须一个或多个SHOULD子句且必须与文档匹配,用于布尔值查询匹配。 不允许null值。
  906. .filter(QueryBuilders.termQuery("price", "30.3"));//添加一个查询,必须出现在匹配的文档中,但会不贡献得分。 不允许null值。
  907. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  908. for (SearchHit  searchHit: response.getHits()) {
  909. println(searchHit);
  910. }
  911. }
  912. /**
  913. * boostingQuery
  914. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-compound-queries.html'>
  915. * @throws Exception
  916. */
  917. @Test
  918. public void boostingQuery() throws Exception {
  919. QueryBuilder qb = QueryBuilders.boostingQuery(
  920. QueryBuilders.termQuery("id","AV5NF_Dbhqf-jFOFkksR"),
  921. QueryBuilders.termQuery("title","C"))
  922. .negativeBoost(0.2f);//设置负增强因子。
  923. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  924. for (SearchHit  searchHit: response.getHits()) {
  925. println(searchHit);
  926. }
  927. }
  928. /**
  929. * constantScoreQuery
  930. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-compound-queries.html'>
  931. * @throws Exception
  932. */
  933. @Test
  934. public void constantScoreQuery() throws Exception {
  935. QueryBuilder qb = QueryBuilders.constantScoreQuery(
  936. QueryBuilders.termQuery("title","C")
  937. ).boost(2.0f);
  938. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  939. for (SearchHit  searchHit: response.getHits()) {
  940. println(searchHit);
  941. }
  942. }
  943. /**
  944. * disMaxQuery
  945. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-compound-queries.html'>
  946. * @throws Exception
  947. */
  948. @Test
  949. public void disMaxQuery() throws Exception {
  950. QueryBuilder qb = QueryBuilders.disMaxQuery()
  951. .add(QueryBuilders.termQuery("id", "512"))
  952. .add(QueryBuilders.termQuery("author", "ckse"))
  953. .boost(1.2f)
  954. .tieBreaker(0.7f);
  955. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  956. for (SearchHit  searchHit: response.getHits()) {
  957. println(searchHit);
  958. }
  959. }
  960. /**
  961. * functionScoreQuery
  962. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-compound-queries.html'>
  963. * @throws Exception
  964. */
  965. @Test
  966. public void functionScoreQuery() throws Exception {
  967. FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
  968. new FunctionScoreQueryBuilder.FilterFunctionBuilder(
  969. QueryBuilders.matchQuery("id", "512"),
  970. ScoreFunctionBuilders.randomFunction("ABCDEF")),
  971. new FunctionScoreQueryBuilder.FilterFunctionBuilder(
  972. ScoreFunctionBuilders.exponentialDecayFunction("age", 0L, 1L))
  973. };
  974. QueryBuilder qb = QueryBuilders.functionScoreQuery(functions);
  975. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  976. for (SearchHit  searchHit: response.getHits()) {
  977. println(searchHit);
  978. }
  979. }
  980. /**
  981. * regexpQuery 匹配包含具有指定正则表达式的术语的文档的查询。
  982. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  983. * @throws Exception
  984. */
  985. @Test
  986. public void regexpQuery() throws Exception {
  987. QueryBuilder qb = QueryBuilders.regexpQuery(
  988. "title",
  989. "*J");
  990. SearchResponse response=client.prepareSearch().setQuery(qb).execute().actionGet();
  991. for (SearchHit  searchHit: response.getHits()) {
  992. println(searchHit);
  993. }
  994. }
  995. /**
  996. * typeQuery
  997. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  998. * @throws Exception
  999. */
  1000. @Test
  1001. public  void typeQuery() throws Exception{
  1002. QueryBuilder queryBuilder = QueryBuilders.typeQuery("data");
  1003. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  1004. for (SearchHit  searchHit: response.getHits()) {
  1005. println(searchHit);
  1006. }
  1007. }
  1008. /**
  1009. * idsQuery
  1010. * 类型是可选的
  1011. * 指定type和id进行查询。
  1012. * <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-term-level-queries.html'>
  1013. * @throws Exception
  1014. */
  1015. @Test
  1016. public  void idsQuery() throws Exception{
  1017. QueryBuilder queryBuilder = QueryBuilders.idsQuery(content)
  1018. .addIds("512", "520", "531");
  1019. SearchResponse response = client.prepareSearch(article).setQuery(queryBuilder).get();
  1020. for (SearchHit  searchHit: response.getHits()) {
  1021. println(searchHit);
  1022. }
  1023. }
  1024. /**
  1025. * group 分组查询
  1026. */
  1027. @Test
  1028. public void group(){
  1029. }
  1030. /**
  1031. * Aggregation
  1032. */
  1033. @Test
  1034. public void Aggregation()
  1035. {
  1036. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  1037. //添加时间范围过滤
  1038. boolQueryBuilder.must(QueryBuilders.rangeQuery("@timestamp").format("yyyy-MM-dd HH:mm:ss").gte("").lte(""));
  1039. AggregationBuilder aggregationBuilder = AggregationBuilders
  1040. //terms(查询字段别名).field(分组字段)
  1041. .terms("").field("")
  1042. .order(Terms.Order.aggregation("", false))
  1043. .size(10)
  1044. .subAggregation(AggregationBuilders.count("").field(""));
  1045. SearchRequestBuilder searchRequestBuilder = client.prepareSearch("article").setTypes("articledate")
  1046. .setQuery(boolQueryBuilder)
  1047. .addAggregation(aggregationBuilder)
  1048. .setSize(0);
  1049. SearchResponse sr = searchRequestBuilder.execute().actionGet();
  1050. Terms genders = sr.getAggregations().get("");//统计字段别名
  1051. for (Terms.Bucket entry : genders.getBuckets())
  1052. {
  1053. System.out.println((String) entry.getKey()+"-("+entry.getDocCount()+")");
  1054. }
  1055. //如想group by 时间,并且按天来进行分组
  1056. AggregationBuilder aggregation = AggregationBuilders
  1057. .dateHistogram("agg")
  1058. .field("@timestamp")
  1059. .format("yyyy-MM-dd")
  1060. .dateHistogramInterval(DateHistogramInterval.DAY);
  1061. //可能有新需求,group by 时间,姓名
  1062. //AggregationBuilder nameAgg = AggregationBuilders.terms(姓名别名).field(姓名).size(10);
  1063. //aggregation.subAggregation(nameAgg);
  1064. //可以能需要进行名称统计,但是需要distinct
  1065. //aggregation.subAggregation(AggregationBuilders.cardinality(别名).field(姓名))
  1066. //其他如下
  1067. //        (1)统计某个字段的数量
  1068. //        ValueCountBuilder vcb=  AggregationBuilders.count("count_uid").field("uid");
  1069. //      (2)去重统计某个字段的数量(有少量误差)
  1070. //       CardinalityBuilder cb= AggregationBuilders.cardinality("distinct_count_uid").field("uid");
  1071. //      (3)聚合过滤
  1072. //      FilterAggregationBuilder fab= AggregationBuilders.filter("uid_filter").filter(QueryBuilders.queryStringQuery("uid:001"));
  1073. //      (4)按某个字段分组
  1074. //      TermsBuilder tb=  AggregationBuilders.terms("group_name").field("name");
  1075. //      (5)求和
  1076. //      SumBuilder  sumBuilder= AggregationBuilders.sum("sum_price").field("price");
  1077. //      (6)求平均
  1078. //      AvgBuilder ab= AggregationBuilders.avg("avg_price").field("price");
  1079. //      (7)求最大值
  1080. //      MaxBuilder mb= AggregationBuilders.max("max_price").field("price");
  1081. //      (8)求最小值
  1082. //      MinBuilder min= AggregationBuilders.min("min_price").field("price");
  1083. //      (9)按日期间隔分组
  1084. //      DateHistogramBuilder dhb= AggregationBuilders.dateHistogram("dh").field("date");
  1085. //      (10)获取聚合里面的结果
  1086. //      TopHitsBuilder thb=  AggregationBuilders.topHits("top_result");
  1087. //      (11)嵌套的聚合
  1088. //      NestedBuilder nb= AggregationBuilders.nested("negsted_path").path("quests");
  1089. //      (12)反转嵌套
  1090. //      AggregationBuilders.reverseNested("res_negsted").path("kps ");
  1091. }
  1092. /**
  1093. * MultiSearchResponse 多字段检索
  1094. */
  1095. @Test
  1096. public void MultiSearchResponse(){
  1097. SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("JAVA"));
  1098. SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("title", "C"));
  1099. MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).get();
  1100. for (MultiSearchResponse.Item item : sr.getResponses()) {
  1101. SearchResponse response = item.getResponse();
  1102. for (SearchHit searchHit : response.getHits()) {
  1103. println(searchHit);
  1104. }
  1105. }
  1106. }
  1107. /**
  1108. * 复杂查询
  1109. */
  1110. @Test
  1111. public void complexSearch1(){
  1112. int page=1;
  1113. int pageSize=10;
  1114. String keyword="";
  1115. BoolQueryBuilder boolQueryBuilder=QueryBuilders.boolQuery();
  1116. if(keyword!=null&&!keyword.equals("")){
  1117. QueryBuilder nameBuilder=QueryBuilders.matchQuery("zuName", keyword).analyzer("ik_max_word").boost(10);
  1118. QueryBuilder labelBuilder=QueryBuilders.matchQuery("zuLabelName", keyword).analyzer("ik_max_word").boost(10);
  1119. QueryBuilder categoryBuilder=QueryBuilders.matchQuery("categoryName", keyword).analyzer("ik_max_word").boost(10);
  1120. boolQueryBuilder.should(nameBuilder).should(labelBuilder).should(categoryBuilder);
  1121. }else{
  1122. boolQueryBuilder.must(QueryBuilders.matchAllQuery());
  1123. }
  1124. SearchResponse response=client.prepareSearch("article").setTypes("articledate")
  1125. .setQuery(boolQueryBuilder)
  1126. .setFrom((page-1)*pageSize).setSize(pageSize)
  1127. .setExplain(true)
  1128. .get();
  1129. SearchHits hits=response.getHits();
  1130. }
  1131. /**
  1132. * 复杂查询2
  1133. */
  1134. @Test
  1135. public void complexSearch2(){
  1136. String relatedValue="fendo";
  1137. String userId="1234";
  1138. int page=1;
  1139. int pageSize=10;
  1140. BoolQueryBuilder builders=new BoolQueryBuilder();
  1141. //加上条件
  1142. builders.must(QueryBuilders.termQuery("userId", userId));
  1143. if(relatedValue=="fendo"){
  1144. builders.must(QueryBuilders.nestedQuery("related4ZuValue",
  1145. QueryBuilders.boolQuery()
  1146. .must(QueryBuilders.termQuery("related4ZuValue.nameValue", ""))
  1147. //.must(QueryBuilders.rangeQuery("endTime").lte(LongformStringDate(System.currentTimeMillis())))
  1148. ,ScoreMode.None));
  1149. }else{
  1150. builders.must(QueryBuilders.nestedQuery("related4ZuValue", QueryBuilders.termQuery("related4ZuValue.nameValue", ""),
  1151. ScoreMode.None));
  1152. }
  1153. SearchResponse response=client.prepareSearch("article").setTypes("articledate")
  1154. .setQuery(builders).setFrom((page-1)*pageSize)
  1155. .setSize(pageSize)
  1156. .get();
  1157. SearchHits hits=response.getHits();
  1158. }
  1159. /**
  1160. * 取查询结果总和count
  1161. */
  1162. @Test
  1163. public void countSum() {
  1164. int relatedValue=1;
  1165. String userId="111";
  1166. BoolQueryBuilder builders=new BoolQueryBuilder();
  1167. builders.must(QueryBuilders.termQuery("userId", userId));
  1168. if(relatedValue==1){
  1169. builders.must(QueryBuilders.nestedQuery("related4ZuValue",QueryBuilders.boolQuery()
  1170. .must(QueryBuilders.termQuery("related4ZuValue.nameValue", "123"))
  1171. .must(QueryBuilders.rangeQuery("endTime").lte(""))
  1172. ,ScoreMode.None));
  1173. }else{
  1174. builders.must(QueryBuilders.nestedQuery("related4ZuValue", QueryBuilders.termQuery("related4ZuValue.nameValue", "111"),
  1175. ScoreMode.None));
  1176. }
  1177. SearchResponse response=client.prepareSearch("article").setTypes("articledate")
  1178. .setQuery(builders)
  1179. .setSize(1)
  1180. .get();
  1181. SearchHits hits=response.getHits();
  1182. System.out.println(hits.getTotalHits());
  1183. }
  1184. /**
  1185. * 聚合求和sum
  1186. * @param keyword
  1187. * @param startTime
  1188. * @param endTime
  1189. */
  1190. @Test
  1191. public void getPlatformZuOrdersTotalAmount() {
  1192. String keyword="";
  1193. String startTime="";
  1194. String endTime="";
  1195. BoolQueryBuilder boolQueryBuilder=QueryBuilders.boolQuery();
  1196. if(keyword==null||keyword.equals("")){
  1197. QueryBuilder queryBuilder=QueryBuilders.matchAllQuery();
  1198. boolQueryBuilder.must(queryBuilder);
  1199. }else{
  1200. QueryBuilder zuNameBuilder=QueryBuilders.matchQuery("zuName", keyword);
  1201. QueryBuilder buyerNameBuilder=QueryBuilders.matchQuery("buyerName", keyword);
  1202. QueryBuilder sellerNameBuilder=QueryBuilders.matchQuery("sellerName", keyword);
  1203. boolQueryBuilder.should(zuNameBuilder).should(buyerNameBuilder).should(sellerNameBuilder);
  1204. }
  1205. if(!startTime.equals("")){
  1206. QueryBuilder addTimeBuilder=QueryBuilders.rangeQuery("addTime").from(startTime).to(endTime);
  1207. boolQueryBuilder.must(addTimeBuilder);
  1208. }
  1209. SearchResponse response=client.prepareSearch("article").setTypes("articledate")
  1210. .setQuery(boolQueryBuilder)
  1211. .addAggregation(AggregationBuilders.sum("price").field("price"))
  1212. .get();
  1213. Sum sum=response.getAggregations().get("price");
  1214. System.out.println(sum.getValue());
  1215. }
  1216. /**
  1217. * ---------------------------分页
  1218. */
  1219. /**
  1220. * 使用Scroll方法分页
  1221. */
  1222. @Test
  1223. public void queryPageScroll(){
  1224. QueryBuilder qb = QueryBuilders.termQuery("id", "1");
  1225. SearchResponse scrollResp = client.prepareSearch("article")
  1226. .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
  1227. .setScroll(new TimeValue(60000))
  1228. .setQuery(qb)
  1229. .setSize(1).get();
  1230. do {
  1231. for (SearchHit hit : scrollResp.getHits().getHits()) {
  1232. println(hit);
  1233. }
  1234. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
  1235. } while(scrollResp.getHits().getHits().length != 0);
  1236. }
  1237. /**
  1238. * 分页
  1239. * @throws Exception
  1240. */
  1241. @Test
  1242. public void fenye() throws Exception {
  1243. SearchResponse response = client.prepareSearch("article")
  1244. .setQuery(QueryBuilders.matchAllQuery())
  1245. .setFrom(10)
  1246. .setSize(20)
  1247. .execute().actionGet();
  1248. for (SearchHit searchHit : response.getHits()) {
  1249. println(searchHit);
  1250. }
  1251. }
  1252. /**
  1253. * 高亮
  1254. * @throws Exception
  1255. */
  1256. @Test
  1257. public void highlighter() throws Exception{
  1258. QueryBuilder matchQuery = QueryBuilders.matchQuery("author", "fendo");
  1259. HighlightBuilder hiBuilder=new HighlightBuilder();
  1260. hiBuilder.preTags("<h2>");
  1261. hiBuilder.postTags("</h2>");
  1262. hiBuilder.field("author");
  1263. // 搜索数据
  1264. SearchResponse response = client.prepareSearch("article")
  1265. .setQuery(matchQuery)
  1266. .highlighter(hiBuilder)
  1267. .execute().actionGet();
  1268. for (SearchHit searchHit : response.getHits()) {
  1269. println(searchHit);
  1270. }
  1271. }
  1272. /**
  1273. * ---------------------------分词器
  1274. */
  1275. /**
  1276. * AnalyzeRequest 分词器
  1277. * <a href='https://www.elastic.co/guide/cn/elasticsearch/guide/current/standard-tokenizer.html'>
  1278. * @throws Exception
  1279. */
  1280. @Test
  1281. public void AnalyzeRequest() throws Exception {
  1282. AnalyzeRequest analyzeRequest = new AnalyzeRequest();
  1283. analyzeRequest.text("My œsophagus caused a débâcle");
  1284. /**
  1285. * whitespace (空白字符)分词器按空白字符 —— 空格、tabs、换行符等等进行简单拆分
  1286. * letter 分词器 ,采用另外一种策略,按照任何非字符进行拆分
  1287. * standard 分词器使用 Unicode 文本分割算法
  1288. */
  1289. analyzeRequest.addTokenFilter("standard");
  1290. analyzeRequest.addCharFilter("asciifolding");
  1291. ActionFuture<AnalyzeResponse> analyzeResponseActionFuture =  client.admin().indices().analyze(analyzeRequest);
  1292. List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
  1293. for (AnalyzeResponse.AnalyzeToken analyzeToken : analyzeTokens){
  1294. System.out.println(analyzeToken.getTerm());
  1295. }
  1296. }
  1297. /**
  1298. * IK分词器
  1299. * @param args
  1300. * @throws IOException
  1301. */
  1302. public  void IKAnalyzer(String []args) throws IOException {
  1303. Settings settings  = Settings.EMPTY;
  1304. IKAnalyzer analyzer = new IKAnalyzer();
  1305. String text = "*国歌";
  1306. StringReader stringReader = new StringReader(text);
  1307. TokenStream tokenStream = analyzer.tokenStream("",stringReader);
  1308. tokenStream.reset();
  1309. CharTermAttribute term=tokenStream.getAttribute(CharTermAttribute.class);
  1310. while(tokenStream.incrementToken()){
  1311. System.out.print(term.toString()+"—");
  1312. }
  1313. stringReader.close();
  1314. tokenStream.close();
  1315. }
  1316. /**
  1317. * 输出结果SearchResponse
  1318. * @param response
  1319. */
  1320. public static void println(SearchResponse response){
  1321. System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
  1322. System.err.println(
  1323. "getFailedShards : " + response.getFailedShards() + "\n" +
  1324. "getNumReducePhases : " + response.getNumReducePhases() + "\n" +
  1325. "getScrollId : " + response.getScrollId() +  "\n" +
  1326. "getTookInMillis : " + response.getTookInMillis() + "\n" +
  1327. "getTotalShards : " + response.getTotalShards() +  "\n" +
  1328. "getAggregations : " + response.getAggregations() + "\n" +
  1329. "getProfileResults : " + response.getProfileResults() + "\n" +
  1330. "getShardFailures : " + response.getShardFailures() + "\n" +
  1331. "getSuggest : " + response.getSuggest() + "\n" +
  1332. "getTook : " + response.getTook() + "\n" +
  1333. "isTerminatedEarly : " + response.isTerminatedEarly() + "\n" +
  1334. "isTimedOut : " + response.isTimedOut() + "\n" +
  1335. "remoteAddress : " + response.remoteAddress() + "\n" +
  1336. "status : " + response.status() + "\n" +
  1337. "getHits : " + response.getHits()
  1338. );
  1339. }
  1340. /**
  1341. * 输出结果SearchResponse
  1342. * @param response
  1343. */
  1344. public static void println(SearchHit searchHit){
  1345. System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
  1346. System.err.println(
  1347. "docId : " + searchHit.docId() + "\n" +
  1348. "getId : " + searchHit.getId() + "\n" +
  1349. "getIndex : " + searchHit.getIndex()+ "\n" +
  1350. "getScore : " + searchHit.getScore() + "\n" +
  1351. "getSourceAsString : " + searchHit.getSourceAsString() + "\n" +
  1352. "getType : " + searchHit.getType() + "\n" +
  1353. "getVersion : " + searchHit.getVersion() + "\n" +
  1354. "fieldsOrNull : " + searchHit.fieldsOrNull() + "\n" +
  1355. "getExplanation : " + searchHit.getExplanation() + "\n" +
  1356. "getFields : " + searchHit.getFields() + "\n" +
  1357. "highlightFields : " + searchHit.highlightFields() + "\n" +
  1358. "hasSource : " + searchHit.hasSource()
  1359. );
  1360. }
  1361. }

完整项目如下:http://download.csdn.net/download/u011781521/9964499

上一篇:Java操作Elasticsearch 之 [Java High Level REST Clientedit]


下一篇:使用adb命令查看APP包名 和 包入口方法