match
对字段进行全文检索,最基本和常用的查询类型,API示例如下:
GET book/_search
{
"query": {
"match": {
"title": "java菜"
}
}
}
首先会将查询条件进行分词,如果查询的字段是text的格式,也会对要查询的字段进行分词。满足一个分词的条件就好被查询出来。
text
上面的title是text类型,查询结果是:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.474477,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.474477,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "6e028090-453d-4995-9b6f-c64fec701d57",
"_score" : 0.37881336,
"_source" : {
"id" : "6e028090-453d-4995-9b6f-c64fec701d57",
"title" : "java基础",
"author" : "张三",
"wordCount" : 1000,
"publishDate" : "2019-09-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "9b821e2a-79dc-4288-b47a-817ad8496eda",
"_score" : 0.33698124,
"_source" : {
"id" : "9b821e2a-79dc-4288-b47a-817ad8496eda",
"title" : "java大法好",
"author" : "张三",
"wordCount" : 2000,
"publishDate" : "2019-11-01T10:00:00"
}
}
]
}
}
包含java或菜的文档都会被查询出来。下面是对一个查询的分析。
除了或的关系,还能指定分词的结果必须同时满足。
GET book/_search
{
"query": {
"match": {
"title": {
"query": "java学",
"operator": "and"
}
}
}
}
查询结果要必须包含java和学:
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.474477,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.474477,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
}
]
}
}
通过minimum_should_match可以控制需要匹配的单词数。
GET book/_search
{
"query": {
"match": {
"title": {
"query": "java学",
"minimum_should_match": 2 #必须包含2个匹配的单词
}
}
}
}
keword或者其他
keword或者其他格式的字段是不会进行分词的。例如:
GET book/_search
{
"query": {
"match": {
"author": "李四"
}
}
}
这里的author是keyword格式,所以必须等于李四才会返回,而不会分词。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.2039728,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.2039728,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
}
]
}
}
match_phrase
match_phrase是短语搜索,亦即它会将给定的短语(phrase)当成一个完整的查询条件。当使用match_phrase进行搜索的时候,你的结果集中,所有的Document都必须包含你指定的查询词组。
match_phrase匹配keyword字段
这个同上必须跟keywork一致才可以。
GET book/_search
{
"query": {
"match_phrase": {
"author": "李四"
}
}
}
返回结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.3862944,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
}
]
}
}
match_phrase匹配text字段
match_phrase是分词的,text也是分词的。match_phrase的分词结果必须在text字段分词中都包含,而且顺序必须相同,而且必须都是连续的。
GET book/_search
{
"query": {
"match_phrase": {
"title": "怎么学"
}
}
}
返回结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 2.5122144,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 2.5122144,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "Ic7qYmwB4Jr3cw6pi6Wc",
"_score" : 2.5122144,
"_source" : {
"title" : "es怎么学",
"author" : "李",
"wordCount" : 3500,
"publishDate" : "2019-11-01T11:11:11"
}
}
]
}
}
query_string
可以在query中增加筛选条件,default_field表示默认查询的字段。
GET book/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "java AND 菜"
}
}
}
fields 指定要查询的字段。
GET book/_search
{
"query": {
"query_string": {
"fields": ["title", "author"],
"query": "java AND 张三"
}
}
}
term
将查询语句作为整个单词进行查询,即不对查询语句做分词处理。
term查询keyword字段
term不会分词。而keyword字段也不分词。需要完全匹配才可。
GET book/_search
{
"query": {
"term": {
"author": {
"value": "李四"
}
}
}
}
返回结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.3862944,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
}
]
}
}
term查询text字段
因为text字段会分词,而term不分词,所以term查询的条件必须是text字段分词后的某一个。
GET book/_search
{
"query": {
"term": {
"title": {
"value": "java"
}
}
}
}
返回结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.57843524,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "6e028090-453d-4995-9b6f-c64fec701d57",
"_score" : 0.57843524,
"_source" : {
"id" : "6e028090-453d-4995-9b6f-c64fec701d57",
"title" : "java基础",
"author" : "张三",
"wordCount" : 1000,
"publishDate" : "2019-09-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 0.5155618,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "9b821e2a-79dc-4288-b47a-817ad8496eda",
"_score" : 0.5155618,
"_source" : {
"id" : "9b821e2a-79dc-4288-b47a-817ad8496eda",
"title" : "java大法好",
"author" : "张三",
"wordCount" : 2000,
"publishDate" : "2019-11-01T10:00:00"
}
}
]
}
}
terms
类似于sql中的in,查询字段title包含学和菜。
GET book/_search
{
"query": {
"terms": {
"title": [
"学",
"菜"
]
}
}
}
返回结果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "book",
"_id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"_score" : 1.0,
"_source" : {
"id" : "c7738478-d1da-43d7-b839-29e894cbdf08",
"title" : "java怎么学",
"author" : "李四",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "a7c5b2d2-aff7-415a-8c24-3caebc938116",
"_score" : 1.0,
"_source" : {
"id" : "a7c5b2d2-aff7-415a-8c24-3caebc938116",
"title" : "j菜谱",
"author" : "王五",
"wordCount" : 2500,
"publishDate" : "2019-10-01T11:11:11"
}
},
{
"_index" : "book",
"_type" : "book",
"_id" : "Ic7qYmwB4Jr3cw6pi6Wc",
"_score" : 1.0,
"_source" : {
"title" : "es怎么学",
"author" : "李",
"wordCount" : 3500,
"publishDate" : "2019-11-01T11:11:11"
}
}
]
}
}
range
范围查询主要针对数值和日期类型,如下所示:
GET book/_search
{
"query": {
"range": {
"wordCount": {
"gte": 1000,
"lte": 2000
}
}
}
}
包含比较条件是:gt、gte、lt、lte。
对于日期类型还提供了更友好的计算方式:
例如:
GET book/_search
{
"query": {
"range": {
"publishDate": {
"gte": "2011-09-01T11:11:11||+5y",
"lte": "now+2M"
}
}
}
}
Bool Query
布尔查询由一个或多个布尔子句组成,主要包括4个:
其中的must、must_not、should、filter都是支持数组格式,每个元素既可以是term类的字段查询也可以是bool的复合条件查询。
Filter
Filter查询只过滤符合条件的文档,不会进行相关性算分。
- es针对filter会有智能缓存,因此其执行效率很高。
- 做简单匹配查询且不考虑算分时,推荐使用filter代替query等。
GET book/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"author": "张三"
}
}
]
}
}
}
must
必须同时满足设置的多个查询条件。
GET book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"author": "李四"
}
},
{
"match": {
"title": "java"
}
}
]
}
}
}
must_not
必须不包含。例如:
GET book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"author": "张三"
}
}
],
"must_not": [
{
"match": {
"title": "java"
}
}
]
}
}
}
should
当bool查询中只包含should,不包含must。
当bool查询中同时包含should和must。
_count
_count用来查询符合条件的文档数,而不用返回具体内容。
GET book/_count
{
"query": {
"term": {
"title": {
"value": "java"
}
}
}
}
返回结果:
{
"count" : 3,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
_source
过滤返回结果中_source中的字段,主要有以下几种方式。