8.4. Query DSL

简介:


8.4.1. match 匹配

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {
			"tag" : "美"   
		}
	}
}
'
			

8.4.2. multi_match 多字段匹配

multi_match 实现多字段查询

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query": {
	    "multi_match": {
		    "query":      "国际",
		    "type":       "cross_fields",
		    "fields":     [ "title", "content" ],
		    "operator":   "and"
	    }
	},
	"from": 0,
	"size": 20,
	"_source":["id","title","ctime"],
	"sort": [
	   {
	      "ctime": {"order": "desc"}
	   }
	]
	
}
'
			

8.4.3. Query bool 布尔条件

Elasticsearch 提供三个布尔条件

must: AND

must_not:NOT

should:OR

8.4.3.1. must

查询必须满足 tags=天气 and title 包含 台风关键字

				
curl -XPOST http://test:123456@so.netkiller.cn/information/article/_search?pretty  -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "tags" : "天气" }},
        { "match": { "title": "台风"   }}
      ]
    }
  },
 "_source":["id","title","ctime"],
 "highlight" : {
        "pre_tags" : ["<strong>", "<b>"],
        "post_tags" : ["</strong >", "</b>"],
        "fields" : {
            "content" : {}
        }
    }
}'
				
				

8.4.3.2. should

查询必须满足标title or author 两个条件

GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "Linux" }},
        { "match": { "author": "Neo"   }}
      ]
    }
  }
}
				

可以嵌套使用

GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }},
        { "bool":  {
          "should": [
            { "match": { "translator": "Constance Garnett" }},
            { "match": { "translator": "Louise Maude"      }}
          ]
        }}
      ]
    }
  }
}				
				

8.4.3.3. must_not

8.4.4. filter 过滤

query 相当于 SQL 中的 LIKE 匹配, filter 更像是 where 条件。下面的例子查询 site_id = 23 的数据并且 tags 包含 “头条” 关键字

curl -XGET 'http://test:123456@so.netkiller.cn/information/article/_search?pretty' -d '
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "tags": "头条"
        }
      },
      "filter": {
        "term": {
          "site_id" : "23"
        }
      }
    }
  }
}'
			

8.4.5. sort 排序

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {"tag" : "美"}
	},
	"sort": {
		"ctime": {"order": "desc", "mode":  "min"}
	}
}
'			

8.4.6. _source

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {
			"tag" : "美"   
		}
	},
	"_source":["id","title","ctime"]
}
'

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"_source":["id","title","ctime"],
	"query" : {
		"match" : {"tag" : "美"}
	},
	"sort": {
		"ctime": {"order": "desc", "mode":  "min"}
	}
}
'	
			

8.4.7. highlight 高亮处理

			
curl -XPOST http://test:123456@so.netkiller.cn/information/article/_search  -d'
{
    "query" : { "match" : {  "content" : "股市" }},
    "highlight" : {
        "pre_tags" : ["<strong>", "<b>"],
        "post_tags" : ["</strong >", "</b>"],
        "fields" : {
            "content" : {}
        }
    }
}
'			
			
			




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
5月前
|
SQL 存储 关系型数据库
SQL(Structured Query Language)简介和常见 SQL 命令示例
SQL(Structured Query Language)是一种用于访问和操作关系型数据库的标准语言。它是一个功能强大的语言,用于执行各种数据库操作,包括检索数据、插入新记录、更新记录、删除记录、创建数据库、创建新表、设置权限以及执行存储过程和视图等。以下是 SQL 的一些重要方面:
76 0
|
5月前
|
SQL 关系型数据库 数据库连接
Hasor【环境搭建 03】Dataway接口配置服务使用DataQL聚合查询引擎(SQL执行器实现分页查询举例说明+报错 Query dialect missing 原因分析及解决)
Hasor【环境搭建 03】Dataway接口配置服务使用DataQL聚合查询引擎(SQL执行器实现分页查询举例说明+报错 Query dialect missing 原因分析及解决)
140 0
|
SQL 自然语言处理 索引
DSL的诞生 | 复杂sql转成Elasticsearch DSL深入详解
源自死磕ElasticsearchQQ群(626036393)中的一个问题: 问题如下: where (position=ES or work=ES or content=ES) and academic=本科 and (city=北京 or city=深圳) 1 怎么构建ES的查询条件? 我的问题拆解与实现如下:
1751 0
DSL的诞生 | 复杂sql转成Elasticsearch DSL深入详解
Elasticsearch Query DSL
Elasticsearch Query DSL
82 0
|
SQL 索引
ES中复杂DSL查询语句不会写怎么办?
ES中复杂DSL查询语句不会写怎么办?
202 0
|
SQL 自然语言处理 Java
使用SQL替代DSL操作ES
有接触过大数据BI框架的同学应该都有类似的需求:项目需要对接不同类型的数据源(如:MYSQL、ES、HIVE等),并提供一个数据视图(View)用于用户编写 SQL,并将SQL执行结果转为对应报表项。
使用SQL替代DSL操作ES
|
缓存 Java
Elasticsearch Query DSL之Compound queries(复合查询)
Elasticsearch Query DSL之Compound queries(复合查询)
Elasticsearch Query DSL之Compound queries(复合查询)
|
存储 自然语言处理 算法
Elasticsearch Query DSL之全文检索(Full text queries)上篇
Elasticsearch Query DSL之全文检索(Full text queries)上篇
Elasticsearch Query DSL之全文检索(Full text queries)上篇
|
存储 JSON 缓存
Elasticsearch Query DSL之Term level queries
Elasticsearch Query DSL之Term level queries
Elasticsearch Query DSL之Term level queries
|
自然语言处理 Java 关系型数据库
Elasticsearch Query DSL之全文检索(Full text queries)下篇
Elasticsearch Query DSL之全文检索(Full text queries)下篇