Elasticsearch是⾯向⽂档(document oriented)的,这意味着它可以存储整个对象或⽂档(document)。然
⽽它不仅仅是存储,还会索引(index)每个⽂档的内容使之可以被搜索。在Elasticsearch中,你可以对⽂
档(⽽⾮成⾏成列的数据)进⾏索引、搜索、排序、过滤。
IK分词器下载地址https://github.com/medcl/elasticsearch-analysis-ik/releases
必须安装和elasticsearch版本一样的ik分词器,否侧会报错,容器闪退
需要聚合查询的字段要添加fielddata = true
复杂查询都使用NativeSearchQueryBuilder
实例代码
实体类@document(indexName = "car_index", type = "car")public class Car { @Id @Field(type = FieldType.Long, store = true) private Long id; @Field(type = FieldType.Text, store = true, analyzer = "ik_smart") private String name; @Field(type = FieldType.Text, store = true, analyzer = "ik_smart",fielddata = true) private String color; @Field(type = FieldType.Text, store = true, analyzer = "ik_smart",fielddata = true) private String brand; @Field(type = FieldType.Double, store = true) private Double price; 。。。}
daopublic interface CarDao extends ElasticsearchRepository
TEST代码@Testpublic void testQuerySelfAggs(){ //查询条件的构建器 NativeSearchQueryBuilder queryBuilder = newNativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()); //排除所有的字段查询, queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{},null)); //添加聚合条件 queryBuilder.addAggregation(AggregationBuilders.terms("group_by_color").field("color")); //执⾏查询,把查询结果直接转为聚合page AggregatedPage