package com.woniu.springsecurityday01.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
/**
* 你也可以不继承 AbstractElasticsearchConfiguration 类,而将 ESConfig 写成一般的配置类的型式。
* 不过继承 AbstractElasticsearchConfiguration 好处在于,它已经帮我们配置好了 elasticsearchTemplate 直接使用。
*/
@Configuration
public class ESConfig extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
ES.java
package com.woniu.springsecurityday01.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "test")
@AllArgsConstructor
@NoArgsConstructor
public class ES {
@Id
private Integer id;
@Field(type = FieldType.Keyword)
private String bookName;
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Keyword)
private String author;
@Field(type = FieldType.Float)
private Double price;
@Field(type = FieldType.Text)
private String description;
}
BookRepository.java
package com.woniu.springsecurityday01.dao;
import com.woniu.springsecurityday01.domain.ES;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<ES,Integer> {
/**
* 根据描述查找
* @return
*/
List<ES> findESByDescription(String desc, Pageable varl);
/**
* 根据作者和描述和标题
*/
List<ES> queryESByAuthorAndDescriptionAndTitle(String author,String desc,String title);
List<ES> findESByPriceBetween(Double price1,Double price2);
}
Springsecurityday01ApplicationTests.java
package com.woniu.springsecurityday01;
import com.alibaba.fastjson.JSON;
import com.woniu.springsecurityday01.dao.BookRepository;
import com.woniu.springsecurityday01.dao.MenuDao;
import com.woniu.springsecurityday01.dao.UserDao;
import com.woniu.springsecurityday01.domain.ES;
import com.woniu.springsecurityday01.domain.Menus;
import com.woniu.springsecurityday01.domain.Shoppingcar;
import com.woniu.springsecurityday01.domain.Users;
import com.woniu.springsecurityday01.domain.vo.MenusTreeVo;
import com.woniu.springsecurityday01.service.ShoppingcarService;
import com.woniu.springsecurityday01.util.Count;
import com.woniu.springsecurityday01.util.ResponseResult;
import org.checkerframework.checker.units.qual.A;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class Springsecurityday01ApplicationTests {
@Autowired
private BookRepository bookRepository;
@Autowired
private ElasticsearchRestTemplate restTemplate;
@Test
void createIndex(){
System.out.println("索引创建成功");
}
@Test
void createDoc(){
List<ES> esList=new ArrayList<>();
ES es= new ES(1001,"按预定","按预定","按预定",50.0,"按预定");
ES es1= new ES(1002,"按预定2","按预定2","按预定2",50.0,"按预定2");
esList.add(es);
esList.add(es1);
//新增和修改都是save方法
//删除用delete
bookRepository.saveAll(esList);
}
@Test
void query(){
//查询
// Optional byId = bookRepository.findById(1001);
// ES es=byId.get();
// System.out.println(es);
Sort sort=Sort.by(Sort.Order.desc("id"));
//排序查
Iterable<ES> all = bookRepository.findAll(sort);
for (ES es1:all) {
System.out.println(es1);
}
}
@Test//分页查
void queryPage(){
Sort sort=Sort.by(Sort.Order.desc("id"));
//es的分页的pagenum从0开始
Pageable pageable=PageRequest.of(0,2,sort);
Iterable<ES> all = bookRepository.findAll(pageable);
for (ES es1:all) {
System.out.println(es1);
}
}
@Test//自定义查询
void queryCondition1(){
Pageable pageable=PageRequest.of(0,2);
List<ES> es = bookRepository.findESByDescription("预定2",pageable);
for (ES es1:es) {
System.out.println(es1);
}
}
@Test//自定义查询
void queryCondition2() {
List<ES> es = bookRepository.queryESByAuthorAndDescriptionAndTitle("按预定2", "按预定2", "按预定2");
for (ES es1:es) {
System.out.println(es1);
}
}
@Test//自定义查询
void queryCondition3() {
List<ES> es=bookRepository.findESByPriceBetween(40.0,80.0);
for (ES es1:es) {
System.out.println(es1);
}
}
@Test//分词条件变成or
void nativeQuery(){
NativeSearchQuery nativeSearchQuery=
new NativeSearchQueryBuilder()
//.withQuery(QueryBuilders.queryStringQuery("预定2").defaultField("description"))//查询条件
//boolQuery()多条件查询
//must类似and
//should 类似or
.withQuery(QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("预定2").defaultField("description"))
.should(QueryBuilders.queryStringQuery("预定2").defaultField("title")))
.withPageable(PageRequest.of(0,2))
.build();
SearchHits<ES> search = restTemplate.search(nativeSearchQuery, ES.class);
List<SearchHit<ES>> searchHits = search.toList();
for (SearchHit<ES> searchHit:searchHits) {
System.out.println(searchHit.getContent());
}
}
}