初始化数据
创建一个company索引,在company索引下面建立一个emp类型,并且批量保存几个文档
curl -XPOST 'study0:9200/company/emp/_bulk' -d '
{ "index": { "_id": 1 }}
{ "name" : "zhangshan", "age" : 21 , "sex": "man", "birthday": "1996-01-02" , "hobby": "i like sport" }
{ "index": { "_id": 2 }}
{ "name" : "lisi", "age" : 21 , "sex": "man", "birthday": "1995-01-02" , "hobby": "i like compute" }
{ "index": { "_id": 3 }}
{ "name" : "wangwu", "age" : 17 , "sex": "man", "birthday": "1998-01-02" , "hobby": "i like trivel" }
{ "index": { "_id": 4 }}
{ "name" : "wangba", "age" : 25 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like trivel" }
{ "index": { "_id": 5 }}
{ "name" : "xiaoming", "age" : 25 , "sex": "woman", "birthday": "1991-01-02" , "hobby": "i like song" }
{ "index": { "_id": 6 }}
{ "name" : "xiaohong", "age" : 31 , "sex": "man", "birthday": "1988-01-02" , "hobby": "i like piano" }
{ "index": { "_id": 7 }}
{ "name" : "zhaowu", "age" : 31 , "sex": "man", "birthday": "1997-01-02" , "hobby": "i like trivel and music" }
{ "index": { "_id": 8 }}
{ "name" : "zhaoliu", "age" : 18 , "sex": "woman", "birthday": "1998-01-02" , "hobby": "i like compute game" }
{ "index": { "_id": 9 }}
{ "name" : "zhaoqi", "age" : 20 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like compute game" }
'
match
类似sql中的like,但是比like查询更加强大
match_all
match_all 表示要获取全部文档
curl -XPOST 'study0:9200/company/emp/_search' -d '
{
"query": {
"match_all": {}
}
}'
查询结果:
{"took":10,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":9,"max_score":1.0,"hits":[{"_index":"company","_type":"emp","_id":"5","_score":1.0,"_source":{ "name" : "xiaoming", "age" : 25 , "sex": "woman", "birthday": "1991-01-02" , "hobby": "i like song" }},{"_index":"company","_type":"emp","_id":"8","_score":1.0,"_source":{ "name" : "zhaoliu", "age" : 18 , "sex": "woman", "birthday": "1998-01-02" , "hobby": "i like compute game" }},{"_index":"company","_type":"emp","_id":"9","_score":1.0,"_source":{ "name" : "zhaoqi", "age" : 20 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like compute game" }},{"_index":"company","_type":"emp","_id":"2","_score":1.0,"_source":{ "name" : "lisi", "age" : 21 , "sex": "man", "birthday": "1995-01-02" , "hobby": "i like compute" }},{"_index":"company","_type":"emp","_id":"4","_score":1.0,"_source":{ "name" : "wangba", "age" : 25 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like trivel" }},{"_index":"company","_type":"emp","_id":"6","_score":1.0,"_source":{ "name" : "xiaohong", "age" : 31 , "sex": "man", "birthday": "1988-01-02" , "hobby": "i like piano" }},{"_index":"company","_type":"emp","_id":"1","_score":1.0,"_source":{ "name" : "zhangshan", "age" : 21 , "sex": "man", "birthday": "1996-01-02" , "hobby": "i like sport" }},{"_index":"company","_type":"emp","_id":"7","_score":1.0,"_source":{ "name" : "zhaowu", "age" : 31 , "sex": "man", "birthday": "1997-01-02" , "hobby": "i like trivel and music" }},{"_index":"company","_type":"emp","_id":"3","_score":1.0,"_source":{ "name" : "wangwu", "age" : 17 , "sex": "man", "birthday": "1998-01-02" , "hobby": "i like trivel" }}]}}
pretty
在查询url后面添加pretty,可以格式化查询结果,使阅读更加友好
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"match_all": {}
}
}'
查询结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9,
"max_score" : 1.0,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "xiaoming",
"age" : 25,
"sex" : "woman",
"birthday" : "1991-01-02",
"hobby" : "i like song"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"name" : "zhaoliu",
"age" : 18,
"sex" : "woman",
"birthday" : "1998-01-02",
"hobby" : "i like compute game"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"name" : "zhaoqi",
"age" : 20,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like compute game"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "lisi",
"age" : 21,
"sex" : "man",
"birthday" : "1995-01-02",
"hobby" : "i like compute"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "xiaohong",
"age" : 31,
"sex" : "man",
"birthday" : "1988-01-02",
"hobby" : "i like piano"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "zhangshan",
"age" : 21,
"sex" : "man",
"birthday" : "1996-01-02",
"hobby" : "i like sport"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
match
查询hobby字段,喜欢trivel的人
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"match": {"hobby": "trivel"}
}
}'
查询结果
{
"took" : 20,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.86312973,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 0.86312973,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 0.6160039,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 0.25316024,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
多条件查询
多条件查询的时候,可以用bool对象。Bool包含的字段有:must,must_not,should
must和must_not
查找hobby是compute,并且sex字段不是”man”的文档
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match": {"hobby": "compute"}},
"must_not": {"match": {"sex": "man"}}
}
}
}'
查询结果:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.45315093,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "8",
"_score" : 0.45315093,
"_source" : {
"name" : "zhaoliu",
"age" : 18,
"sex" : "woman",
"birthday" : "1998-01-02",
"hobby" : "i like compute game"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "9",
"_score" : 0.45315093,
"_source" : {
"name" : "zhaoqi",
"age" : 20,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like compute game"
}
}
]
}
}
should
查询hobby是trivel或者性别是man的文档
这种查询和sql中的or很类似
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match": {"hobby": "trivel"}},
"should": {"match": {"sex": "man"}}
}
}
}'
查询结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.86312973,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 0.86312973,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 0.7983254,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 0.5408423,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
Term过滤
term主要用于精确匹配值,比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型)
{ "term": { "age": 26 }}
{ "term": { "date": "2017-06-09" }}
{ "term": { "sex": true }}
用term过滤替换match
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "term": {"hobby": "trivel"}},
"should": {"term": {"sex": "man"}}
}}
}'
查询结果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.86312973,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 0.86312973,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 0.7983254,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 0.5408423,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
terms可以用来匹配多个值
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "terms": {"hobby": ["trivel","music"]}}
}
}
}'
查询结果,可以匹配到语句里面包含,trivel,或者music的单词。
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.2320077,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 1.2320077,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 0.86312973,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 0.25316024,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
关于match和term比较的例子
- term主要是用于精确的过滤,代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇,比如说,要匹配”我爱中国”,只能匹配到包含”我爱中国”的文本
- match会根据你给定的字段提供合适的分析器,比如需要匹配”我爱中国”,会先分词,我、爱、中国、我爱等,然后在匹配
match
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match": {"hobby": "trivel and"}}
}
}
}'
查询结果
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.2320077,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "7",
"_score" : 1.2320077,
"_source" : {
"name" : "zhaowu",
"age" : 31,
"sex" : "man",
"birthday" : "1997-01-02",
"hobby" : "i like trivel and music"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 0.86312973,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "3",
"_score" : 0.25316024,
"_source" : {
"name" : "wangwu",
"age" : 17,
"sex" : "man",
"birthday" : "1998-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
上面的,返回包含trivel 或者 and 或者 trivel and 都包含的索引。
term
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "term": {"hobby": "trivel and"}}
}
}
}'
查询结果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
但是可以看到使用terms是没有返回结果的。就说明terms是精准匹配词组。
Range过滤
Range过滤允许我们按照指定的范围查找一些数据:操作范围:gt(大于),gae(大于等于),lt(小于),lte(小于等于):
range之gt和lt
查找出大于20岁,小于25岁的emp
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"range": {
"age": {"gt":20,"lt":30}
}
}
}
}'
查询结果
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "xiaoming",
"age" : 25,
"sex" : "woman",
"birthday" : "1991-01-02",
"hobby" : "i like song"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "lisi",
"age" : 21,
"sex" : "man",
"birthday" : "1995-01-02",
"hobby" : "i like compute"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
},
{
"_index" : "company",
"_type" : "emp",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "zhangshan",
"age" : 21,
"sex" : "man",
"birthday" : "1996-01-02",
"hobby" : "i like sport"
}
}
]
}
}
exists和 missing过滤
Exists表示包含某个字段,missing表示没有某个字段的文档
exists
匹配有age字段的文档
curl -XPOST 'study0:9200/company/emp/_search' -d '
{
"query": {
"exists": {
"field": "age"
}
}
}
}'
查询结果
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":9,"max_score":1.0,"hits":[{"_index":"company","_type":"emp","_id":"5","_score":1.0,"_source":{ "name" : "xiaoming", "age" : 25 , "sex": "woman", "birthday": "1991-01-02" , "hobby": "i like song" }},{"_index":"company","_type":"emp","_id":"8","_score":1.0,"_source":{ "name" : "zhaoliu", "age" : 18 , "sex": "woman", "birthday": "1998-01-02" , "hobby": "i like compute game" }},{"_index":"company","_type":"emp","_id":"9","_score":1.0,"_source":{ "name" : "zhaoqi", "age" : 20 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like compute game" }},{"_index":"company","_type":"emp","_id":"2","_score":1.0,"_source":{ "name" : "lisi", "age" : 21 , "sex": "man", "birthday": "1995-01-02" , "hobby": "i like compute" }},{"_index":"company","_type":"emp","_id":"4","_score":1.0,"_source":{ "name" : "wangba", "age" : 25 , "sex": "woman", "birthday": "1996-01-02" , "hobby": "i like trivel" }},{"_index":"company","_type":"emp","_id":"6","_score":1.0,"_source":{ "name" : "xiaohong", "age" : 31 , "sex": "man", "birthday": "1988-01-02" , "hobby": "i like piano" }},{"_index":"company","_type":"emp","_id":"1","_score":1.0,"_source":{ "name" : "zhangshan", "age" : 21 , "sex": "man", "birthday": "1996-01-02" , "hobby": "i like sport" }},{"_index":"company","_type":"emp","_id":"7","_score":1.0,"_source":{ "name" : "zhaowu", "age" : 31 , "sex": "man", "birthday": "1997-01-02" , "hobby": "i like trivel and music" }},{"_index":"company","_type":"emp","_id":"3","_score":1.0,"_source":{ "name" : "wangwu", "age" : 17 , "sex": "man", "birthday": "1998-01-02" , "hobby": "i like trivel" }}]}}
注意:上面全部结果都查询出来了
bool
用bool可以进行多行条件查询
- must: 多个查询条件的完全匹配,相当于 and 。
- must_not:多个查询条件的相反匹配,相当于 not 。
- should:至少有一个查询条件匹配, 相当于 or 。
执行下面的查询
curl -XGET ‘study0:9200/company/emp/_search?pretty’ -d ‘
{
“query”: {
“bool”: {
“must”: { “match”: {“hobby”: “trivel”}},
“must_not”: {“match”: {“sex”: “man”}}
}
}
}’
查询结果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.8631297,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "4",
"_score" : 1.8631297,
"_source" : {
"name" : "wangba",
"age" : 25,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like trivel"
}
}
]
}
}
普通查询与过滤条件合并
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"must": {"match": {"hobby": "compute"}},
"filter": [{"term":{"age": 20}}]
}
}
}'
查询结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.45315093,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "9",
"_score" : 0.45315093,
"_source" : {
"name" : "zhaoqi",
"age" : 20,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like compute game"
}
}
]
}
}
条件过滤
curl -XPOST 'study0:9200/company/emp/_search?pretty' -d '
{
"query": {
"bool": {
"filter": [{"term":{"age": 20}}]
}
}
}'
查询结果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [
{
"_index" : "company",
"_type" : "emp",
"_id" : "9",
"_score" : 0.0,
"_source" : {
"name" : "zhaoqi",
"age" : 20,
"sex" : "woman",
"birthday" : "1996-01-02",
"hobby" : "i like compute game"
}
}
]
}
}