Elasticsearch sql语句转为es查询条件

比如将select * from esIndex where lastTime = 1648540192929;
转化为es查询语句
{“from”:0,”size”:1000,”query”:{“bool”:{“filter”:[{“bool”:{“must”:[{“match_phrase”:{“lastTime “:{“query”:1648540192929,”slop”:0,”zero_terms_query”:”NONE”,”boost”:1.0}}}],”adjust_pure_negative”:true,”boost”:1.0}}],”adjust_pure_negative”:true,”boost”:1.0}}}

1、添加pom.xml依赖

<!– https://mvnrepository.com/artifact/org.nlpcn/elasticsearch-sql –>
<dependency>
    <groupId>org.nlpcn</groupId>
    <artifactId>elasticsearch-sql</artifactId>
    <version>6.8.13.0</version>
</dependency>
2、scala 将sql转为es查询json语句
object ESUtils {
// 1、获取searchDao
lazy val searchDao = {
import org.elasticsearch.client.Client
import org.elasticsearch.client.node.NodeClient
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.util.concurrent.ThreadContext
import org.elasticsearch.threadpool.ThreadPool
// 填写上自己的cluster.name
val settings = Settings.builder().put(“cluster.name”, MetaStorageImplementation.elasticsearchClusterName).build()
// val settings = Settings.builder().put(“cluster.name”, “my-application”).build()
val threadPool = new ThreadPool(settings)
val client = new NodeClient(settings, threadPool)
new org.nlpcn.es4sql.SearchDao(client)
}
//2、通过sql转为es查询语句
def sqlToEsQuery(sql : String) : String = {
val string = searchDao.explain(sql).explain().explain()
string
}
3、测试
def main(args: Array[String]): Unit = {
val sql = ESUtils.sqlToEsQuery(“select * from esIndex where lastTime = 1648540192929”)
val esJSON =ESUtils.sqlToEsQuery(sql)
println(esJSON)
}
}
4、查询返回结果展示(即步骤三esJSON结果打印)
{“from”:0,”size”:1000,”query”:{“bool”:{“filter”:[{“bool”:{“must”:[{“match_phrase”:{“lastAccessed”:{“query”:1648540192929,”slop”:0,”zero_terms_query”:”NONE”,”boost”:1.0}}}],”adjust_pure_negative”:true,”boost”:1.0}}],”adjust_pure_negative”:true,”boost”:1.0}}}
5、打开postman
POST请求
请求头: http://ip:port/esIndex /_search
请求体:
为 步骤四的返回结果展示esJSON
{“from”:0,”size”:1000,”query”:{“bool”:{“filter”:[{“bool”:{“must”:[{“match_phrase”:{“lastTime”:{“query”:1648540192929,”slop”:0,”zero_terms_query”:”NONE”,”boost”:1.0}}}],”adjust_pure_negative”:true,”boost”:1.0}}],”adjust_pure_negative”:true,”boost”:1.0}}}
查询,即可查到符合条件的数据
欢迎使用66资源网
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
7. 本站有不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!

66源码网 » Elasticsearch sql语句转为es查询条件

提供最优质的资源集合

立即查看 了解详情