数据准备
单字段条件查询(term)
//根据name查询
1.sql示例
select * from user where name="wxx"
2.es查询
GET index3/_search
{
"query": {
"term": {
"name": {
"value": "wxx"
}
}
}
}
单字段条件查询(match)
//根据name查询
1.sql示例
select * from user where address="抚州"
2.es查询
GET index3/_search
{
"query": {
"match": {
"address": {
"value": "抚州"
}
}
}
}
说明:term和match区别是一个是不支持分词,一个支持分词,term不支持分词,match只要能匹配上分词就匹配上了 (比如:江西抚州,会被分词为 江 西 抚 州 其实我查询value只要也分词匹配其中一个即可)
单字段条件批量查询
//根据name查询
1.sql示例
select * from user where name in ("wxx","zhangxianbo")
2.es查询
GET index3/_search
{
"query": {
"terms": {
"name": [
"zhangxianbo",
"wxx"
]
}
}
}
单字段条件范围查询
//根据name查询
1.sql示例
select * from user where age between 10 and 26
2.es查询
GET index3/_search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 26
}
}
}
}
多字段并集查询
//根据名称和年龄 or 查询
1.sql示例
select * from user where name="wxx" or age =26
2.es查询
GET index3/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"name": {
"value": "wxx"
}
}
},
{
"term": {
"age": {
"value": "26"
}
}
}
]
}
}
}
多字段交集查询
//根据名称和年龄 and 查询
1.sql示例
select * from user where name="liujing" and age =18
2.es查询
GET index3/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"name": {
"value": "liujing"
}
}
},
{
"term": {
"age": {
"value": "18"
}
}
}
]
}
}
}
多字段并集复杂查询
//满足某个性别,或者 满足某个名字和年龄
1.sql示例
select * from user where (sex=wman) or (name=wangjian and age=26)
2.es查询
GET index3/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"sex": {
"value": "wman"
}
}
},
{
"bool": {
"must": [
{
"term": {
"name": {
"value": "wangjian"
}
}
},
{
"term": {
"age": {
"value": "26"
}
}
}
]
}
}
]
}
}
}
多字段交集复杂查询
//满足某个性别,并且 满足某个名字或者年龄
1.sql示例
select * from user where (sex=man) and (name=wangjian or name=zhangxianbo or age=18)
2.es查询
GET index3/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"sex": {
"value": "man"
}
}
},{
"bool": {
"should": [
{
"term": {
"name": {
"value": "wangjian"
}
}
},
{
"term": {
"name": {
"value": "zhangxianbo"
}
}
},
{
"term": {
"age": {
"value": "18"
}
}
}
]
}
}
]
}
}
}
查询精准度控制
//查询用户喜欢水果,至少匹配三个
POST index3/_search
{
"query": {
"match": {
"like_fruits": {
"query": "apple banana pear cherry"
, "minimum_should_match": 3 //可使用百分比
}
}
}
}
//使用 should must,must_not也能实现
POST index3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"like_fruits": "apple"
}
},
{
"match": {
"like_fruits": "banana"
}
},
{
"match": {
"like_fruits": "pear"
}
},
{
"match": {
"like_fruits": "cherry"
}
}
],
"minimum_should_match": 3
}
}
}
//使用boost给参与的指标打分,权重越高,计算总分越高
POST index3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"like_fruits": {
"query": "apple"
}
}
},
{
"match": {
"like_fruits": {
"query": "banana"
, "boost": 5
}
}
},{
"match": {
"like_fruits": {
"query": "pear"
}
}
},{
"match": {
"like_fruits": {
"query": "cherry"
}
}
}
]
, "minimum_should_match": 3
}
}
}
**说明: 很明显,匹配精准度肯定只是考虑分词情况,所以使用match,作为搜索引擎,这个是es特有的**
总结
1.单字段查询相对简单,批量查询相当于in,范围查询相当于between and
2.should,must,must_not等可用于or and操作
3.bool里面嵌套should,must,must_not
4.bool里面should,must之类不能再嵌套should,must,而是要用bool进行嵌套should,must
5.精准搜索控制,是es特有,也是搜索引擎特性