ES-Search API

一、Search API的分类

  • URI Search
    在url中传入查询参数进行查询,
  • Request Body Search
    使用es提供的,基于json的更加完备的Query Domain Specific Language(DSL)
语法范围
/_search集群上所有的索引
/index1/_search查询索引名为index1的索引
/index1,index2/_search查询索引名为index1和index2的索引中的数据
/index*/_search查询index开头的索引

二、URI Search

  • URI查询参数说明
    GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
    {
    “profile”:true
    }

    • q指定查询语句
    • df指定查询的字段,不指定时,会对索引内所有字段进行查询
    • sort排序
    • from+size分页参数
    • profile查看查询如何被执行(注意这个参数放在请求体里)
  • q指定参数查询详细描述

    • 指定字段查询 vs 泛查询
      • q=title:2012 指定了仅对title字段进行查询,效果与q=2012&df=title相同
      • q=2012 对所有字段进行查询
    • 布尔查询
      • AND OR NOT 或者 && || !
        • 必须大写
        • title:(Beautiful OR Mind)
      • 分组
        • +代表must
        • -代表must not
        • title:(+Beautiful -Mind)
    • Term查询 vs Phrase查询
      • q=Beautiful Mind 效果与 q=Beautiful OR Mind 一样
      • q=“Beautiful Mind” 效果与 q=Beautiful AND Mind一样
    • 分组 vs 引号
      • q=title:Beautiful Mind 这个查询与tile:Beautiful OR Mind效果一样,即查询title为Beautiful 或 所有字段中存在Mind;如果想要查询title中存在Beautiful OR Mind,则需要使用括号进行分组,即q=title:(Beautiful Mind)
      • q=title:“Beautiful Mind”
    • 范围查询
      • 区间表示[]表示闭区间 {}表示开区间
      • q=year:[2003 TO 2023]
      • q=year:[* TO 2023]
    • 算数符号
      • q=year:(>=2003 && <=2023)
      • q=year:<=2023
    • 通配符(查询效率低,占用内存大,尤其是放在最前面,不建议使用)
      • q=title:(Beuti???) ?代表1个字符
      • q=title:(Beuti*) *或者0代表多个字符
    • 正则
      *
    • 模糊匹配与近似查询

三、Request Body Search

  • 请求 request
    在这里插入图片描述
  • 响应 response
    在这里插入图片描述
    • 衡量相关度
      • Precision(查准率) - 尽可能的返回较少的无关文档
      • Recall(查全率)-尽量返回较多的相关文档
      • Ranking-是否能够按照相关度进行排序?
        其中查准率和查全率的计算方式我们用图来示例:
        在这里插入图片描述
  • Request Body用法详解
GET /movies/_search
{
	"profile":true, ## 展示查询过程
	"_source":["title","order_date"], ## 要查询的字段,支持通配符比如[*name*]
	"script_fields":{ ## 脚本字段
		"new_field_name":{ ## 新字段名称
			"script":{ ## 指定脚本
				"lang":"painless", ## 指定要执行的脚本
				"source":"doc['order_date'].value+'hello'" ## 脚本内容
			}
		}
	}
	"from":0, ## 分页偏移量,从0开始
	"size":20, ## 每页数量,默认10
	"query":{ ## 查询条件
		"match_all":{},	## 查询全部
		"match":{
			"title":"Beutiful Mind", ## 查询title中存在Beutiful OR Mind的文档
			"title":{ ## 查询title中存在Beutiful AND Mind的文档
				"query":"Beutiful Mind", 
				"operator":"AND"
			}
		},
		"match_phrase":{ ## 模糊匹配
			"title":{ ## 查询title中按顺序存在Beutiful AND Mind 但是中间可以存在一个其他单词的文档
				"query":"Beutiful Mind",
				"slop":1
			}
		},
		"query_string":{ ## 查询字段title中存在Beutiful AND Mind
			"default_field":"title",
			"query":"Beutiful AND Mind"
		},
		"query_string":{
			"fields":["title","name"],
			"query":"(Beutiful AND Mind) OR (JAVA AND ES)"
		},
		"simple_query_string":{ ## 类似于query_string,不支持AND OR NOT会当作字符串处理,Term之间默认是OR,可以指定operator,+代替AND  -代替NOT  |代替OR
			"fields":["title","name"],
			"query":"Beutiful -Mind",
			"default_operator":"AND"
		}
	},
	"sort":[ ## 排序条件,最好在数字型和日期型字段加上排序
		{"order_date":"desc"}
	]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值