Elasticsearch 结构化查询
程序员文章站
2022-07-14 13:25:26
...
1)环境准备
启动Elasticsearch https://blog.csdn.net/qq_36918149/article/details/104221934
启动Kinbana https://blog.csdn.net/qq_36918149/article/details/104224625
2)什么是结构化数据?
2)什么是结构化数据搜索?
3)demo 演示
#结构化搜索,数据初始化
DELETE products
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }
GET products/_mapping
#对布尔值 match 查询,有算分
POST products/_search
{
"profile": "true",
//表示看索引规则
"explain": true,
"query": {
"term": {
"avaliable": true
}
}
}
#结果搜索出了 "avaliable": true的所有记录
#对布尔值,通过constant score 转成 filtering,没有算分
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"constant_score": {
"filter": {
"term": {
"avaliable": true
}
}
}
}
}
#没有算分功能,查询更快
#数字类型 Term,查询固定值
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"term": {
"price": 30
}
}
}
#数字类型 terms,查询区间
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"price": [
"20",
"30"
]
}
}
}
}
}
#数字 Range 查询,gte 大于等于,lte小于等于
GET products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"price" : {
"gte" : 20,
"lte" : 30
}
}
}
}
}
}
#日期 range,3y表示三年
POST products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"date" : {
"gte" : "now-3y"
}
}
}
}
}
}
日期表达式
#exists查询
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"exists": {
"field": "date"
}
}
}
}
}
#查询date字段存在的数据
#处理多值字段
POST /movies/_bulk
{ "index": { "_id": 1 }}
{ "title" : "Father of the Bridge Part II","year":1995, "genre":"Comedy"}
{ "index": { "_id": 2 }}
{ "title" : "Dave","year":1993,"genre":["Comedy","Romance"] }
#处理多值字段,term 查询是包含,而不是等于
POST movies/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"genre.keyword": "Comedy"
}
}
}
}
}
#字符类型 terms
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"productID.keyword": [
"QQPX-R-3956-#aD8",
"JODL-X-1937-#pV7"
]
}
}
}
}
}
推荐阅读
-
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
-
SQL分页查询存储过程代码分享
-
mysql 正则表达式查询含有非数字和字符的记录
-
mysql模糊查询like与REGEXP的使用详细介绍
-
Spring Data JPA实现动态条件与范围查询实例代码
-
sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
-
MySQL 随机查询数据与随机更新数据实现代码
-
SQL学习笔记八 索引,表连接,子查询,ROW_NUMBER
-
在SQL查询中使用LIKE来代替IN查询的方法
-
sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)