StringBoot + ElasticSearch
StringBoot(2.4.3)+ ElasticSearch(7.9.3)
ElasticsearchRepository
实体类
- @Document(必写)
| 属性名 | 说明 |
|---|---|
| indexName | 索引名,支持SpEl |
| shards | 分片 |
| replicas | 每个分区备份数 |
| refreshInterval | 刷新间隔,默认1s |
| indexStoreType | 索引文件存储类型,默认fs |
| versionType | 配置版本管理,默认VersionType.EXTERNAL |
@Id(必写)
@Field
| 属性名 | 说明 |
|---|---|
| name | 将在Elasticsearch文档中表示的字段名称,如果未设置,则使用Java字段名称 |
| type | 属性类型 |
| store | 标记是否原始字段值应存储在Elasticsearch中,默认值为false。 |
| analyzer | 分词 |
- demo
1 | @Data |
Repository接口
继承ElasticsearchRepository<T, ID>
ElasticsearchRepository接口中的方式都是@Deprecated
两种方式:关键字拼接查询条件、@Query注解查询
| 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(Collection |
{ “query” : { “bool” : { “must” : [ {“bool” : {“must” : [ {“terms” : {“name” : [“?”,”?”]}} ] } } ] } }} |
| In | findByNameIn(Collection |
{ “query”: {“bool”: {“must”: [{“query_string”:{“query”: “"?" "?"“, “fields”: [“name”]}}]}}} |
| NotIn (when annotated as FieldType.Keyword) | findByNameNotIn(Collection |
{ “query” : { “bool” : { “must” : [ {“bool” : {“must_not” : [ {“terms” : {“name” : [“?”,”?”]}} ] } } ] } }} |
| NotIn | findByNameNotIn(Collection |
{“query”: {“bool”: {“must”: [{“query_string”: {“query”: “NOT("?" "?")”, “fields”: [“name”]}}]}}} |
| Near | findByStoreNear | Not Supported Yet ! |
| 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”}}] } |
- demo
1 | public interface EsTestRepository extends ElasticsearchRepository<EsTestVo, Long> { |
ElasticsearchRestTemplate
主要有以下操作
IndexOperations定义索引级别的操作,如创建或删除索引。
DocumentOperations定义基于实体 ID 存储、更新和检索实体的操作。
SearchOperations定义使用查询搜索多个实体的操作
demo
1 | @PostMapping("/update") |
sql
1 | # yml |