DSL语言高级查询
1.无条件查询
GET /article/_search
{
"query": {
"match_all": {}
}
}
2.有查询条件
1). 模糊匹配 match (会根据查询得字段进行分词)
- match : 通过match关键词模糊匹配条件内容
- prefix : 前缀匹配
- regexp : 通过正则表达式来匹配数据
1.模糊查询 match, match会根据该字段的分词器,进行分词查询
GET /article/_search
{
"from": 0,
"size": 2,
"query": {
"match": {
"title": "师"
}
}
}
2. 模糊短语查询
GET /article/_search
{
"from": 0,
"size": 2,
"query": {
"match_phrase": {
"content": "老师"
}
}
}
3. 多个字段模糊查询
POST /article/_search
{
"query": {
"multi_match": {
"query": "老师NB",
"fields": [
"title",
"content"
]
}
}
}
2)term 精确查询,不会对字段进行分词查询
注意: 采用term精确查询, 查询字段映射类型属于为keyword.
分析“苹果手机”的分词
POST /product_db/_analyze
{
"analyzer": "standard",
"text": "苹果手机"
}
分析结果
{
"tokens" : [
{
"token" : "苹",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "果",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "手",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "机",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
}
]
}
查出结果为空
GET /product_db/_search
{
"query": {
"term": {
"keywords": {
"value": "苹果"
}
}
}
}
可以查出结果
GET /product_db/_search
{
"query": {
"term": {
"keywords": {
"value": "苹"
}
}
}
}
3)query_string 查询
指定字段条件查询 query_string , 含 AND 与 OR 条件
GET /product_db/_search
{
"query": {
"query_string": {
"fields": [
"keywords"
],
"query": "苹果 AND 手机"
}
}
}
未指定字段条件查询 query_string , 含 AND 与 OR 条件
GET /product_db/_search
{
"query": {
"query_string": {
"query": "苹果 AND 手机"
}
}
}
4)范围查询
GET /product_db/_search
{
"query": {
"range": {
"price": {
"gte": 2000,
"lte": 6000
}
}
},
"from": 1,
"size": 2,
"_source": ["name","keywords","subTitle"]
}
5)Filter过滤器方式查询,不会计算相关性分值,不排序, 效率高,结果可以被缓存。
GET /product_db/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"price": {
"gte": 2000,
"lte": 6000
}
}
}
]
}
},
"from": 1,
"size": 2,
"_source": ["name","keywords","subTitle"]
}
6)组合条件查询(多条件查询)
GET /product_db/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"price": {
"gte": 2000,
"lte": 6000
}
}
}
],
"must": [
{
"match": {
"keywords": "手机"
}
}
],
"should": [
{
"term": {
"name": {
"value": "小明"
}
}
}
],
"must_not": [
{
"term": {
"name": {
"value": "小小"
}
}
}
]
}
},
"from": 1,
"size": 2,
"_source": ["name","keywords","subTitle"]
}
7)连接查询(多文档合并查询)
- 父子文档查询:parent/child
- 嵌套文档查询: nested
1.嵌套查询
GET /product_db/_search
{
"query": {
"nested": {
"path": "attrs",
"query": {
"match": {
"attrs.attrId": "1"
}
}
}
}
}
2.根据子文档查询父文档
GET aid_rdr_zsyy_xjb_index/_search
{
"query": {
"has_child": {
"type": "aid_pat_visit",
"query": {
"match": {
"aid_pat_visit.empi_id": "0000504341"
}
}
}
}
}
总结:
1. match
match:模糊匹配,需要指定字段名,会进行分词,查询条件相对来说比较宽松。
2. term
term: 查询时不会进行分词,但是如果内容进行分词了,就有可能查不到,比如查询"hello world"的字样,"hello world"会被分成hello和world,不存在"hello world",因此这里的查询结果会为空。这也是term查询和match的区别。
3. match_phase
match_phase:会对输入做分词,但是需要结果中也包含所有的分词,而且顺序要求一样。以"hello world"为例,要求结果中必须包含hello和world,而且还要求他们是连着的,顺序也是固定的,hello that world不满足,world hello也不满足条件。
4. query_string
query_string:和match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛。