Elasticsearch-搜索并获取数据
Elasticsearch-搜索并获取数据
在group中搜索elasticsearch
curl -XGET "localhost:9200/get-together/group/_search?\
> q=elasticsearch\
> &fields=name,location\
> &size=\
> $pretty"
URL指出在何处进行查询:在get-together索引的group类型中
URI参数给出了搜索的细节:发现包含“elasticsearch”的文档,但是只返回排名靠前结果的name和location字段
1.在哪里搜索
可以告诉ES在特定的类型和特定索引中进行查询,但是也可以在同一个索引的多个字段中搜索、在多个索引中搜索或是在所有的索引中搜索。
(1).在多个类型中搜索,使用逗号分隔的列表。如:同时在group和event类型中搜索
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together/group,event/_search?q=elasticsearch&pretty"
(2).通过向索引URL的_search端点发送请求,可以在某个索引的多个类型中搜索
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together/_search?q=elasticsearch&pretty"
(3).和类型类似,为了在多个索引中搜索,用逗号分隔它们
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together,other_index/_search?q=elasticsearch&pretty"
如果事先没有创建other-index,这个特定的请求将会失败。为了忽略这种问题,可以像pretty旗标那样添加ignore_unavailable旗标。
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together,other_index/_search?q=elasticsearch&ignore_unavailable&pretty"
(4).在所有的索引中搜索,彻底省略索引的名称
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/_search?q=elasticsearch&ignore_unavailable&pretty"
如果需要在所有索引内搜索,也可以使用名为_all的占位符作为索引的名称。当需要在全部索引中的同一个单独类型中进行搜索时,这一点就派上用场了,如
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/_all/event/_search?q=elasticsearch&ignore_unavailable&pretty"
2.回复的内容
除了和搜索条件匹配的文档,搜索答复还包含其他有价值的信息,用于检验搜索的性能或结果的相关性。
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together/group/_search?q=Test&ignore_unavailable&pretty"
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_score" : 0.15342641,
"_source" : {
"name" : "ES Test",
"organizer" : "Feng"
}
} ]
}
}
解析返回结果
"took" : 24,
"timed_out" : false,
请求耗时多久,以及它是否超时
"total" : 5,
"successful" : 5,
"failed" : 0
查询了多少分片
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
所有匹配文档的统计数据
"hits" : [ {
"_index" : "get-together",
"_type" : "group",
"_id" : "1",
"_score" : 0.15342641,
"_source" : {
"name" : "ES Test",
"organizer" : "Feng"
}
} ]
结果数组
(1)时间
"took" : ,
"timed_out" : false,
其中took字段告诉ES花了多久处理请求,时间单位是毫秒,而time_out字段表示搜索请求是否超时。默认情况下,搜索永远不会超时,但是可以通过timeout参数来设定限制。如:设置3秒超时
FengZhendeMacBook-Pro:bin FengZhen$ curl "localhost:9200/get-together/group/_search?q=Test&ignore_unavailable&pretty&timeout=3s"
如果搜索超时了,timed_out字段的值就是true,而且只能获得超时前所收集的结果
(2).分片
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
在一个拥有5份分片的索引中搜索,所有的分片都有返回,所以成功(successful)的值是5,而失败(failed)的值是0.
当一个节点宕机而且一份分片无法回复搜索请求时,ES提供正常分片中的结果,并在failed字段中报告不可搜索的分片数量。
(3).命中统计数据
回复的最后一项组成元素是hits,它包含了匹配文档的数组。在数组之前,包含了几项统计信息
"total" : ,
"max_score" : 0.15342641,
将看到匹配文档的总数,而且通过max_score会看到这些匹配文档的最高得分
搜索返回的文档得分,是该文档和给定搜索条件的相关性衡量。得分默认是通过TF-IDF(词频-逆文档频率)算法进行计算的。词频意味着对于搜索的每个词条,其在某篇文档中出现的次数越多则该文档的得分就越高。逆文档频率意味着,如果该词条在整个文档集合中出现在越少的文档中则该文档得分越高,原因是我们会认为词条和这篇文档的相关度更高。如果词条经常在其它文档中出现,他可能是一个常见词,相关性更低。
文档的总数和回复中的文档数量可能不匹配,因为ES默认返回10篇文档,可以使用size参数来修改返回的结果数量。
(4)结果文档
"hits" : [ {
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_score" : 0.15342641,
"_source" : {
"name" : "ES Test",
"organizer" : "Feng"
}
} ]
展示了每个匹配文档所属的索引和类型、它的ID和它的得分,若在查询时没有通过fields指定查询的字段,则会展示_source字段。和_all一样,_source是一个特殊的字段,ES默认在其中存储原始的JSON文档。
指定fields的查询:
curl "localhost:9200/get-together/group/_search?q=Test&fields=name,location&ignore_unavailable&pretty"
"hits" : [ {
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_score" : 0.15342641,
"fields" : {
"name" : [ "ES Test" ]
}
} ]
3.如何搜索
ES允许使用JSON格式指定所有的搜索条件。当搜索变得越来越复杂的时候,JSON更容易读写,并且提供了更多的功能。
FengZhendeMacBook-Pro:bin FengZhen$ curl 'localhost:9200/get-together/group/_search?pretty' -d '{
"query":{
"query_string":{
"query":"Test"
}
}
}'
运行一个类型为query_string的查询,字符串内容是Test
(1).设置查询的字符串选项
ES默认查询_all字段。如果想在分组的名称里查询,需要指定:
“default_field”:”name”
同样,ES默认返回匹配了任一指定关键词的文档(默认的操作符是OR)。如果希望匹配所有的关键词,需要指定:
“default_operator”:”AND”
修改后的查询:
FengZhendeMacBook-Pro:bin FengZhen$ curl 'localhost:9200/get-together/group/_search?pretty' -d '{
"query":{
"query_string":{
"query":"ES san francisco",
“default_field”:”name”,
“default_operator”:”AND”
}
}
}'
获取同样结果的另一种方法是查询字符串中指定字段和操作符
“query”:”name:ES AND name:san AND name:francisco”
(2).选择合适的查询类型
如果在name字段中查找“Test”一个词,term查询可能更快捷、更直接
FengZhendeMacBook-Pro:bin FengZhen$ curl 'localhost:9200/get-together/group/_search?pretty' -d '{
"query":{
"term":{
"name":"Test"
}
}
}'
(3).使用过滤器
如果对得分不感兴趣,可以使用过滤查询来替代。过滤只关心一条结果是否匹配搜索条件,因此,对比相应的查询,过滤查询更为快速而且更容易缓存。
FengZhendeMacBook-Pro:bin FengZhen$ curl 'localhost:9200/get-together/group/_search?pretty' -d '{
"query":{
“filtered”:{
“filter”:{
"term":{
"name":"Test"
}
}
}
}
}'
返回的结果和同样词条的查询相同,但是结果没有根据得分来排序(因为所有的结果得分都是1.0)
(4).应用聚集
除了查询和过滤,还可以通过聚集进行各种统计。词条聚集(terms aggregation)。这会展示指定字段中出现的每个词的计数器。
curl 'localhost:9200/get-together/group/_search?pretty' -d '{
"aggregations":{
"organizers":{
"terms":{"field":"organizer"}
}
}
}'
聚集解释:给我一个名为organizers的聚集,类型是terms,并且查找organizers字段
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 1.0,
"hits" : [ {
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_score" : 1.0,
"_source" : {
"name" : "Din",
"organizer" : "DinDin"
}
}, {
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_score" : 1.0,
"_source" : {
"name" : "ES Test",
"organizer" : "Feng"
}
} ]
},
"aggregations" : {
"organizers" : {
"doc_count_error_upper_bound" : ,
"sum_other_doc_count" : ,
"buckets" : [ {
"key" : "dindin",
"doc_count" :
}, {
"key" : "feng",
"doc_count" :
} ]
}
}
}
结果表示,”feng”出现了1次,”dindin”出现了一次。
4.通过ID获取文档
为了获取一个具体的文档,必须要知道它所属的索引和类型,以及它的ID。
FengZhendeMacBook-Pro:nacos FengZhen$ curl 'localhost:9200/get-together/group/1?pretty'
{
"_index" : "get-together",
"_type" : "group",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"name" : "ES Test",
"organizer" : "Feng"
}
}
回复包括所指定的索引、类型和ID。如果文档存在,会发现found字段的值是true,此外还有其版本和源。如果文档不存在,found为false。
通过ID获得的文档要比搜索更快,所消耗的资源成本也更低。这也是实时完成的:只要一个索引操作完成了,新的文档就可以通过GET API获取。相比之下,搜索时近实时的,因为它们需要等待默认情况下每秒进行一次的刷新操作。
Elasticsearch-搜索并获取数据的更多相关文章
- Elasticsearch 搜索不到数据问题(_mapping 设置)
需求 由于 kibana3 中,不支持直接在请求的 url 中设置搜索的 type (是不是我不知道???). 为了支持特定 type 的搜索,所以我设置了个下每个 panel 的查询语句,让它增加一 ...
- 使用Python对ElasticSearch获取数据及操作
#!/usr/bin/env python# -*- coding: utf-8 -*-""" @Time : 2018/7/4 @Author : LiuXueWen ...
- Elasticsearch 搜索数据
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- 【ElasticSearch】ES 读数据,写数据与搜索数据的过程
ES读数据的过程: 1.ES客户端选择一个node发送请求,该请求作为协调节点(coordinating node): 2.corrdinating node 对 doc id 对哈希,找出该文档对应 ...
- 用ElasticSearch和Protovis实现数据可视化
搜索引擎最重要的目的,嗯,不出意料就是搜索.你传给它一个请求,然后它依照相关性返回你一串匹配的结果.我们可以根据自己的内容创造各种请求结构,试验各种不同的分析器,搜索引擎都会努力尝试提供最好的结果. ...
- 基于Elasticsearch搜索平台设计
背景 随着公司业务的高速发展以及数据爆炸式的增长,当前公司各产线都有关于搜索方面的需求,但是以前的搜索服务系统由于架构与业务上的设计,不能很好的满足各个业务线的期望,主要体现下面三个问题: 不能支持对 ...
- ElasticSearch搜索介绍四
ElasticSearch搜索 最基础的搜索: curl -XGET http://localhost:9200/_search 返回的结果为: { "took": 2, &quo ...
- 第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中
第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中 前面我们讲到的elasticsearch( ...
- 四十六 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中
前面我们讲到的elasticsearch(搜索引擎)操作,如:增.删.改.查等操作都是用的elasticsearch的语言命令,就像sql命令一样,当然elasticsearch官方也提供了一个pyt ...
- ElasticStack学习(六):ElasticSearch搜索初探
一.ElasticSearch搜索介绍 1.ElasticSearch搜索方式主要分为以下两种: 1).URI Search:此种查询主要是使用Http的Get方法,在URL中使用查询参数进行查询: ...
随机推荐
- Python CGI编程Ⅲ
GET和POST方法 浏览器客户端通过两种方法向服务器传递https://www.xuanhe.net/信息,这两种方法就是 GET 方法和 POST 方法. 使用GET方法传输数据 GET方法发送编 ...
- k8s-强制删除pod
kubectl get deployments --all-namespaces [root@master ~]# kubectl get deployments --all-namespacesNA ...
- js中的“==”与“===”的区别
console(34 == "34") 输出true console(34 === "34") 输出false 因此,== 只判断值,不判断类型,实际上里 ...
- java 后台封装json数据学习总结
一.数据封装 1. List集合转换成json代码 List list = new ArrayList(); list.add( "first" ); list.add( &quo ...
- tp5获取器的用法。
1.命名规则 get + 属性名的驼峰命名+ Attr ------>在相应的model中创建方法 例如: protected function getSexAttr($value ...
- openssl-1.0.1u静态库编译
不管Windows还是linux都是需要安装好perl环境的 Windows步骤 1.解压openssl-1.0.1u.tar.gz 2.使用Vs2005命令行工具进入解压后的目录 3.执行如下命令 ...
- SpringMvc中@PathVariable注解简单的用法
@PathVariable /** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中. * @param id * @return */ jsp页面请求 <a h ...
- 五一 DAY 7
五一 DAY 7 P1514 引水入城 P1311 选择客栈 题解: P1315 观光公交 题解: 设 wait i 为最晚到达的旅客 arrive i 为到达i 的时刻 arrive i =max ...
- 浏览器端-W3School-JavaScript:Location 对象
ylbtech-浏览器端-W3School-JavaScript:Location 对象 1.返回顶部 1. Location 对象 Location 对象 Location 对象包含有关当前 URL ...
- MVC1:.Net MVC Cotroller向View传值
下面介绍 ASP .Net MVC中 Cotroller 向 View 传值 的4中方式: ViewBag,ViewData,TempData,Model. (注:参数可根据需要为复杂类型,只需在应用 ...