目录
版本选择
引入依赖
yml配置
创建实体
实现ElasticsearchRepository
测试
使用ElasticsearchRestTemplate
版本选择
Elasticsearch 7.17.3 对应依赖 Spring Data服务器托管网 Elasticsearch 4.4.x,对应springboot版本2.7.x
引入依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
yml配置
spring:
elasticsearch:
uris: http://localhost:9200
connection-timeout: 3s
创建实体
@Data
@AllArgsConstructor
@Document(indexName = "employees")
public class Employee {
@Id
private Long id;
@Field(type= FieldType.Keyword)
private String name;
private int sex;
private int age;
@Field(type= FieldType.Text,analyzer="ik_max_word")
private String address;
private String remark;
}
实现ElasticsearchRepository
该接口是框架封装的用于操作Elastsearch的高级接口
@Repository
public interface EmployeeRepository extends ElasticsearchRepository {
List findByName(String name);
}
测试
@Autowired
EmployeeRepository employeeRepository;
@Test
public void testDocument(){
Employee employee = new Employee(1L,"666",1,18,"北京","java");
//插入文档
employeeRepository.save(employee);
//根据id查询
Optional result = employeeRepository.findById(1L);
log.info(String.valueOf(result.get()));
//根据name查询
List list = employeeRepository.findByName("666");
log.info(String.valueOf(list.get(0)));
}
使用ElasticsearchRestTemplate
ElasticsearchRestTemplate模板类,封装了便捷操作Elasticsearch的模板方法,包括索引 / 映射 / CRUD 等底层操作和高级操作。
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void testCreateIndex(){
//创建索引
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("employee_index"));
if (indexOperations.exists()) {
log.info("索引已经存在");
}else {
//创建索引
indexOperations.create();
}
}
@Test
public void testDeleteIndex(){
//删除索引
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("employee_index"));
indexOperations.delete();
}
文档操作
@Test
public void testQueryDocument(){
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//查询
builder.withQuery(QueryBuilders.matchQuery("address","公园"));
// 设置分页信息
builder.withPageable(PageRequest.of(0, 5));
// 设置排序
builder.withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC));
SearchHits search = elasticsearchRestTemplate.search(builder.build(), Employee.class);
List> searchHits = search.getSearchHits();
for (SearchHit hit: searchHits){
log.info("返回结果:"+hit.toString());
}
}
@Test
public void testInsertBatch(){
List employees = new ArrayList();
employees.add(new Employee("2","张三",1,25,"北京","java"));
employees.add(new Employee("3","李四",1,28,"山西","pytion"));
employees.add(new Employee("4","小红",0,26,"天津","php"));
List queries = new ArrayList();
for (Employee employee : employees) {
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId(employee.getId());
String json = JSONObject.toJSONString(employee);
indexQuery.setSource(json);
queries.add(indexQuery);
}
//bulk批量插入
elasticsearchRestTemplate.bulkIndex(queries,Employee.class);
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
Avalonia是一个跨平台的.NET UI框架,它允许开发者使用C#和XAML来创建丰富的桌面应用程序。在Avalonia中,Alignment、Margin和Padding是非常重要的布局属性,它们与Panel元素一起使用,可以构建出各种复杂的用户界面。 …