首页 > 其他分享 >Elasticsearch 结构化搜索

Elasticsearch 结构化搜索

时间:2024-10-09 19:32:37浏览次数:1  
标签:结构化 9200 elastic products 搜索 Elasticsearch Type KJNQ2rMeC481nFwSsqyf localhost

过滤器

当进行精确查找时,我们会使用过滤器。

  • term 单值匹配
  • terms 多值匹配
  • bool 复合过滤器(must/must_not/should)
  • range 范围查询 (gt/lt/gtq/lte)
  • exists null值查询

使用 constant_score 以非评分模式进行查询。

示例数据

curl -X DELETE -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products"
curl -X POST -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_bulk" -d'
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
'
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products?pretty"

term 精确单值查询

term 查询数值

查询价格为 20 的产品

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "term": {
                    "price": 20
                }
            }
        }
    }
}
'

term 查询文本

ES 会对文本进行分词导致不能直接用精确匹配查询,需要指定文本的类型为精确值

以下查询会查找失败

curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "term": {
                    "productID": "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}
'

查看 ES 如何分析(分词)字段

  curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_analyze?pretty" -d'
  {
      "field": "productID",
      "text": "XHDK-A-1293-#fJ3"
  }
  '

重新索引数据(删除索引是必须的,ES不能更新已存在的映射)

curl -X DELETE -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products"

ES 5.0 开始,使用 keyword 类型表示不需要分词的文本

curl -k -X PUT -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products" -d'
{
    "mappings": {
        "properties": {
            "productID": {
                "type": "keyword"
            }
        }
    }
}
'
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_mappings?pretty"

组合过滤器

bool 复合过滤器

  • must 必须匹配,等价于 and
  • must_not 不能匹配,等价于 not
  • should 至少一个匹配,等价于 or
{
    "bool": {
        "must": [],
        "must_not": [],
        "should": []
    }
}

ES 5.0 废弃了 filtered 语法

  curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
  {
      "query": {
          "bool": {
              "should": [
                  {"term": {"price": 20}},
                  {"term": {"productID": "XHDK-A-1293-#fJ3"}}
              ],
              "must_not": [
                  {"term": {"price": 30}}
              ]
          }
      }
  }
  '

嵌套 bool 过滤器


curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "bool": {
            "should": [
                {"term": {"productID": "KDKE-B-9947-#kL5"}},
                {"bool": {
                    "must": [
                        {"term": {"productID": "JODL-X-1937-#pV7"}},
                        {"term": {"price": 30}}
                    ]
                }}
            ]
        }
    }
}
'

terms 精确多值查询

query -> constant_scoe -> filter -> term/terms

query -> bool -> shold/must/must_not -> term

curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "terms": {
                    "price": [20, 30]
                }
            }
        }
    }
}
'

对于标签类数据(数组数据),terms 会返回包含词项的数据;如果要返回完全相等的数据,可以新增一个字段记录数组词项个数。

范围查询

range 过滤器可以使用以下表达式

  • gt
  • lt
  • gte
  • lte
curl -k -X GET -H 'Content-Type: application/json' -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "price": {
                        "gte": 20,
                        "lte": 40
                    }
                }
            }
        }
    }
}
'

range 支持日期计算

减1小时

"range" : {
    "timestamp" : {
        "gt" : "now-1h"
    }
}

加1个月

"range" : {
    "timestamp" : {
        "gt" : "2014-01-01 00:00:00",
        "lt" : "2014-01-01 00:00:00||+1M" 
    }
}

range 支持字典顺序

"range" : {
    "title" : {
        "gte" : "a",
        "lt" :  "b"
    }
}

处理null值

示例数据

curl -X POST -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_bulk" -d'
{ "index": { "_id": "1"              }}
{ "tags" : ["search"]                }  
{ "index": { "_id": "2"              }}
{ "tags" : ["search", "open_source"] }  
{ "index": { "_id": "3"              }}
{ "other_field" : "some data"        }  
{ "index": { "_id": "4"              }}
{ "tags" : null                      }  
{ "index": { "_id": "5"              }}
{ "tags" : ["search", null]          } 
'

存在 tags 字段的文档(存在该字段,但值为 null 不返回)

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "exists": {"field": "tags"}
            }
        }
    }
}
'

ES 5.0 开始废弃 missing,需要使用 must_not 代替

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/products/_search?pretty" -d'
{
    "query": {
        "bool": {
            "must_not": [
                {"exists": {"field": "tags"}}
            ]
        }
    }
}
'

在需要查询某字段是否被显示设置为 null 的场景时,可以将 null 指定成一个特殊的值,比如字符串类型可以指定 "null_value"。

标签:结构化,9200,elastic,products,搜索,Elasticsearch,Type,KJNQ2rMeC481nFwSsqyf,localhost
From: https://www.cnblogs.com/liaozibo/p/18440584

相关文章

  • 基于禁忌搜索算法的VRP问题求解matlab仿真,带GUI界面,可设置参数
    1.程序功能描述基于禁忌搜索算法的VRP问题求解matlab仿真,带GUI界面,可设置参数。2.测试软件版本以及运行结果展示MATLAB2022a版本运行 3.核心程序whileCOUNT<=ItertionsֲL=zeros(Ant_Num,1);fori=1:Ant_NumInfor_Tabu_tmps=......
  • ElasticSearch7.17.3简介+centos7详细安装教程+Springboot整合ES
    一、ElasticSearch简介    官方地址:Elasticsearch:官方分布式搜索和分析引擎|Elastic1.1ElasticSearch简介        Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,同时是可扩展的数据存储和矢量数据库,能够应对日益增多的各种用例。作为......
  • 6、Elasticsearch集群
    Elasticsearch集群1、三台机器大家集群192.168.204.209elasticsearch.ymlcluster.name:mycluster#集群名,所有节点node都应配置相同的集群名node.name:node1#本节点名,同一集群下不同的node名字不能重复node.master:truenode.data:truenetwork.host:0.0.0.0http.p......
  • 1、Elasticsearch安装
    Elasticsearch安装1.1什么是elasticsearch?ElasticSearch是一个分布式,高性能、高可用、可伸缩的搜索和分析系统。ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。Elasticsearch是用Java开发的,并作为Apache许可......
  • 【C++】二叉搜索树
    文章目录1、二叉搜索树的说明性2、二叉搜索树2.1二叉搜索树的概念2.2二叉搜索树的操作2.2.1插入2.2.2查找2.2.3删除2.3二叉搜索树的实现2.4二叉搜索树的应用二叉搜索树的性能分析1、二叉搜索树的说明性map和set特性需要先铺垫二叉搜索树,而二叉搜索树也是一种树形......
  • 二分搜索与二分答案
    二分前提条件数组的有序的数组数组中无重复元素,否则查找的元素下标可以不算唯一的二分答案二分答案时需要满足答案的有界性二分答案的思路:首先判断这个解是否为可行解,然后我们”认为“这个可行解的最优解,然后以这个可行解为标准去左(右)区间寻找最优解时间复杂......
  • 【电商搜索】现代工业级电商搜索技术-EMNLP2024-无监督的用户偏好学习
    【电商搜索】现代工业级电商搜索技术-EMNLP2024-无监督的用户偏好学习0.论文信息Title:UnsupervisedHumanPreferenceLearningAuthors:SumukShashidhar,AbhinavChinta,VaibhavSahai,DilekHakkaniTurComments:EMNLP2024MainConferencehttps://arxiv.or......
  • 逆向 Virustotal 搜索接口 X-VT-Anti-Abuse-Header
    搜索示例搜索123,网页地址为:https://www.virustotal.com/gui/search/123/comments请求接口GET/ui/search?limit=20&relationships%5Bcomment%5D=author%2Citem&query=123HTTP/1.1Accept-Encoding:gzip,deflate,br,zstdAccept-Ianguage:en-US,en;q=0.9,es;q=0.8Accep......
  • 3.搜索、模拟
    搜索、模拟\(A\)luoguP1120小木棍\(B\)luoguP2540[NOIP2015提高组]斗地主加强版\(C\)CF58EExpression\(D\)CF293BDistinctPaths\(E\)[ABC352F]EstimateOrder\(F\)[ABC336F]RotationPuzzle\(G\)CF525EAnyaandCubes\(H\)luoguP9234买瓜\(I\)......
  • el-Select组件远程搜索没有箭头图标
    在控制台查看对应的dom,发现使用远程搜索之后,对应的icon的class缺失,将缺失部分的class补全(el-icon-arrow-up)即可tip:只需要找到对应的dom,然后添加class(el-icon-arrow-up)初始化的时候新增下拉箭头的class,当用户触发聚焦的时候,还得添加对应的旋转的class(is-reverse......