文章目录
1、GET /lib/user/_search
: 查询lib索引下的user类型的全部数据
2、GET /lib/_search
:查询lib索引下的全部类型的数据
3、GET /_search
:查询全部索引下的数据
ElasticSearch
会为我们自动创建映射,这就意味着只要是文本就会为我们使用分词器分词。POST /my_store/products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
constant_score
查询以非评分模式来执行 term
查询并以一作为统一评分,这样返回的结果的评分全部是1constant_score
将term转化为过滤器查询GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"price" : 20
}
}
}
}
}
//结果如下
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "my_store",
"_type": "products",
"_id": "2",
"_score": 1,
"_source": {
"price": 20,
"productID": "KDKE-B-9947-#kL5"
}
}
]
}
}
GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"productID" : "XHDK-A-1293-#fJ3" //虽然和插入的数据一样,但是却查询不到
}
}
}
}
}
productID
如何实现分词的,如下:GET /my_store/_analyze
{
"field": "productID", //指定分词的域
"text": "XHDK-A-1293-fJ3-the" //文本内容
}
//结果如下:
{
"tokens": [
{
"token": "xhdk",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "a",
"start_offset": 5,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "1293",
"start_offset": 7,
"end_offset": 11,
"type": "<NUM>",
"position": 2
},
{
"token": "fj3",
"start_offset": 12,
"end_offset": 15,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "the",
"start_offset": 16,
"end_offset": 19,
"type": "<ALPHANUM>",
"position": 4
}
]
}
-
和&
DELETE my_store //先删除索引
PUT /my_store //手动指定映射
{
"mappings" : {
"products" : {
"properties" : {
"productID" : {
"type" : "string",
"index" : "not_analyzed" //不分词
}
}
}
}
}
GET /my_store/products/_search
{
"query":{
"terms":{
"price":[20,10,30]
}
}
}
from
和size
指定文档的数量,如下:GET /my_store/products/_search
{
"from":0, //从第一文档开始
"size":2, //查询两个文档
"query":{
"terms":{
"price":[20,10,30]
}
}
}
_source
指定返回的字段GET /lib/user/_search
{
"_source":["address","age"],
"query": {
"match_phrase": {
"address": "huibei,wuhan"
}
}
}
exclude
即可GET /lib/user/_search
{
"_source":{
"exclude": ["address","age"], //排除字段
"include": ["name","date"] //包含的字段
},
"query": {
"match_phrase": {
"address": "huibei,wuhan"
}
}
}
version:true
即可GET /my_store/products/_search
{
"version":true,
"from":0,
"size":2,
"query":{
"terms":{
"price":[20,10,30]
}
}
}
productId
的时候,因为terms不知道分词器的存在,因此查询不到,但是我们使用match查询可以匹配到,如下:GET /my_store/products/_search
{
"query" : {
"match" : {
"productID" : "XHDK-A-1293-#fJ3"
}
}
}
zhaoliu
或者zhaoming
的,那么只需要使用match即可GET /my_store/products/_search
{
"query" : {
"match" : {
"name" : "zhaoliu zhaoming" //会对这个短语进行分词,分出两个,之后去查询
}
}
}
GET /my_store/products/_search
{
"query": {
"match_all": {
}
}
}
match
查询, match_phrase
查询首先将查询字符串解析成一个词项列表,然后对这些词项进行搜索,但只保留那些包含 全部 搜索词项,且 位置 与搜索词项相同的文档。 比如对于 quick fox
的短语搜索可能不会匹配到任何文档,因为没有文档包含的 quick
词之后紧跟着 fox
。GET /lib/user/_search
{
"query": {
"match_phrase": {
"address": "huibei,wuhan"
}
}
}
sort
可以进行排序GET /lib/user/_search
{
"_source":{
"exclude": ["address","name"],
"include": ["age","date"]
},
"query": {
"match_phrase": {
"address": "huibei,wuhan"
}
},
"sort": [ //指定排序
{
"age": { //对字段age进行排序
"order": "desc"
},
"address": { //address排序
"order": "asc"
}
}
]
}
gt
: >
大于(greater than)
lt
: <
小于(less than)gte
: >=
大于或等于(greater than or equal to)lte
: <=
小于或等于(less than or equal to)GET /lib/user/_search
{
"query": {
"range": {
"date": {
"gt": "2010-11-11", //大于
"lt": "2012-12-31" //小于
}
}
}
}
GET /lib/user/_search
{
"query": {
"range": {
"date": {
"gt":"now" //查询大于现在时间的文档
}
}
}
}
GET /lib/user/_search
{
"query": {
"range": {
"date": {
"gt":"now-1h" //查询距离现在一小时之内的文档,直接使用now减去一小时即可
}
}
}
}
GET /lib/user/_search
{
"query": {
"range": {
"date": {
"gt": "2010-11-11 00:00:00", //指定时分秒查询
"lt": "2012-12-31 00:00:00"
}
}
}
}
GET /lib/user/_search
{
"query": {
"range": {
"price": {
"gt": 10, //数值范围查找
"lt": 20
}
}
}
}
range
查询同样可以处理字符串字段, 字符串范围可采用 字典顺序(lexicographically) 或字母顺序(alphabetically)。例如,下面这些字符串是采用字典序(lexicographically)排序的:"range" : {
"title" : {
"gte" : "a",
"lt" : "b"
}
}
*
: 代表一个或者多个字符?
:代表任意一个字符GET team/user/_search
{
"query": {
"wildcard": {
"name":"chen*"
}
}
}
GET team/user/_search
{
"query": {
"wildcard": {
"name":"chen?iabing"
}
}
}
chenjiabing
这个名字,那么使用模糊查询的话,如果其中有个字符写错了,也是能够查询到的GET team/user/_search
{
"query": {
"fuzzy": {
"name":"chejiabing"
}
}
}
GET team/user/_search
{
"query": {
"fuzzy": {
"name":"chejiabing"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"], //指定包裹的标签前半部分,默认的是<em>
"post_tags": ["</span>"], //指定后半部分
"fields": {
"name": {} //name字段高量
}
}
}
exists
这个语句用来查询存在值的信息,如果和must结合表示查询不为null的数据,如果must_not集合表示查询为null
的数据,如下://查询password=null的数据
GET ea/user/_search
{
"query": {
"bool": {
"must_not":{
"exists":{
"field":"password"
}
}
}
}
}
//查询password!=null的数据
GET ea/user/_search
{
"query": {
"bool": {
"must":{
"exists":{
"field":"password"
}
}
}
}
}
post_filter
GET /lib/user/_search
{
"post_filter": {
"term": {
"age":22
}
}
}
must
:所有的语句都 必须(must) 匹配,与 AND
等价。must_not
:所有的语句都 不能(must not) 匹配,与 NOT
等价。should
:至少有一个语句要匹配,与 OR
等价。{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
GET /lib/user/_search
{
"query": {
"bool": {
"must": [
{"term": {"age":22}},
{"match":{"address": "湖北"}}
]
}
}
}
GET /lib/user/_search
{
"post_filter": {
"bool": {
"should": [
{"term":{"name":"郑元梅"}},
{"term":{"age":33}}
],
"must_not": [
{"term":{"age":22}}
]
}
}
}
select * from user where name="郑元梅" or age=33 or(age=22 and price=33);
GET /lib/user/_search
{
"query": {
"bool": {
"should": [
{"term":{"name":"郑元梅"}},
{"term":{"age":33}},
{
"bool": {
"must": [
{"term":{"age":22},
{"term":{"price":33}}
}
]
}
}
]
}
}
}
GET /lib/user/_search
{
"post_filter": {
"range": {
"age": {
"gte": 20,
"lte": 21
}
}
}
}
select * from user where address is not null
GET /lib/user/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "address"
}
}
}
}
}
GET /lib/user/_search
{
"size": 0, //在使用聚合的时候,默认还会返回全部的文档结果,如果不需要,可以使用size限制
"aggs": {
"sum_age": { //sum_age 指定返回字段的名称
"sum": { //sum是指定的聚合函数的名称
"field": "age" //这里指定聚合的字段
}
}
}
}
GET /lib/user/_search
{
"size": 0,
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
GET /lib/user/_search
{
"size": 0,
"aggs": {
"max_age": {
"max": {
"field": "age"
}
}
}
}
GET /lib/user/_search
{
"size": 0,
"aggs": {
"min_age": {
"min": {
"field": "age"
}
}
}
}
GET /lib/user/_search
{
"size": 0,
"aggs": {
"cardinality_age": {
"cardinality": { //查询某个字段的基数,就是对应的字段有多少个不同的值
"field": "age"
}
}
}
}
terms
GET /lib/user/_search
{
"size": 0,
"aggs": {
"age_group": {
"terms": {
"field": "date" //按照日期进行分组
}
}
}
}
//结果如下:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"age_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1352635200000,
"key_as_string": "2012-11-11 12:00:00",
"doc_count": 5 //分组的数量
},
{
"key": 1352592000000,
"key_as_string": "2012-11-11 00:00:00",
"doc_count": 1
}
]
}
}
}
GET /lib/user/_search
{
"size": 0,
"query": {
"term": {
"age": "22"
}
},
"aggs": {
"age_group": {
"terms": {
"field": "date"
}
}
}
}
select *,avg(age) from user group by date;
GET /lib/user/_search
{
"query": {
"term": {
"age": "22"
}
},
"aggs": {
"age_group": {
"terms": {
"field": "date"
},
"aggs": { //直接在分组的聚合中,再次使用聚合求age的均值
"age_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}
GET /lib/user/_search
{
"query": {
"term": {
"age": "22"
}
},
"aggs": {
"age_group": {
"terms": {
"field": "date",
"order": {
"age_avg": "asc" //按照聚合查询的平均年龄进行升序排序
}
},
"aggs": {
"age_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}