Elasticsearch支持请求体Body放在GET请求中,但因为带请求体的GET请求并不被广泛支持,所以同时支持请求体放在POST请求中。
一、空查询:
GET http://$user:$passwd@$host:$port/$index/$type/_search
或者
GET http://$user:$passwd@$host:$port/$index/$type/_search
{}
二、分页:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"from": 0,
"size": 20
}
三、自定义排序:
默认使用_socre降序排序,如果要先按指定字段count进行排序,然后按相关性排序,可以:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"sort": [
{
"count": {
"order": "asc"
}
},
{
"_score":{
"order": "desc"
}
}
]
}
四、相关性得分计算明细:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"explain": true,
"query": {
"match": {
"title": "示例"
}
}
}
五、显示文档的版本号:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"version": true,
"query": {
"match": {
"title": "示例"
}
}
}
六、多个索引查询,为多个索引设置不同权重:
GET http://$user:$passwd@$host:$port/$index1,$index2/_search
{
"indices_boost" : {
$index1 : 2,
$index2 : 0.1
},
"query": {
"match": {
"title": "示例"
}
}
}
或者
GET http://$user:$passwd@$host:$port/$index1,$index2/_search
{
"indices_boost" : [
{
$index1 : 2
},
{
$index2 : 0.1
}
],
"query": {
"match": {
"title": "示例"
}
}
}
七、只查相关性评分高于指定分值的文档:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"min_score": 80,
"query": {
"match": {
"title": "示例"
}
}
}
八、指定前N条文档重新计算相关性评分:
GET http://$user:$passwd@$host:$port/$index/_search
{
"query": {
"match": {
"title": "示例"
}
},
"rescore": {
"window_size": 2,
"query": {
"rescore_query": {
"match_phrase": {
"title": {
"query": "示例"
}
}
},
"query_weight": 1,
"rescore_query_weight": 2
}
}
}
也可以进行多次重新计算相关性评分:
GET http://$user:$passwd@$host:$port/$index/_search
{
"query": {
"match": {
"title": "示例"
}
},
"rescore": [
{
"window_size": 2,
"query": {
"rescore_query": {
"match_phrase": {
"title": {
"query": "示例"
}
}
},
"query_weight": 1,
"rescore_query_weight": 2
}
},
{
"window_size": 1,
"query": {
"score_mode": "multiply",
"rescore_query": {
"function_score": {
"script_score": {
"script": {
"source": "Math.log10(doc.readCount.value + 1000)"
}
}
}
}
}
}
]
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-rescore.html
九、高亮:
对查询结果的内容进行高亮显示,一般都与query查询条件结合使用。高亮有三种方式,分别是:
1、unified:使用Lucene的统一高亮器,是ES默认的高亮方式,可以将文本分成句子,支持精确、模糊、前缀,正则等高亮,并使用BM25算法对单个句子进行评分;
2、plain:使用Lucene的标准高亮器,为了准确反映查询逻辑,它会创建一个很小的内存索引,所以会相对耗性能;
3、fvh:即快速矢量高亮器,mapping字段的属性“term_vector”设置为"with_positions_offsets"的字段上才能使用,不支持range匹配高亮。
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"highlight": {
"order": "score",
"no_match_size": 150,
"pre_tags": "<span>",
"post_tags": "</span>",
"fields": [
{
"title": {
"type": "fvh",
"number_of_fragments": 0
}
}
]
}
}
高亮器的属性分为全局属性和局部属性,字段中的局部属性会把全局属性覆盖。常用属性有:
1、number_of_fragments:返回的高亮片断数,这些片断可能不是完整的句子。如果设置为0,则如果有高亮时将整个字段的内容都返回,可以避免高亮片断不完整的问题,但如果当前字段上无高亮,则该字段不返回高亮片断;
2、fragment_size:返回的每个高亮片断的长度,如果number_of_fragments为0,则fragment_size不生效;
3、no_match_size:如果无高亮片断时从该字段开始位取截指定长度,保证highlight中有内容,但不高亮;
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-highlighting.html
十、查询表达式:
1、match_all:查询所有文档:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"match_all": {}
}
}
2、match:如果字段类型是keyword,进行整个字段的完全匹配。如果字段类型是text,则根据指定的分析器进行分词后对这些分词完全匹配,boost表示相关性得分的增加倍数。
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query" : {
"match" : {
"title" : "示例"
}
}
}
或者:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"match": {
"title": {
"query": "示例",
"boost": 5
}
}
}
}
3、multi_match:类似于match,但可以在多个字段上执行相同的match查询,^5表示相关性得分的增加倍数:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"multi_match": {
"query": "示例",
"fields": ["title^5","content"]
}
}
}
4、match_phrase:短语匹配查询,slop表示分词的跨度,指分词和分词之间可以相隔多少个词,缺失了这些词仍然可以查到结果:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"match_phrase": {
"title": {
"query": "示例成功",
"slop": 4,
"boost": 5
}
}
}
}
5、term:精确匹配单个值,被查字段的类型必须是数字、时间、布尔或者keyword的字符串,区分大小写的。如果查询字段会分词,即使内容相同也不会被查到 :
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"term": {
"title": {
"value": "示例结果",
"boost": 5
}
}
}
}
6、terms:精确匹配多个值,类似于term,但terms规定只要被查字段包含指定数组中任何一个值就算符合条件:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"terms": {
"title": ["百度","阿里"],
"boost": 5
}
}
}
7、prefix:分词的前缀匹配,会扫描全部的倒排索引,倒排索引一般都经过分词器转成了小写 ,而查询字符串本身不会被拆词,所以不会转成小写,如果大小写不一致也不会返回结果:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"prefix": {
"title": {
"value": "示例",
"boost": 5
}
}
}
}
8、wildcard:分词的通配符(*表示任何字符串,?表示任何单个字符)匹配,会扫描全部的倒排索引,所以不区分大小写,找到符合通配符格式的分词:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"wildcard": {
"title": {
"value": "*例",
"boost": 5
}
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-wildcard-query.html
9、regexp:分词的正则匹配,会扫描全部的倒排索引,倒排索引一般都经过分词器转成了小写 ,而查询字符串本身不会被拆词,所以不会转成小写,如果大小写不一致也不会返回结果:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"regexp": {
"title": {
"value": ".*例",
"boost": 5
}
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-regexp-query.html
10、fuzzy:分词的近似匹配,fuzziness用于控制levenshtein距离
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"fuzzy": {
"title": {
"value": "requeab",
"fuzziness": 2,
"boost": 5
}
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-fuzzy-query.html
11、range:数值类型的区间匹配,可以使用:gt(大于)、gte(大于等于)、lt(小于)、lte(小于等于):
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"range": {
"count": {
"gte": 200,
"lt": 250
}
}
}
}
12、exist:匹配指定字段是非空的文档:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"exists": {
"field": "title",
"boost": 5
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-exists-query.html
13、ids:匹配id是指定的数组中的值的文档:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"ids": {
"values": [1,3]
}
}
}
14、filter:符合条件就返回,否则不会返回:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"bool":{
"filter": {
"range" : {
"字段名": {
"gte": 2
}
}
}
}
}
}
15、多条件组合查询:使用bool将must,must_not,should以及filter组合进行查询,must,must_not,should以及filter中又可以嵌套多层bool组成更复杂的查询。其中:
1>must:必须满足这些条件;
2>must not:必须不满足这些条件;
3>should:可以满足也可以不满足这些条件,不要求必须满足,一般用于控制相关性得分,比如满足该条件时增加相关性得分权重;
4>filter:过滤,用于控制符合条件的文档要不要被查询出来,而不能控制查询出来的文档相关性得分。也就是说只是用来控制有没有,而不能控制得分高不高;
为了更好的控制查询结果,bool中有一些常用的属性控制查询策略:
1>minimum_should_match:控制should条件至少要匹配的数量,因为should条件不是必须满足,所以不加该参数时可能都不满足;
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"title.keyword": {
"value": "示例",
"boost": 5
}
}
},
{
"match_phrase": {
"title": {
"query": "示例",
"boost": 10
}
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"title": {
"query": "示例"
}
}
},
{
"prefix": {
"title": {
"value": "示例",
"boost": 15
}
}
}
]
}
},
{
"multi_match": {
"fields": [
"title^5",
"content"
],
"query": "示例"
}
}
],
"filter": [
{
"term": {
"count": "999"
}
}
],
"minimum_should_match": 1
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/bool-query-usage.html
十一、查询字符串:
使用查询字符串query_string:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"query_string": {
"fields" : ["title", "content"],
"query" : "this AND that",
"boost": 5
}
}
}
可以转化成如下格式,查到的结果是一样的:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"query_string": {
"query": "(title:this OR content:this) AND (title:that OR content:that)",
"boost": 5
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-query-string-query.html
十二、简单查询字符串:
使用简单查询字符串simple_query_string,它不会抛异常,而且会忽略错误的查询语法:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"query": {
"simple_query_string": {
"fields": ["title", "content"],
"query": "(关键字1~1 关键字2~1) + (关键字3 关键字4) | (关键字5 关键字6)",
"boost": 5
}
}
}
详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-simple-query-string-query.html