- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
Es提供了基于JSON的完整查询DSL(Domain Specific Language 特定域的语言)来定义查询。将查询DSL视为查询的AST(抽象语法树)。它由两种子句组成:
叶子查询子句,在特定域中寻找特定的值,如match、term或range查询
复合查询子句包装其他叶子查询或复合查询,并用于以逻辑方式组合多个查询。如bool、dis_max、constant_score查询
POST /索引名称/_search
{
"query":{
"match_all": {}
}
}
查询结果示例:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test-demo1",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "百度3",
"job" : "运营",
"amt" : "3000.34",
"logo" : "http://www.lgstatic.com/ttasdf2",
"createTime" : "20220303230000"
}
}
...省略2条数据
]
}
}
took:查询花费时间,单位是毫秒
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据
全文搜索能够搜索已分析的文本字段,如电子邮件正文、商品描述等。
先造一些测试数据:
PUT /item
{
"settings": {},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
POST /item/_doc/
{
"title": "小米电视4A",
"images": "http://image.lagou.com/12479122.jpg",
"price": 4288
}
POST /item/_doc/
{
"title": "小米手机",
"images": "http://image.lagou.com/12479122.jpg",
"price": 2688
}
POST /item/_doc/
{
"title": "苹果手机",
"images": "http://image.lagou.com/12479122.jpg",
"price": 5699
}
match类型的查询,会把查询条件分词,多个词条之间是or的关系。如下面的例子,会根据小米和手机分别去搜索,能搜出3条数据。
POST /item/_search
{
"query":{
"match": {
"title": "小米手机"
}
}
}
POST /item/_search
{
"query":{
"match": {
"title": {
"query": "小米手机",
"operator":"and"
}
}
}
}
match_phrase查询用来对一个字段进行短语查询,可以指定analyzer、slop移动因子
POST /item/_search
{
"query":{
"match_phrase": {
"title": "小米手机"
}
}
}
带slop:
POST /item/_search
{
"query":{
"match_phrase": {
"title": {
"query": "手机小米",
"slop":2
}
}
}
}
slop参数告诉match_phrase查询词条能够相隔多远时仍然将文档视为匹配。相隔多远的意思是,你需要移动一个词条多少次来让查询和文档匹配
query string提供了无需指定某字段而对文档全文进行匹配查询的一个高级查询,同时可以指定在哪些字段上进行匹配。
GET /item/_search
{
"query": {
"query_string": {
"query": "2688"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "price",
"query": "2688"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "手机 OR 小米"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "手机 and 小米"
}
}
}
#模糊查询
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "小米~1"
}
}
}
#多字段支持
GET /item/_search
{
"query": {
"query_string": {
"fields": ["title","price"],
"query": "2699"
}
}
}
如果你需要在多个字段上进行文本搜索,可用multi_match。
GET /item/_search
{
"query": {
"multi_match": {
"query": "2688",
"fields": ["title","price"]
}
}
}
#还可以使用*配置
GET /item/_search
{
"query": {
"multi_match": {
"query": "2688",
"fields": ["title","pri*"]
}
}
}
可以使用term-level queries根据结构化数据中的精确值查找文档。term-level queries不分析搜索词。搜索词与存储在字段中的词需要完全匹配
用于查询指定字段包含某个搜索词的文档
POST /item/_search
{
"query": {
"term": {
"title":"小米"
}
}
}
POST /item/_search
{
"query": {
"terms": {
"title": ["小米","电视"]
}
}
}
POST /item/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 3000
}
}
}
}
#日期范围
POST /item/_search
{
"query": {
"range": {
"createTime": {
"gte": "2022-01-01",
"lte": "2022-02-01",
"format": "yyyy-MM-dd"
}
}
}
}
GET /item/_search
{
"query": {
"exists": {
"field": "price"
}
}
}
GET /item/_search
{
"query": {
"prefix": {
"title": {
"value": "小米"
}
}
}
}
GET /item/_search
{
"query": {
"wildcard": {
"title":"小*"
}
}
}
GET /item/_search
{
"query": {
"regexp": {
"title":"小米[a-z0-9]"
}
}
}
GET /item/_search
{
"query": {
"fuzzy": {
"title": "手机"
}
}
}
#错别字纠正
GET /item/_search
{
"query": {
"fuzzy": {
"title": {
"value": "大米",
"fuzziness": 1
}
}
}
}
GET /item/_search
{
"query": {
"ids": {
"values": ["t76YgYEB9TD2fYkcLzha","tb6XgYEB9TD2fYkc6zhx"]
}
}
}
GET /item/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"title": "小米"
}
},
"boost": 1.2
}
}
}
POST /item/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
}
], "filter": {
"term": {
"title": "电视"
}
},"must_not": [
{
"range": {
"price": {
"gte": 4200,
"lte": 4300
}
}
}
]
,"minimum_should_match": 0
}
}
}
minimum_should_match代表了最小匹配精度,如果设置为1,代表should语句中至少需要有一个条件满足。
默认情况下,返回的结果是按照相关性进行排序的。默认排序是_score降序
# 按照评分升序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"_score":{
"order":"asc"
}
}
]
}
#根据字段值排序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"price":{
"order":"asc"
}
}
]
}
#多个字段的排序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"price":{
"order":"asc"
}
},{
"createTime": {
"order":"desc"
}
}
]
}
size:每页显示多少条
from:当前页起始索引
POST /item/_search
{
"query": {
"match_all": {}
}
,"size": 2,
"from": 0
}
POST /item/_search
{
"query": {
"match": {
"title": "小米"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [{"title":{}}]
}
}
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
title:这里声明title字段需要高亮
不同的索引
GET /_mget
{
"docs":[
{
"_index":"item",
"_id":"tb6XgYEB9TD2fYkc6zhx"
},
{
"_index":"test-location",
"_id":1
}
]
}
相同的索引
POST /test-location/_search
{
"query": {
"ids": {
"values": ["1","2"]
}
}
}
语法:
POST /_bulk
{"action": {"metadata"}}
{"data"}
示例:
POST /_bulk
{"delete":{"_index":"item","_id":"tb6XgYEB9TD2fYkc6zhx"}}
{"create":{"_index":"item","_id":"1"}}
{"title":"华为电脑","price":2333}
{"update":{"_index":"item","_id":2}}
{"doc":{"title":"冰箱"}}
格式:每个json不能换行,相邻json必须换行
隔离:每个操作互不影响,操作失败的行会返回其失败信息
实际用法:bulk请求一次不要太大,否则一下积压到内存中,性能会下降。所以,一次请求几千个操作、大小在几M正好。bulk会将要处理的数据载入内存中,所以数据量是有限的,最佳的数据量不是一个确定的数据,它取决于你的硬件,你的文档大小以及复杂性,你的索引以及搜索的负载。一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(ES的config下的elasticsearch.yml)中配置。
http.max_content_length: 10mb
书山有路勤为径,学海无涯苦作舟
我在我的应用程序中使用 Hibernate Search。其中一个子集合被映射为 IndexedEmbedded。子对象有两个字段,一个是 id,另一个是日期(使用日期分辨率到毫秒)。当我搜索 id=
The App Engine Search API有一个 GeoPoint 字段。可以用它来进行半径搜索吗?例如,给定一个 GeoPoint,查找位于特定半径内的所有文档。 截至目前,它看起来像 Ge
客户对我正在做的员工管理项目提出了这个新要求,以允许他们的用户进行自定义 bool 搜索。 基本上允许他们使用:AND、OR、NOT、括号和引号。 实现它的最佳方法是什么?我检查了 mysql,它们使
很想知道哪个更快 - 如果我有一个包含 25000 个键值对的数组和一个包含相同信息的 MySQL 数据库,搜索哪个会更快? 非常感谢大家! 最佳答案 回答这个问题的最好方法是执行基准测试。 关于ph
我喜欢 smartcase,也喜欢 * 和 # 搜索命令。但我更希望 * 和 # 搜索命令区分大小写,而/和 ?搜索命令遵循 smartcase 启发式。 是否有隐藏在某个地方我还没有找到的设置?我宁
我有以下 Marklogic 查询,当在查询控制台中运行时,它允许我检索具有管理员权限的系统用户: xquery version "1.0-ml"; import schema namespace b
我希望当您搜索例如“A”时,所有以“A”开头的全名都会出现。因此,如果名为“Andreas blabla”的用户将显示 我现在有这个: $query = "SELECT full_name, id,
我想在我的网站上添加对人名的搜索。好友列表已经显示在页面上。 我喜欢 Facebook 这样做的方式,您开始输入姓名,Facebook 只会显示与查询匹配的好友。 http://cl.ly/2t2V0
您好,我在我的网站上进行搜索时遇到此错误。 Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /ho
声明( 叠甲 ):鄙人水平有限,本文为作者的学习总结,仅供参考。 1. 搜索介绍 搜索算法包括深度优先搜索(DFS)和广度优先搜索(BFS)这两种,从起点开始,逐渐扩大
我正在为用户列表使用 FuturBuilder。我通过 futur: fetchpost() 通过 API 获取用户。在专栏的开头,我实现了一个搜索栏。那么我该如何实现我的搜索栏正在搜索呢? Cont
我正在使用 MVC5,我想搜索结果并停留在同一页面,这是我在 Controller (LiaisonsProjetsPPController) 中执行搜索操作的方法: public ActionRes
Azure 搜索中的两种方法 Upload 与 MergeOrUpload 之间有什么区别。 他们都做完全相同的事情。即,如果文档不存在,它们都会上传文档;如果文档已经存在,则替换该文档。 由于这两种
实际上,声音匹配/搜索的当前状态是什么?我目前正在远程参与规划一个 Web 应用程序,该应用程序将包含和公开记录的短音频剪辑(最多 3-5 秒,人名)的数据库。已经提出了一个问题,是否可以实现基于用户
在商业应用程序中,具有数百个面并不罕见。当然,并非所有产品都带有所有这些标记。 但是在搜索时,我需要添加一个方面查询字符串参数,其中列出了我想要返回的所有方面。由于我事先不知道相关列表,因此我必须在查
当我使用nvcc 5.0编译.cu文件时,编译器会为我提供以下信息。 /usr/bin/ld: skipping incompatible /usr/local/cuda-5.0/lib/libcud
我正在使用基于丰富的 Lucene 查询解析器语法的 Azure 搜索。我将“~1”定义为距离符号的附加参数)。但我面临的问题是,即使存在完全匹配,实体也没有排序。 (例如,“blue~1”将返回“b
我目前有 3 个类,一个包含 GUI 的主类,我在其中调用此方法,一个包含数据的客户类,以及一个从客户类收集数据并将其放入数组列表的 customerList 类,以及还包含搜索数组列表方法。 我正在
假设我有多个 6 字符的字母数字字符串。 abc123、abc231、abc456、cba123、bac231 和 bac123 。 基本上我想要一个可以搜索和列出所有 abc 实例的选择语句。 我只
我有这个表 "Table"内容: +--------+ | Serial | +--------+ | d100m | <- expected result | D100M | <- expect
我是一名优秀的程序员,十分优秀!