关于用scala 写Elasticsearch +springboot+jpa
最近在项目中用到这个 框架结构,用起来特别方便,分享下
引包 build.sbt
"org.springframework.boot" % "spring-boot-starter-data-elasticsearch" %verSpringBoot,
配置,我的是springcloud,配置文件配置在对应文件中就行了,
spring:
data:
elasticsearch:
cluster-name: xxxxx
cluster-nodes: xxx:9300
cluster-nodes: xxx:9300
repositories:
enabled: true
@Document( indexName = "xxx", `type` = "xxxx" )
@Mapping( mappingPath = "jsons/userjson.json" )
@JsonIdentityInfo( generator = classOf[JSOGGenerator] )
class UserEntity() {
@BeanProperty
var id:Long=_
@BeanProperty
var uid:String=_
}
映射文件userjson.json
{
"article": {
"properties": {
"id": {
"type": "long"
},
"uid": {
"type": "keyword"
,"index":"false"
}
}
}
}
注,因为springboot不是很兼容elasticsearch 框架,在elasticsearch库中,需要put
我配置的是kibana
PUT /xxx
{
"mappings": {
"xxx": {
"properties": {
"id": {
"type": "long"
},
"uid": {
"type": "keyword"
}
}
}
}
}
下面写jpa
@Repository
trait UserDao extends CrudRepository[UserEntity,java.lang.Long] with ElasticsearchRepository[UserEntity,java.lang.Long]{
//写jpa的规则,就不介绍了,文章最后有个连接,可以借鉴下,这不本文的重点
}
注,因为在有的地方elasticsearch 并不兼容于scala的类型所以在映射的时候尽量用java类型,在其他地方做个隐式转换就好了
隐式转换
import scala.collection.JavaConverters._
jpa关键字介绍下
关键字
使用示例
等同于的ES查询
关键字 | 示例 | es查询 |
---|---|---|
And | findByNameAndPrice | {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Or | findByNameOrPrice | {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Is | findByName | {“bool” : {“must” : {“field” : {“name” : “?”}}}} |
Not | findByNameNot | {“bool” : {“must_not” : {“field” : {“name” : “?”}}}} |
Between | findByPriceBetween | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
LessThanEqual | findByPriceLessThan | {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
GreaterThanEqual | findByPriceGreaterThan | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
Before | findByPriceBefore | {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
After | findByPriceAfter | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
Like | findByNameLike | {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}} |
StartingWith | findByNameStartingWith | {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}} |
EndingWith | findByNameEndingWith | {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}} |
Contains/Containing | findByNameContaining | {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}} |
In | findByNameIn(Collectionnames) | {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}} |
NotIn | findByNameNotIn(Collectionnames) | {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}} |
True | findByAvailableTrue | {“bool” : {“must” : {“field” : {“available” : true}}}} |
False | findByAvailableFalse | {“bool” : {“must” : {“field” : {“available” : false}}}} |
OrderBy | findByAvailableTrueOrderByNameDesc | {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}} |
关于jpa的规则,有比较全的介绍博客,推荐下
https://blog.youkuaiyun.com/KingBoyWorld/article/details/78654820
初学者可以对比学习!
仅自勉,履霜坚冰至!