ElasticSearch 和kinana
Springboot: 2.3.1
ElasticSearch: 7.7.1
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
##端口号
server.port=8888
##es地址
spring.elasticsearch.rest.uris=127.0.0.1:9200
@Document(indexName = "product")
@Data
@ToString
public class Product {
private long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String name;
private BigDecimal price;
private String img;
private String describe;
}
id必须有,否则后面用spring-data-elasticsearch操作会报错
核心接口: ElasticsearchRepository
大致结构图
我们操作 ElasticSearch 只需要继承 ElasticsearchRepository接口就可以了
其中Repository核心方法由这些:
其他类相关方法可以自己去官网查看,这里不再重述
springboot
我们定义一个ProductMapper
public interface ProductMapper extends ElasticsearchRepository<Product, Long> {
/**
* 通过名字查询
* @param name
* @return
*/
List<Product> findByName(String name);
/**
* 通过名字分页查询
* @param name
* @param pageable
* @return
*/
Page<Product> findByName(String name, Pageable pageable);
/**
* 通过名字排序查询
* @param name
* @param sort
* @return
*/
List<Product> findByName(String name, Sort sort);
}
内置在Spring的查询构造器会对findBy 这些关键词进行解析然后生成相应的方法,有点类似jpa的操作方式,我们来测试一下
这些都是ElasticsearchRepository接口自带的方法
@SpringBootTest
class DemoApplicationTests {
@Autowired
ProductMapper productMapper;
@Autowired
ElasticsearchRestTemplate template;
@Test
void insert() {
Product product = new Product();
product.setId(3);
product.setImg("图片三");
product.setName("huawei");
product.setDescribe("好好看的手机呢");
product.setPrice(new BigDecimal("3999.3"));
productMapper.save(product);
}
/**
* 批量插入
*/
@Test
void inserts() {
Product product = new Product();
product.setImg("图片一");
product.setName("iphone");
product.setDescribe("打电话娱乐用的");
product.setPrice(new BigDecimal("5666.3"));
List<Product> list = new ArrayList();
list.add(product);
productMapper.saveAll(list);
}
/**
* 更新
*/
@Test
void update() {
Optional<Product> product = productMapper.findById(0l);
Product product1 = product.orElseThrow( RuntimeException::new);
productMapper.save(product1);
}
/**
* 删除
*/
@Test
void delete() {
productMapper.deleteById(0l);
}
}
下面我们来看看通过方法名解析的方法操作
@SpringBootTest
class ProductMapperTest {
@Autowired
ProductMapper productMapper;
@Autowired
ElasticsearchOperations elasticsearchOperations;
@Test
void findByName() {
List<Product> list = productMapper.findByName("iphone");
list.forEach(s -> System.out.println(s));
}
@Test
void findByDescribe() {
List<Product> list = productMapper.findByDescribe("打电话");
list.forEach(s -> System.out.println(s));
}
/**
* 分页
*/
@Test
void findByNmaePage() {
Page<Product> list = productMapper.findByName("iphone", PageRequest.of(0, 20));
list.get().forEach(s -> System.out.println(s));
}
/**
* 排序 通过价格排序
*/
@Test
void findBYNameDesc() {
Sort.TypedSort<Product> product = Sort.sort(Product.class);
/**
* 也可以使用这种写法
* Sort sort = Sort.by("price").ascending()
* .and(Sort.by("name").descending());
*/
List<Product> list = productMapper.findByName("iphone", product.by(Product::getPrice).descending());
list.forEach(s -> System.out.println(s));
}
/**
* 复杂一点的查询
*/
@Aut
@Test
void testQuery() {
String name = "iphone";
String price = "6666";
String describe = "电话";
BoolQueryBuilder builder = QueryBuilders.boolQuery();
BoolQueryBuilder nameMatch = QueryBuilders.boolQuery().minimumShouldMatch(1)
.should(QueryBuilders.matchQuery("name",name))
.should(QueryBuilders.matchQuery("product",price));
BoolQueryBuilder must = QueryBuilders.boolQuery().minimumShouldMatch(2)
.should(nameMatch)
.should(QueryBuilders.matchQuery("price",price))
.should(QueryBuilders.matchQuery("describe",describe).boost(5));
builder.must(must);
NativeSearchQuery query = new NativeSearchQuery(builder);
PageRequest request = PageRequest.of(0,1);
query.setPageable(request);
List<Product> list = elasticsearchOperations.queryForList(query,Product.class, IndexCoordinates.of("product"));
list.forEach(s -> System.out.println(s));
}
}
更多操作慢慢研究吧,官网好像API也不算太多,相关资料也不多
下载