Elasticsearch 结构化搜索详解
搜索elasticsearch 详解 结构化
2023-09-11 14:15:46 时间
文章目录
1. 结构化数据
1.结构化搜索(Structured search) 是指对结构化数据的搜索
- 日期,布尔类型和数字都是结构化
2.文本也可以是结构化的
- 如彩色笔可以有离散的颜色集合:红(red)、绿(green)、蓝(blue)
- 一个博客可能被标记了标签,例如,分布式(distributed)和搜索(search)
- 电商网站上的商品都有 UPCs(通用产品码 Universal Product Codes)或其他的唯一标识,它们都遵从严格规定的、结构化的格式
2. ES 中的机构化搜索
布尔、时间,日期和数字
这类结构化数据:有精确的格式,我们可以对这些格式进行逻辑操作。包括比较数字或时间的范围,或判断两个值的大小- 结构化的文本可以做到
精确匹配
或者部分匹配
Term
查询 /Prefix 前缀
查询 - 结构化结构只有 “是” 或 “否” 两个值
根据场景需要,可以决定结构化搜索是否需要打分
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"}
#查看mapping
GET products/_mapping
{
"products" : {
"mappings" : {
"properties" : {
"avaliable" : {
"type" : "boolean"
},
"date" : {
"type" : "date"
},
"price" : {
"type" : "long"
},
"productID" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
3.1 对布尔值 match 查询,有算分
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"term": {
"avaliable": true
}
}
}
3.2 对布尔值,通过 constant score 转成 filtering,没有算分
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"constant_score": {
"filter": {
"term": {
"avaliable": true
}
},
"boost": 1.2
}
}
}
3.3 数字类型 Term
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"constant_score": {
"filter": {
"term": {
"price": 30
}
},
"boost": 1.2
}
}
}
3.4 数字类型 terms
POST products/_search
{
"profile": "true",
"explain": true, //explain 返回文档的评分解释
"query": {
"constant_score": {
"filter": {
"terms": {
"price": [
"20",
"30"
]
}
}
}
}
}
3.5 数字 Range 查询
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 20,
"lte":30
}
}
}
}
}
}
- gt 大于
- lt 小于
- gte 大于等于
- lte 小于等于
3.6 日期 range
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"date": {
"gte": "now-1y" //当前时间减1天
}
}
}
}
}
}
Date Match Expressions
2014-01-01 00:00:00 || +1M
3.7 exists 查询 - 非空查询
POST products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"exists": {
"field":"date"
}
}
}
}
}
3.8 字符类型 terms
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"productID.keyword": [
"QQPX-R-3956-#aD8",
"JODL-X-1937-#pV7"
]
}
}
}
}
}
3.9 处理多值字段
#demo
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"]}
4.0 处理多值字段,term 查询是包含,而不是等于
//返回2条数据
POST movies/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"genre.keyword": "Comedy"
}
}
}
}
}
4.1 Match 跟 term 对比
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"term": {
"date": "2019-01-01"
}
}
}
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"match": {
"date": "2019-01-01"
}
}
}
参考资料:
极客时间:Elasticsearch核心技术与实战
相关阅读:
初学elasticsearch入门
Elasticsearch本地安装与简单配置
docker-compose安装elasticsearch集群
Elasticsearch 7.X之文档、索引、REST API详解
Elasticsearch节点,集群,分片及副本详解
Elasticsearch倒排索引介绍
Elasticsearch Analyzer 进行分词详解
Elasticsearch search API详解
Elasticsearch URI Search 查询方法详解
Elasticsearch Request Body 与 Query DSL详解
Elasticsearch Dynamic Mapping 和常见字段类型详解
Eelasticsearch 多字段特性及 Mapping 中配置自定义 Analyzer详解
Elasticsearch index template与dynamic template详解
Elasticsearch 聚合分析简介
Elasticsearch 第一阶段总结与测试
Elasticsearch 基于词项和基于全文的搜索详解
相关文章
- 通过fluentd配置输出到elasticsearch,启动服务报错 Using Elasticsearch client 8.4.0 is not compatible for your Elasticsearch server
- Elasticsearch-搜索并获取数据
- 在启动时将文件夹添加到 MATLAB 搜索路径
- GraphMatrix::BFS广度优先搜索
- 分布式搜索elasticsearch 基本概念
- LeetCode高频题33. 搜索旋转排序数组
- Elasticsearch 配置跨集群搜索
- Elasticsearch Term & Phrase Suggester搜索建议
- ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch
- 全文搜索之 Elasticsearch
- ElasticSearch搜索实例含高亮显示及搜索的特殊字符过滤
- 深入浅出 spring-data-elasticsearch 之 ElasticSearch 架构初探(一)
- macOS SwiftUI 封装组件之 可设置高度搜索框NSSearchField(教程含源码)
- Lucene搜索方式大合集
- ElasticSearch + Canal 开发千万级的实时搜索系统
- 玩转elasticsearch:ElasticSearch介绍
- odoo 模糊搜索
- Google已占据全球搜索市场75.8%份额
- Devexpress 开发思路:1看案例;2点一下看属性列表;3论坛搜索和提问;4看文档(文档大多就写个名字,没有例子);
- [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
- 【bzoj1415】[Noi2005]聪聪和可可 期望记忆化搜索