一、导入elasticsearch依赖
其他依赖自行导入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
二、在相对应的启动类添加elasticsearch开启注解
@Import({Knife4jConfiguration.class}) //Swagger配置文件
@EnableElasticsearchRepositories
@SpringCloudApplication
public class SearchApplication {
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class,args);
}
}
三、创建对应的实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "person")
public class Person {
@Id
private String id;
@Field(value = "name",type = FieldType.Text)
private String name;
@Field(value = "address",type = FieldType.Text)
private String address;
///usr/share/elasticsearch/plugins
}
四、实现elasticsearch相关业务接口
@Repository
//CrudRepository不能进行分页,分页可继承PagingAndSortingRepository
public interface PersonRepositories extends CrudRepository<Person,String> {
}
五、测试
@SpringBootTest
public class EsTest {
@Autowired
ElasticsearchRestTemplate restTemplate;
@Autowired
PersonRepositories personRepositories;
/**
* 创建索引并插入数据数据
*/
@Test
public void test5(){
Person person=new Person();
person.setId("5");
person.setName("张三");
person.setAddress("xxxxx");
personRepositories.save(new Person());
log.info("操作成功");
}
/**
* 获取数据
*/
@Test
public void test4(){
/**
* Optional<T> findById(ID var1); 根据id查询数据
*
* boolean existsById(ID var1); 判断对应id是否存在
*
* Iterable<T> findAll(); 查询所有数据
*/
Iterable<Person> people = personRepositories.findAll();
people.forEach(item->{
System.out.println(item);
});
log.info("操作成功");
}
/**
* 修改数据
*/
@Test
public void test3(){
Person person = personRepositories.findById("5").get();
person.setName("小明");
personRepositories.save(person); //save id存在就修改,不存在就添加
log.info("操作成功");
}
/**
* 删除数据
*/
@Test
public void test2(){
/**
* void deleteById(ID var1); //根据id删除
*
* void delete(T var1); //删除整个对应实体
*
* void deleteAll(Iterable<? extends T> var1); //批量删除
*
* void deleteAll(); //删除所有
*/
personRepositories.deleteById("5");
log.info("操作成功");
}
/**
* 自定义方法
*/
@Test
public void test1(){
/**
* Spring Data 的另一个强大功能,是根据方法名称自动实现功能。
* 比如:你的方法名叫做:findByTitle,那么它就知道你是根据title查询,
* 然后自动帮你完成,无需写实现类。
* 当然,方法名称要符合一定的约定:
*
*/
}
}
官方命名模板文章来源:https://www.toymoban.com/news/detail-696605.html
Keyword | Sample | Elasticsearch Query String |
---|---|---|
And | findByNameAndPrice | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } }, { “query_string” : { “query” : “?”, “fields” : [ “price” ] } } ] } }} |
Or | findByNameOrPrice | { “query” : { “bool” : { “should” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } }, { “query_string” : { “query” : “?”, “fields” : [ “price” ] } } ] } }} |
Is | findByName | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } } ] } }} |
Not | findByNameNot | { “query” : { “bool” : { “must_not” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } } ] } }} |
Between | findByPriceBetween | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }} |
LessThan | findByPriceLessThan | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : false } } } ] } }} |
LessThanEqual | findByPriceLessThanEqual | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }} |
GreaterThan | findByPriceGreaterThan | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : false, “include_upper” : true } } } ] } }} |
GreaterThanEqual | findByPriceGreaterThan | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : true, “include_upper” : true } } } ] } }} |
Before | findByPriceBefore | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }} |
After | findByPriceAfter | { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : true, “include_upper” : true } } } ] } }} |
Like | findByNameLike | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?*”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }} |
StartingWith | findByNameStartingWith | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?*”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }} |
EndingWith | findByNameEndingWith | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “*?”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }} |
Contains/Containing | findByNameContaining | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }} |
In (when annotated as FieldType.Keyword) | findByNameIn(Collectionnames) | { “query” : { “bool” : { “must” : [ {“bool” : {“must” : [ {“terms” : {“name” : [“?”,“?”]}} ] } } ] } }} |
In | findByNameIn(Collectionnames) | { “query”: {“bool”: {“must”: [{“query_string”:{“query”: “”?" “?”", “fields”: [“name”]}}]}}} |
NotIn (when annotated as FieldType.Keyword) | findByNameNotIn(Collectionnames) | { “query” : { “bool” : { “must” : [ {“bool” : {“must_not” : [ {“terms” : {“name” : [“?”,“?”]}} ] } } ] } }} |
NotIn | findByNameNotIn(Collectionnames) | {“query”: {“bool”: {“must”: [{“query_string”: {“query”: “NOT(”?" “?”)", “fields”: [“name”]}}]}}} |
True | findByAvailableTrue | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “true”, “fields” : [ “available” ] } } ] } }} |
False | findByAvailableFalse | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “false”, “fields” : [ “available” ] } } ] } }} |
OrderBy | findByAvailableTrueOrderByNameDesc | { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “true”, “fields” : [ “available” ] } } ] } }, “sort”:[{“name”:{“order”:“desc”}}] } |
Exists | findByNameExists | {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}} |
IsNull | findByNameIsNull | {“query”:{“bool”:{“must_not”:[{“exists”:{“field”:“name”}}]}}} |
IsNotNull | findByNameIsNotNull | {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}} |
IsEmpty | findByNameIsEmpty | {“query”:{“bool”:{“must”:[{“bool”:{“must”:[{“exists”:{“field”:“name”}}],“must_not”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}]}}} |
IsNotEmpty | findByNameIsNotEmpty | {“query”:{“bool”:{“must”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}} |
官方连接地址:https://docs.spring.io/spring-data/elasticsearch/docs/4.4.6/reference/html/#elasticsearch.repositories文章来源地址https://www.toymoban.com/news/detail-696605.html
到了这里,关于spring cloud如何集成elasticsearch的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!