之前的查询直接指定id,这里的查询只是输入条件
先来一个没有条件的查询:
返回的结果很容易理解,hits的返回的文档数组,_source是文档内容,_score是这此query中该文档的得分,得分越高越靠前。
若user索引中文档有1w个,都返回吗?当然不是,es默认返回10条,也可以自己控制:
{
"from":1,
"size":1
}
上面的结果是查询第2页,每页1条(mysql limit一模一样)
空查询等同于在请求体中添加如下json:
{
"query": {
"match_all": {}
}
}
加上一个查询条件:
{
"query": {
"match": {
"first_name":"Douglas"
}
}
}
多个查询条件如何传参呢?使用bool把多个条件嵌套:
{
"query":{
"bool": {
"must": { "match": { "first_name": "Douglas" }},
"must_not": { "match": { "last_name": "mary" }},
"should": { "match": { "about": "abc" }},
"filter": { "range": { "age" : { "gt" : 30 }} }
}
}
}
bool查询里面可以有下面几种key
must:必须满足
must_not:必须不满足
should:满足则加分(影响排名)
filter:不满足则过滤掉
进一步了解查询前先看一下映射:
put操作后es会帮助你把字段做映射,就是识别这个字段类型,string、object、date、integer等等,让我们先put一个user
查看映射:GET http://localhost:9200/user/_mapping/doc
age->long birthday->date happy->