The background,

Recently, I learned the integration of ES and Java. During the integration process, I found that I did not know how to write some query statements in Java, and I could not find comprehensive ones on the Internet, so I sorted them out by myself for subsequent reference.

Two, prepare the environment

2.1 Environment Information

The framework The version number note
springboot 2.2.6. RELEASE
Elasticsearch 7.1.0 Single node, not cluster

2.2 Setting up the SpringBoot Environment

Maven configuration requires the following JAR packages:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
Copy the code

2.3 Adding es Configuration Files

/** * es configuration file **@author liuqiuyi
 * @date2021/4/19 23:06 * /
@Configuration
public class TransportClientConfig extends ElasticsearchConfigurationSupport {

	@Bean
	public Client elasticsearchClient(a) throws UnknownHostException {
		Settings settings = Settings.builder().put("cluster.name"."elasticsearch").build();
		TransportClient client = new PreBuiltTransportClient(settings);
                // Note that the port is 9300
		client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
		return client;
	}

	@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
	public ElasticsearchTemplate elasticsearchTemplate(a) throws UnknownHostException {
		return newElasticsearchTemplate(elasticsearchClient(), entityMapper()); }}Copy the code

Three, use examples

3.1 Add, Delete and modify statements

  • A new Java bean
/** * es test object class *@author liuqiuyi
 * @date2021/4/22 transgress * /
@Data
@Document(indexName = "employee", type = "_doc")
public class Employee implements Serializable {
	private static final long serialVersionUID = 813700671392848305L;

	private Long id;

	private String name;

	private String job;

	private Integer age;

	private String gender;

	public Employee(Long id, String name, String job, Integer age, String gender) {
		this.id = id;
		this.name = name;
		this.job = job;
		this.age = age;
		this.gender = gender;
	}

	public Employee(a) {}}Copy the code
  • Creating a Test Class
/** * add/delete test **@author liuqiuyi
 * @date2021/4/28 but * /
public class CrudTest extends UtilsApplicationTests {
	@Resource
	ElasticsearchTemplate elasticsearchTemplate;

	/** * Single save or update ** ID does not exist, save * ID already exists, update **@author liuqiuyi
	 * @date2021/4/28 all * /
	@Test
	public void saveOrUpdateTest(a) {
		Employee employee = new Employee(17L."liuqiuyi"."The whole stack".25."Male");

		IndexQuery indexQuery = new IndexQueryBuilder()
				.withObject(employee)
				.build();
		String index = elasticsearchTemplate.index(indexQuery);
		// Index is the data id, if specified, returns the specified id, if not specified, returns an es automatically generated
		System.out.println(index);
	}


	/** * Batch save **@author liuqiuyi
	 * @date2021/4/29 19:53 * /
	@Test
	public void batchSaveTest(a) {
		Employee employeeA = new Employee(18L."liuqiuyi"."java".25."Male");
		Employee employeeB = new Employee(19L."liuqiuyi"."java".25."Male");
		ArrayList<Employee> employeeArrayList = Lists.newArrayList(employeeA, employeeB);

		List<IndexQuery> indexQueryList = Lists.newArrayList();
		for (Employee employee : employeeArrayList) {
			IndexQuery indexQuery = new IndexQueryBuilder()
					.withObject(employee)
					.build();

			indexQueryList.add(indexQuery);
		}

		elasticsearchTemplate.bulkIndex(indexQueryList);
	}

	/** * delete ** by id@author liuqiuyi
	 * @date2021/4/29 20:30 * /
	@Test
	public void deleteByIdTest(a){
		String delete = elasticsearchTemplate.delete(Employee.class, "79R2HXkBm5pjjA5okt3o");
		System.out.println(delete);
	}

	/** * delete ** in batches@author liuqiuyi
	 * @date2021/5/6 and * /
	@Test
	public void batchDeleteByIdsTest(a){
		CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
		criteriaQuery.setIds(Lists.newArrayList("18"."19")); elasticsearchTemplate.delete(criteriaQuery, Employee.class); }}Copy the code

3.2 Simple DSL queries

3.2.1 Importing Test Data

  • Import using logstash. See appendix for details

3.2.2 Creating a Java Bean

/** * ES test object * needs to be added@Document, specify the index name and type(fixed value after es 7) * *@author liuqiuyi
 * @date 2021/4/19 23:37
 */
@Data
@Document(indexName = "movies", type = "_doc")
public class Movies implements Serializable {
	private static final long serialVersionUID = -343559824622431609L;

	private String id;
	private String title;
	private String[] genre;
	private Long year;
}
Copy the code

3.2.3 Query Example

/** * simple DSL query **@author liuqiuyi
 * @date2021/4/22 and * /
public class SimpleQueryTest extends UtilsApplicationTests {
	@Resource
	ElasticsearchTemplate elasticsearchTemplate;


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "term": {
	 *       "title": {
	 *         "value": "beautiful"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 yet * /
	@Test
	public void termTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(termQuery("title"."beautiful"))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "terms": {
	 *       "title": [
	 *         "beautiful",
	 *         "mind"
	 *       ]
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 16:53 * /
	@Test
	public void termsTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(termsQuery("title"."beautiful"."mind"))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "range": {
	 *       "year": {
	 *         "gte": 2016,
	 *         "lte": 2018
	 *       }
	 *     }
	 *   },
	 *   "sort": [
	 *     {
	 *       "year": {
	 *         "order": "desc"
	 *       }
	 *     }
	 *   ]
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 16:57 * /
	@Test
	public void rangeTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(rangeQuery("year").gte(2016).lte(2018))
				.withSort(fieldSort("year").order(SortOrder.DESC))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "constant_score": {
	 *       "filter": {
	 *         "term": {
	 *           "title": "beautiful"
	 *         }
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 17:02 * /
	@Test
	public void constant_scoreTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(constantScoreQuery(QueryBuilders.termQuery("title"."beautiful")))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/** * GET movies/_search * { * "query": { * "match": { * "title": "beautiful" * } * }, * "from": 10, * "size": 10 *} * *@author liuqiuyi
	 * @date2021/4/21 17:06 * /
	@Test
	public void matchTest(a) {
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(matchQuery("title"."beautiful"))
				// Es paging starts at 0 by default
				.withPageable(PageRequest.of(1.10)).build();

		AggregatedPage<Movies> queryForPage = elasticsearchTemplate.queryForPage(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(queryForPage.getContent()));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "_source": [
	 *     "title",
	 *     "id"
	 *   ],
	 *   "query": {
	 *     "match": {
	 *       "title": "beautiful mind"
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date 2021/4/21 17:13
	 */
	@Test
	public void match_sourceTest(a) {
		String[] includes = new String[]{"title"."id"};
		FetchSourceFilter sourceFilter = new FetchSourceFilter(includes, null);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(matchQuery("title"."beautiful mind"))
				.withSourceFilter(sourceFilter)
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "match_phrase": {
	 *       "title": "beautiful mind"
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 and * /
	@Test
	public void match_phraseTest(a) {
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(matchPhraseQuery("title"."beautiful mind"))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "multi_match": {
	 *       "query": "beautiful Adventure",
	 *       "fields": [
	 *         "title",
	 *         "genre"
	 *       ]
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 returned to * /
	@Test
	public void multi_matchTest(a) {
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(multiMatchQuery("beautiful Adventure"."title"."genre"))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "match_all": {}
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date 2021/4/21 17:26
	 */
	@Test
	public void match_allTest(a) {
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(matchAllQuery())
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}

	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "query_string": {
	 *       "default_field": "title",
	 *       "query": "mind beautiful",
	 *       "default_operator": "AND"
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 laboureth * /
	@Test
	public void query_stringTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(queryStringQuery("mind beautiful").defaultField("title").defaultOperator(Operator.AND))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "simple_query_string": {
	 *       "query": "beautiful + mind",
	 *       "fields": [
	 *         "title"
	 *       ]
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/21 answering * /
	@Test
	public void simple_query_stringTest(a){
		Map<String, Float> fields = Maps.newHashMap();
		// Do not know what the value is
		fields.put("title".1f);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(simpleQueryStringQuery("mind beautiful").fields(fields).defaultOperator(Operator.AND))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/** * GET movies/_search * { * "query": { * "fuzzy": { * "title": { * "value": "neverendign", * "fuzziness": 1, * "prefix_length": 5 *} *} *} * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *@author liuqiuyi
	 * @date2021/4/21 saveth * /
	@Test
	public void fuzzyTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(fuzzyQuery("title"."neverendign").fuzziness(Fuzziness.ONE).transpositions(true).prefixLength(5))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}



	/** * GET movies/_search * { * "query": { * "bool": { * "must": [ * { * "simple_query_string": { * "query": "beautiful mind", * "fields": [ * "title" * ] * } * }, * { * "range": { * "year": { * "gte": 2016, * "lte": 2018 *} *} *} *] *} *} *} * * *@author liuqiuyi
	 * @date2021/4/22 * /
	@Test
	public void boolTest(a){
		Map<String, Float> fields = Maps.newHashMap();
		// Do not know what the value is
		fields.put("title".1f);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(boolQuery().must(simpleQueryStringQuery("beautiful mind").fields(fields)).must(rangeQuery("year").gte(2016).lte(2018)))
				.build();

		List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class);

		System.out.println(JSON.toJSONString(movies));
	}


	/**
	 * GET movies/_search
	 * {
	 *   "query": {
	 *     "bool": {
	 *       "filter": [
	 *         {
	 *           "term": {
	 *             "title": "beautiful"
	 *           }
	 *         },
	 *         {
	 *           "range": {
	 *             "year": {
	 *               "gte": 2016,
	 *               "lte": 2018
	 *             }
	 *           }
	 *         }
	 *       ]
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/22 20:00 * /
	@Test
	public void boolFilterTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(boolQuery().filter(termQuery("title"."beautiful")).must(rangeQuery("year").gte(2016).lte(2018))) .build(); List<Movies> movies = elasticsearchTemplate.queryForList(searchQuery, Movies.class); System.out.println(JSON.toJSONString(movies)); }}Copy the code

3.3 Aggregate Query

/ aggregation query example * * * * * PUT the employee / _bulk * {" index ": {" _id" : 1}} * {"id":1,"name":"Bob","job":"java","age":21,"sal":8000,"gender":"female"} * {"index":{"_id":2}} * {"id":2,"name":"Rod","job":"html","age":31,"sal":18000,"gender":"female"} * {"index":{"_id":3}} * {"id":3,"name":"Gaving","job":"java","age":24,"sal":12000,"gender":"male"} * {"index":{"_id":4}} * {"id":4,"name":"King","job":"dba","age":26,"sal":15000,"gender":"female"} * {"index":{"_id":5}} * {"id":5,"name":"Jonhson","job":"dba","age":29,"sal":16000,"gender":"male"} * {"index":{"_id":6}} * {"id":6,"name":"Douge","job":"java","age":41,"sal":20000,"gender":"female"} * {"index":{"_id":7}} * {"id":7,"name":"cutting","job":"dba","age":27,"sal":7000,"gender":"male"} * {"index":{"_id":8}} * {"id":8,"name":"Bona","job":"html","age":22,"sal":14000,"gender":"female"} * {"index":{"_id":9}} * {"id":9,"name":"Shyon","job":"dba","age":20,"sal":19000,"gender":"female"} * {"index":{"_id":10}} * {"id":10,"name":"James","job":"html","age":18,"sal":22000,"gender":"male"} * {"index":{"_id":11}} * {"id":11,"name":"Golsling","job":"java","age":32,"sal":23000,"gender":"female"} * {"index":{"_id":12}} * {"id":12,"name":"Lily","job":"java","age":24,"sal":2000,"gender":"male"} * {"index":{"_id":13}} * {"id":13,"name":"Jack","job":"html","age":23,"sal":3000,"gender":"female"} * {"index":{"_id":14}} * {"id":14,"name":"Rose","job":"java","age":36,"sal":6000,"gender":"female"} * {"index":{"_id":15}} * {"id":15,"name":"Will","job":"dba","age":38,"sal":4500,"gender":"male"} * {"index":{"_id":16}} * {"id":16,"name":"smith","job":"java","age":32,"sal":23000,"gender":"male"} * *@author liuqiuyi
 * @date2021/4/22 and * /
public class AggregationQueryTest extends UtilsApplicationTests {
	@Resource
	ElasticsearchTemplate elasticsearchTemplate;


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "other_info": {
	 *       "sum": {
	 *         "field": "sal"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date 2021/4/22 20:41
	 */
	@Test
	public void sumTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.sum("other_info").field("sal"))
				.build();

		Double sumResult = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			// TODO will return the specific data, so far no solution has been found
			InternalSum internalSum = searchResponse.getAggregations().get("other_info");
			return internalSum.value();
		});

		System.out.println(sumResult);
	}


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "other_aggs_info": {
	 *       "avg": {
	 *         "field": "sal"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/23 choicest * /
	@Test
	public void avgTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.avg("other_aggs_info").field("sal"))
				.build();

		Double sumResult = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			// TODO will return the specific data, so far no solution has been found
			InternalAvg avg = searchResponse.getAggregations().get("other_aggs_info");
			return avg.value();
		});

		System.out.println(sumResult);
	}


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "job_count": {
	 *       "cardinality": {
	 *         "field": "job"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/23 at * /
	@Test
	public void cardinalityTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.cardinality("job_count").field("job"))
				.build();

		Double sumResult = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			// TODO will return the specific data, so far no solution has been found
			InternalCardinality jobCount = searchResponse.getAggregations().get("job_count");
			return jobCount.value();
		});

		System.out.println(sumResult);
	}


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "max_result": {
	 *       "max": {
	 *         "field": "sal"
	 *       }
	 *     },
	 *     "min_result": {
	 *       "min": {
	 *         "field": "sal"
	 *       }
	 *     },
	 *     "avg_result": {
	 *       "avg": {
	 *         "field": "sal"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/23 all * /
	@Test
	public void multivaluedTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.max("max_result").field("sal"))
				.addAggregation(AggregationBuilders.min("min_result").field("sal"))
				.addAggregation(AggregationBuilders.avg("avg_result").field("sal"))
				.build();

		Map<String, Double> resultMap = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			Map<String, Double> stringDoubleMap = Maps.newHashMap();

			InternalMax maxResult = searchResponse.getAggregations().get("max_result");
			InternalMin minResult = searchResponse.getAggregations().get("min_result");
			InternalAvg avgResult = searchResponse.getAggregations().get("avg_result");

			stringDoubleMap.put("max_result", maxResult.value());
			stringDoubleMap.put("min_result", minResult.value());
			stringDoubleMap.put("avg_result", avgResult.value());
			return stringDoubleMap;
		});

		System.out.println(resultMap);
	}


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "sal_info": {
	 *       "stats": {
	 *         "field": "sal"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/25 19:46 * /
	@Test
	public void statsTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.stats("sal_info").field("sal"))
				.build();

		Map<String, Object> resultMap = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			Map<String, Object> stringDoubleMap = Maps.newHashMap();

			InternalStats stats = searchResponse.getAggregations().get("sal_info");

			stringDoubleMap.put("count", stats.getCount());
			stringDoubleMap.put("min", stats.getMin());
			stringDoubleMap.put("max", stats.getMax());
			stringDoubleMap.put("avg", stats.getAvg());
			stringDoubleMap.put("sum", stats.getSum());

			return stringDoubleMap;
		});

		System.out.println(resultMap);
	}



	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "job_count": {
	 *       "terms": {
	 *         "field": "job"
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/25 prepare * /
	@Test
	public void termsTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.terms("job_count").field("job"))
				.build();

		Map<Object, Object> resultMap = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			Map<Object, Object> objectObjectMap = Maps.newHashMap();

			Terms terms = searchResponse.getAggregations().get("job_count");
			for (Terms.Bucket bt : terms.getBuckets()) {
				Object key = bt.getKey();
				long docCount = bt.getDocCount();
				objectObjectMap.put(key, docCount);
			}
			return objectObjectMap;
		});

		System.out.println(resultMap);
	}



	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "job_info": {
	 *       "terms": {
	 *         "field": "job"
	 *       },
	 *       "aggs": {
	 *         "sal_info": {
	 *           "stats": {
	 *             "field": "sal"
	 *           }
	 *         }
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/25 joined * /
	@Test
	public void termsAndStatsTest(a){
		TermsAggregationBuilder jobInfoTerm = AggregationBuilders.terms("job_info").field("job");
		StatsAggregationBuilder subTerm = AggregationBuilders.stats("sal_info").field("sal");
		jobInfoTerm.subAggregation(subTerm);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(jobInfoTerm)
				.build();

		Map<Object, Object> resultMap = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			Map<Object, Object> objectObjectMap = Maps.newHashMap();

			Terms terms = searchResponse.getAggregations().get("job_info");
			for (Terms.Bucket bt : terms.getBuckets()) {
				Object key = bt.getKey();
				long docCount = bt.getDocCount();
				objectObjectMap.put(key, docCount);
				// Get the subset
				InternalStats aggregations = bt.getAggregations().get("sal_info");
				// Set the value as required
				long count = aggregations.getCount();
			}
			return objectObjectMap;
		});

		System.out.println(resultMap);
	}



	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "job_info": {
	 *       "terms": {
	 *         "field": "job"
	 *       },
	 *       "aggs": {
	 *         "gender_info": {
	 *           "terms": {
	 *             "field": "gender"
	 *           },
	 *           "aggs": {
	 *             "sal_info": {
	 *               "stats": {
	 *                 "field": "sal"
	 *               }
	 *             }
	 *           }
	 *         }
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/25 20:51 * /
	@Test
	public void multiAggsTest(a){
		TermsAggregationBuilder jobInfoTerm = AggregationBuilders.terms("job_info").field("job");
		TermsAggregationBuilder genderInfoTerm = AggregationBuilders.terms("gender_info").field("gender");
		StatsAggregationBuilder subTerm = AggregationBuilders.stats("sal_info").field("sal");

		genderInfoTerm.subAggregation(subTerm);
		jobInfoTerm.subAggregation(genderInfoTerm);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(jobInfoTerm)
				.build();

		Map<Object, Object> resultMap = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			Map<Object, Object> objectObjectMap = Maps.newHashMap();

			Terms terms = searchResponse.getAggregations().get("job_info");
			for (Terms.Bucket bt : terms.getBuckets()) {
				Object key = bt.getKey();
				long docCount = bt.getDocCount();
				objectObjectMap.put(key, docCount);
				// Get the subset
				Terms subTerms = bt.getAggregations().get("gender_info");

				for (Terms.Bucket subTermsBucket : subTerms.getBuckets()) {
					Object subKey = subTermsBucket.getKey();
					long subDocCount = subTermsBucket.getDocCount();

					// Get the innermost data
					InternalStats aggregations = subTermsBucket.getAggregations().get("sal_info");
					// Set the value as required

					// Print results for testing purposes
					System.out.println(subKey + "_" + subDocCount + "_"+ JSON.toJSONString(aggregations)); }}return objectObjectMap;
		});

		System.out.println(resultMap);
	}



	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "top_age_2": {
	 *       "top_hits": {
	 *         "size": 2,
	 *         "sort": [
	 *           {
	 *             "age": {
	 *               "order": "desc"
	 *             }
	 *           }
	 *         ]
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/26 their * /
	@Test
	public void TopHitsTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.topHits("top_age_2").size(2))
				.withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC))
				.build();


		List<Employee> resultList = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			List<Employee> employeeList = Lists.newArrayList();

			InternalTopHits hits = searchResponse.getAggregations().get("top_age_2");

			SearchHit[] subHits = hits.getHits().getHits();
			for (SearchHit subHit : subHits) {
				// TODO uses JSONString to Object
				String sourceAsString = subHit.getSourceAsString();
				Employee employee = JSON.parseObject(sourceAsString, Employee.class);

				employeeList.add(employee);
			}
			return employeeList;
		});

		System.out.println(resultList);
	}



	/** * GET employee/_search * { * "size": 0, * "aggs": { * "sal_info": { * "range": { * "field": "sal", * "ranges": [ * { * "key": "0 <= sal <= 5000", * "from": 0, * "to": 5000 * }, * { * "key": "5001 <= sal <= 10000", * "from": 5001, * "to": 10000 * }, * { * "key": "10001 <= sal <= 15000", * "from": 10001, * "to": 15000 *} *] *} *} *} *} * * *@author liuqiuyi
	 * @date2021/4/27 20:00 * /
	@Test
	public void rangeTest(a){
		RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("sal_info").field("sal")
				.addRange("0 <= sal <= 5000".0d.5000d)
				.addRange("5001 <= sal <= 10000".5001d.10000d)
				.addRange("10001 <= sal <= 15000".10001d.15000d);

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(rangeAggregationBuilder)
				.build();

		List<Long> resultList = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			List<Long> docCountList = Lists.newArrayList();
			Range range = searchResponse.getAggregations().get("sal_info");
			for (Range.Bucket bucket : range.getBuckets()) {
				long docCount = bucket.getDocCount();
				docCountList.add(docCount);
			}
			return docCountList;
		});
		System.out.println(JSON.toJSONString(resultList));
	}


	/**
	 * GET employee/_search
	 * {
	 *   "size": 0,
	 *   "aggs": {
	 *     "sal_info": {
	 *       "histogram": {
	 *         "field": "sal",
	 *         "interval": 5000,
	 *         "extended_bounds": {
	 *           "min": 0,
	 *           "max": 30000
	 *         }
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * @author liuqiuyi
	 * @date2021/4/27 their * /
	@Test
	public void histogramTest(a){
		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("employee").withTypes("_doc")
				.addAggregation(AggregationBuilders.histogram("sal_info").field("sal").interval(5000d).extendedBounds(0d.30000d))
				.build();

		List<Long> resultList = elasticsearchTemplate.query(searchQuery, searchResponse -> {
			List<Long> docCountList = Lists.newArrayList();
			Histogram histogram = searchResponse.getAggregations().get("sal_info");
			for (Histogram.Bucket bucket : histogram.getBuckets()) {
				long docCount = bucket.getDocCount();
				docCountList.add(docCount);
			}
			returndocCountList; }); System.out.println(JSON.toJSONString(resultList)); }}Copy the code

3.4 Other Queries

/** * other query statements **@author liuqiuyi
 * @date2021/4/28 19:56 * /
public class OtherQueryTest extends UtilsApplicationTests {
	@Resource
	ElasticsearchTemplate elasticsearchTemplate;

	/ * * * * * GET recommendation movies / _search * {* "suggest" : {* "title_suggestion" : {* "text" : "drema," * "term" : {* "field" : "title", * "suggest_mode": "popular" * } * } * } * } * *@author liuqiuyi
	 * @date2021/4/27 all * /
	@Test
	public void recommendationTest(a){
		// Build the recommendation search criteria
		TermSuggestionBuilder suggestionBuilder = SuggestBuilders.termSuggestion("title")
				.suggestMode(TermSuggestionBuilder.SuggestMode.POPULAR).text("drema");

		SearchResponse searchResponse = elasticsearchTemplate.suggest(new SuggestBuilder().addSuggestion("title_suggestion", suggestionBuilder), Movies.class);
		/ / value
		TermSuggestion termSuggestion = searchResponse.getSuggest().getSuggestion("title_suggestion");
		TermSuggestion.Entry options = termSuggestion.getEntries().get(0);
		options.forEach(e -> {
			Text text = e.getText();
			float score = e.getScore();
			int freq = e.getFreq();

			System.out.println(text.toString() + "-" + score + "-" + freq);
		});
	}


	/** * automatic completion ** PUT articles * {* "mappings": {* "properties": {* "title_completion":{// "Completion" / / * type * * * *}}}} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * POST articles / _bulk * * {" index ": {}} {"title_completion":"liuqiuyi"} * {"index":{}} * {"title_completion":"ls liuqiuyi"} * {"index":{}} * {"title_completion":"this is a liuqiuyi"} * --------------------------- * POST articles/_search * { * "size": 0, * "suggest": { * "articles_suggest": { * "prefix": "l", * "completion": { * "field": "title_completion" * } * } * } * } * *@author liuqiuyi
	 * @date2021/4/28 * / shall lie
	@Test
	public void automaticCompletionTest(a){
		CompletionSuggestionBuilder suggestionBuilder = SuggestBuilders.completionSuggestion("title_completion").prefix("l");

		SearchResponse searchResponse = elasticsearchTemplate.suggest(new SuggestBuilder().addSuggestion("articles_suggest", suggestionBuilder), "articles");

		/ / value
		CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("articles_suggest");
		List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getOptions();
		for(CompletionSuggestion.Entry.Option option : options) { SearchHit hit = option.getHit(); String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); }}/ * * * * * GET highlighted movies / _search * {* "query" : {* "match" : {} * "title" : "beautiful" * *}, * "highlight" : { * "post_tags": "</span>", * "pre_tags": "<span color='red'>", * "fields": { * "title": {} * } * } * } * *@author liuqiuyi
	 * @date2021/4/28 20:22 * /
	@Test
	public void highlightTest(a){
		HighlightBuilder highlightBuilder = new HighlightBuilder().postTags("</span>").preTags("<span color='red'>").field("title");

		NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withIndices("movies").withTypes("_doc")
				.withQuery(matchQuery("title"."beautiful"))
				.withHighlightBuilder(highlightBuilder)
				.build();

		elasticsearchTemplate.query(searchQuery, searchResponse -> {
			SearchHits hits = searchResponse.getHits();
			SearchHit[] resultHits = hits.getHits();
			for (SearchHit resultHit : resultHits) {
				Map<String, HighlightField> highlightFields = resultHit.getHighlightFields();
				for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
					HighlightField value = entry.getValue();
					Text[] fragments = value.getFragments();
					// Set the value based on service requirements
					System.out.println(entry.getKey() + "-" + fragments[0]); }}return null; }); }}Copy the code

Fourth, the appendix

4.1 Import Data using LogStash

  • Download the Logstash website and unzip it
  • In the config directory of the logstash file, create a logstash. Conf file with the following contents:
Input {file {# data file directory path = > "/ Users/liuqiuyi/Desktop/myWorkSpace/utils/docker/logstash - 7.1.0 / CVS/movies. CSV" Start_position => "beginning" In the second import sincedb_path = > "/ Users/liuqiuyi/Desktop/myWorkSpace/utils/docker/logstash - 7.1.0 / logs/db_path log" filter {}}  csv { separator => "," columns => ["id","content","genre"] } mutate { split => { "genre" => "|" } remove_field => ["path", "host","@timestamp","message"] } mutate { split => ["content", "("] add_field => { "title" => "%{[content][0]}"} add_field => { "year" => "%{[content][1]}"} } mutate { convert => { "year" => "integer" } strip => ["title"] remove_field => ["path", "Host ","@timestamp","message","content"]}} output {elasticSearch {# specify es address hosts => "http://localhost:9200" # Specify index => "movies" document_id => "%{id}"} stdout {}}Copy the code
  • To start the import, run the following command
./bin/logstash -f ./config/logstash.conf
Copy the code
  • Movies file download link: pan.baidu.com/s/1Fp0XCNFA… Password: p40r