查询匹配
- match:匹配(会使用分词器解析(先分析文档,然后进行查询))
- _source:过滤字段
- sort:排序
form、size 分页 form 从第几条数据开始 size 返回几条数据
GET /baobao/_search
{
"query": {
"match": {
"name": "包子"
}
}
, "_source": ["name"]
, "sort": [
{
"name.keyword": {
"order": "desc"
}
}
]
, "from": 0
, "size": 1
}
sort 排序
因为name的type是text, text或annotated_text字段doc_values默认为false。
简单理解,就是text字段作为一个整体,默认没有索引。
不过text分词之后的keyword是有索引的,因而可以对name.keyword进行聚合。
也可以set fielddata=true,不过不推荐。多条件查询(Bool)
must 相当于 and
- should 相当于 or
- must_not 相当于 not (… and …)
filter 过滤
GET /baobao/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "包子"
}
},{
"match": {
"age": "12"
}
}
],"filter": [
{"range": {
"age": {
"gte": 10, //gte大于等于 gt 大于
"lte": 20 //lte小于等于 lt 小于
}
}}
]
}
}
}
匹配数组
貌似不能与其它字段一起使用
- 可以多关键字查(空格隔开)— 匹配字段也是符合的
- match 会使用分词器解析(先分析文档,然后进行查询)
搜词
GET /baobao/_search
{
"query": {
"match": {
"tags": "高 好吃"
}
}
}
精确查询
term 直接通过 倒排索引 指定词条查询
适合查询 number、date、keyword ,不适合text
GET /baobao/_search
{
"query": {
"term": {
"name.keyword": {
"value": "包子"
}
}
}
}
text和keyword
text:
- 支持分词,全文检索、支持模糊、精确查询,不支持聚合,排序操作;
- text类型的最大支持的字符长度无限制,适合大字段存储;
- keyword:
// 自定义前缀和后缀 GET /baobao/_search { “query”: { “match”: { “name”:”包子” } } , “highlight”: { “pre_tags”: “
“, “post_tags”: “
“, “fields”: { “name”: {} } } } ```![image.png](/uploads/projects/yuqueyonghug7snii@lqp5gs/6089a486662707421f98517417a002e9.png)