查询所有

query代表一个查询对象,里面可以有不同的查询属性

Get http://127.0.0.1:9200/demo/_search

{
  "query": {
    "match_all": {}
  }
}

字段匹配查询

Get http://127.0.0.1:9200/demo/_search?q=age:22

{
	"took": 40,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"match": {
			"age": 22
		}
	}
}
{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

multi_match与match类似,不同的是它可以在多个字段中查询

{
	"query": {
		"multi_match": {
			"query": "2",
			"fields": [
				"username",
				"address"
			]
		}
	}
}
{
	"took": 13,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 0.6682933,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 0.6682933,
				"_source": {
					"username": "小白2",
					"age": 22,
					"address": "中国上海2"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 0.6682933,
				"_source": {
					"username": "小白2",
					"age": 25,
					"address": "中国上海2"
				}
			}
		]
	}
}

精确匹配查询

term查询,精确的关键词匹配查询,不对查询条件进行分词

{
	"query": {
		"term": {
			"username": {
				"value": "小白2"
			}
		}
	}
}

多关键字精确查询

terms 查询和 term 查询一样,允许指定多值进行匹配。

如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql的in

{
	"query": {
		"terms": {
			"username": [
				"小白白",
				"小白"
			]
		}
	}
}

查询指定字段

默认情况下,ES在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果只想获取其中的部分字段,可以添加_source的过滤

Get http://127.0.0.1:9200/demo/_search

{
  "query": {
    "match_all": {

    },
  },
  "_source":['username']
}
{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 1,
				"_source": {
					"username": "小白2"
				}
			}
		]
	}
}

字段过滤

includes:来指定想要显示的字段

excludes:来指定不想要显示的字段

Get http://127.0.0.1:9200/demo/_search

{
  "_source": {
    "includes": ["age","username"]
  },  
  "query": {
    "match_all": {
			
		},
  }
}

分页查询

from:当前页的起始索引,默认从0开始。 from = (pageNum - 1) * size

size:每页显示多少条

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2
}
{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

单字段排序

通过order指定排序的方式。desc降序,asc升序。

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2,
		"_source":['username'],
		"sort":{
		    "age":{
		        "order":"desc"
		    }
		}
}
{
	"took": 469,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 4,
			"relation": "eq"
		},
		"max_score": null,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": null,
				"_source": {
					"username": "小白2"
				},
				"sort": [
					25
				]
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": null,
				"_source": {
					"username": "小白白"
				},
				"sort": [
					22
				]
			}
		]
	}
}

多字段排序

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2,
		"_source":['username'],
		"sort":[
		    {
		    "age":{
		        "order":"desc"
		    }
		},
		{
      "_score":{
        "order": "desc"
      }
    }
		    ]
}
{
	"took": 4,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 1,
				"_source": {
					"username": "小白2"
				},
				"sort": [
					25,
					1
				]
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白"
				},
				"sort": [
					22,
					1
				]
			}
		]
	}
}

模糊查询

返回包含与搜索字词相似的字词的文档。

编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:

更改字符(box → fox)

删除字符(black → lack)

插入字符(sic → sick)

转置两个相邻字符(act → cat)

为了找到相似的术语,fuzzy查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。

通过fuzziness修改编辑距离。一般使用默认值AUTO,根据术语的长度生成编辑距离。

Get http://127.0.0.1:9200/demo/_search

{
  "query": {
    "fuzzy": {
      "username": {
        "value": "白",
        "fuzziness": 2
      }
    }
  }
}

组合查询

bool把各种其它查询通过must(必须 )must_not(必须不)should`(应该)的方式进行组合

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "小白"
					}
				}
			]
		}
	}
}
{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 1.8220872,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.8220872,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 1.6877716,
				"_source": {
					"username": "小白2",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

或者查询

{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "123"
					}
				}
			]
		}
	}
}

范围查询

range查询找出那些落在指定区间内的数字或者时间,支持一下字符

操作符 说明 符号
gt 大于 >
gte 大于等于 >=
lt 小于 <
lte 小于等于 <=

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "123"
					}
				}
			],
			"filter":{
			    "range":{
			        "age":{
			            "gt":23
			        }
			    }
			}
		}
	}
}
{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 0,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 0,
				"_source": {
					"username": "小白2",
					"age": 25,
					"address": "中国上海2"
				}
			}
		]
	}
}

完全匹配查询

Get http://127.0.0.1:9200/demo/_search

{
   "query":{
       "match_phrase":{
           "username":"小白白"
       }
   }
}
{
	"took": 20,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0316575,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.0316575,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			}
		]
	}
}

高亮查询

Get http://127.0.0.1:9200/demo/_search

{
	"query": {
		"match_phrase": {
			"username": "小白白"
		}
	},
	"highlight": {
		"pre_tags": "<font color='red'>",
		"post_tags": "</font>",
		"fields": {
			"username": {}
		}
	}
}
{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0316575,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.0316575,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				},
				"highlight": {
					"username": [
						"<font color='red'>小</font><font color='red'>白</font><font color='red'>白</font>"
					]
				}
			}
		]
	}
}

聚合查询

Get http://127.0.0.1:9200/demo/_search

对某个字段取最大值max

{
    "aggs":{
      "max_age":{
        "max":{"field":"age"}
      }
    },
    "size":0
}

对某个字段取最大值min

{
    "aggs":{
      "min_age":{
        "min":{"field":"age"}
      }
    },
    "size":0
}

对某个字段求和sum

{
    "aggs":{
      "sum_age":{
        "sum":{"field":"age"}
      }
    },
    "size":0
}

对某个字段取平均值avg

{
    "aggs":{
      "avg_age":{
        "avg":{"field":"age"}
      }
    },
    "size":0
}

对某个字段的值进行去重之后再取总数

{
    "aggs":{
      "distinct_age":{
        "cardinality":{"field":"age"}
      }
    },
    "size":0
}

桶聚合查询

桶聚和相当于sql中的group by语句,terms聚合,分组统计

Get http://127.0.0.1:9200/demo/_search

{
	"aggs": {
		"group_age": {
			"terms": {
				"field": "age"
			}
		}
	},
	"size": 0
}
{
	"took": 165,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"group_age": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": [
				{
					"key": 22,
					"doc_count": 2
				},
				{
					"key": 25,
					"doc_count": 1
				}
			]
		}
	}
}

Stats聚合

stats聚合,对某个字段一次性返回count,max,min,avg和sum五个指标

Get http://127.0.0.1:9200/demo/_search

{
	"aggs": {
		"stats_age": {
			"stats": {
				"field": "age"
			}
		}
	},
	"size": 0
}
{
	"took": 8,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"stats_age": {
			"count": 3,
			"min": 22,
			"max": 25,
			"avg": 23,
			"sum": 69
		}
	}
}