ElasticSearch系列之搜索篇-条件查询,表达式搜索,全文搜索,高亮查询

上一篇:ElasticSearch系列之基础入门篇

和其他关系型数据库一样,我们经常操作数据库是对数据库的查询操作,那么查询在ES中是非常重要的,下面将针对查询做以下操作。

接:ElasticSearch系列之基础入门篇
接下来我们多插入几条数据,通过筛选条件查询出数据。

数据准备

get请求:http://192.168.254.128:9200/my_index_one/employee/2

{
    "first_name" : "Michael",
    "last_name" :  "jack",
    "age" :        20,
    "about" :      "I like reading",
    "interests": [ "movie", "book" ]
}

get请求:http://192.168.254.128:9200/my_index_one/employee/3

{
    "first_name" : "Tom",
    "last_name" :  "Smith",
    "age" :        21,
    "about" :      "I like running",
    "interests": [ "movie", "book" ]
}

数据插入完之后我们开始使用条件查询。

搜索

搜索查询所有数据

ES默认情况下只返回10条数据。

get请求:http://192.168.254.128:9200/my_index_one/employee/_search
搜索结果:

{
    "took": 446,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "first_name": "Tom",
                    "last_name": "Smith",
                    "age": 21,
                    "about": "I like running",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "first_name": "Michael",
                    "last_name": "jack",
                    "age": 20,
                    "about": "I like reading",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            }
        ]
    }
}

条件查询

我们通过一个URL参数来传递查询信息给搜索接口

Get请求:http://192.168.254.128:9200/my_index_one/employee/_search?q=last_name:Smith

查询结果:返回结果给出了所有的 Smith

{
    "took": 783,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.53899646,
        "hits": [
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "1",
                "_score": 0.53899646,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "3",
                "_score": 0.53899646,
                "_source": {
                    "first_name": "Tom",
                    "last_name": "Smith",
                    "age": 21,
                    "about": "I like running",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            }
        ]
    }
}

查询条件可以放在url中,当然也可以放在body体中查询。

使用查询表达式搜索

get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:

 { "query" : { "match" : { "last_name" : "Smith" } } }

返回结果同上。

使用过滤器 filter搜索

get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:

{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 20 } 
                }
            }
        }
    }
}

查询出年龄大于20岁的数据(不包含20)
查询结果:

{
    "took": 27,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.53899646,
        "hits": [
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "1",
                "_score": 0.53899646,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "3",
                "_score": 0.53899646,
                "_source": {
                    "first_name": "Tom",
                    "last_name": "Smith",
                    "age": 21,
                    "about": "I like running",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            }
        ]
    }
}

全文搜索

get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:

{
    "query" : {
        "match" : {
            "about" : "I like"
        }
    }
}

搜索about中带有I like的数据
查询结果:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.40211007,
        "hits": [
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "3",
                "_score": 0.40211007,
                "_source": {
                    "first_name": "Tom",
                    "last_name": "Smith",
                    "age": 21,
                    "about": "I like running",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "2",
                "_score": 0.40211007,
                "_source": {
                    "first_name": "Michael",
                    "last_name": "jack",
                    "age": 20,
                    "about": "I like reading",
                    "interests": [
                        "movie",
                        "book"
                    ]
                }
            },
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "1",
                "_score": 0.06836608,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            }
        ]
    }
}

看看结果是不是很疑惑?明明搜索的是I like,怎么I love也搜索出来了?
其实Elasticsearch 是默认按照相关性得分进行排序的,即每个文档跟查询的匹配程度。第一和第二个最高得分的结果很明显:Tom Smith 和Michael jack的 about 属性清楚地写着 “I like” 。
但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里也有 “I” 。因为只有 “I” 而没有 “like” ,所以她的相关性得分低于 Tom和Michael的。

高亮搜索

我们经常使用搜索引擎查询资料,在返回结果中,匹配我们查询数据的会有高亮显示,当然ES也很容易实现这种效果。
在执行查询前,加一个highlight 就可以了:
get请求:http://192.168.254.128:9200/my_index_one/employee/_search

请求体:

{
    "query" : {
        "match_phrase" : {
            "about" : "running"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

返回结果:

{
    "took": 346,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.9395274,
        "hits": [
            {
                "_index": "my_index_one",
                "_type": "employee",
                "_id": "3",
                "_score": 0.9395274,
                "_source": {
                    "first_name": "Tom",
                    "last_name": "Smith",
                    "age": 21,
                    "about": "I like running",
                    "interests": [
                        "movie",
                        "book"
                    ]
                },
                "highlight": {
                    "about": [
                        "I like <em>running</em>"
                    ]
                }
            }
        ]
    }
}

可以看到返回结果里多了一个highlight节点,而节点里面的running单词是被高亮包裹的。

下一篇:ElasticSearch系列之可视化管理工具篇-elasticsearch-head-master

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silence-wen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值