elasticsearch headnested返回值能返回nested alone吗

Elasticsearch排序 - Ghost Stories - CSDN博客
Elasticsearch排序
elasticsearch
当你发送请求至elasticsearch,返回文档默认按文档得分降序排序,这是通常我们想要了,然而,有时候我们希望能改变这种排序方式
下面的例子就容易做到:
该查询会返回所有在title字段上至少命中一个词项的文档,并且基于section数据排序
也可以通过添加查询sort部分的missing属性为那些section字段缺失的文档定制排序行为
基于多值字段的排序
某些文档的release-dates字段里面储存了多个电影上映日期(同一部电影在不同国家的上映日期不同)我们可以构造查询请求:
例子中es将基于每个文档的release_dates字段的最小值进行排序
mode参数可以设置为以上值:
min 按照该字段最小值排序max 按照该字段最大值进行排序avg 按照多个字段平均值排序sum 按照字段总和进行排序
后面两个选项只对数值类型有效
基于多值geo字段的排序
es提供了基于多维坐标系数据的排序,我们通过一个实例了解这种类型的排序
例如,要查找特点国家里离自己最近的一个机构
我们使用下面的这个映射:
查询如图:
查询结果:
我们可以看到,返回结果包含这个值:“sort”:[0.0]这是因为返回文档的地理坐标和查询中的坐标精确匹配
还可以设置mode属性为max,min,avg
例如avg代表:此时基于字段中的地理位置坐标与查询坐标的距离的均值排序
基于嵌套对象的排序
继续嵌套对象的排序,对以下两种情形都适用:
适用了显式嵌套映射(在映射中配置type=“nested”)的文档使用了对象类型的文档
两者之间的一些细微区别需要注意
假设我们索引如下数据:
查询返回结果按照嵌套对象的usert字段最小值降序
如果将子文档视为一种数据类型,则可以将查询简化为如下形式:
当我们使用对象类型时,可以简化查询,这是因为整个对象结构被当成一个lucene文档进行储存
有些时候,使用nested_path属性会更加便捷
按照下面的方式构造查询:
我们也可以使用nested_filter参数,改参数只对嵌套文档有效,利用这个参数,我们可以在排序前就已经通过一个过滤器在检索期排除了某些文档,而不是检索结果文档集中过滤它们
(注:内容整理自《深入理解Elasticsearch》)
我的热门文章2140人阅读
elasticsearch(7)
& & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &It&is&possible&to&sort&by&the&value&of&a&nested&field,&even&though&the&value&exists&in&a&separate&nested&document.&To&make&the&result&
more&interesting,&we&will&add&another&record:
&&尽管存在于独立的文本内,基于字段的值排序还是可行的。为了让结果更有意思,让我们增加其他的记录:
curl -XPUT 'localhost:9200/my_index/blog/2' -d '
&title&: &Investment secrets&,
&What they don't tell you ...&,
[ &shares&, &equities& ],
&comments&: [
&Mary Brown&,
&comment&: &Lies, lies, lies&,
&John Smith&,
&comment&: &You're making it up!&,
& & & & & & &Imagine&that&we&want&to&retrieve&blog&posts&that&received&comments&in&October,&ordered&by&the&lowest&number&of&stars&that&each&blog
&post&received.&The&search&request&would&look&like&this:
设想我们想要检索在月份被评论的博客文章,同时按每篇文章收到的最低星级排序。检索请求应该类似如下:
curl -XPUT 'localhost:9200/_search' -d '
&nested&:{
&path&:&comments&,
&filter&:{
&comments.date&:{
&comments.stars&:{
&order&:&asc&,
&mode&: &min&,
&nested_filter&:{
&comments.date&:{
& & & & & &Why&do&we&need&to&repeat&the&query&conditions&in&the&nested_filter?&The&reason&is&that&sorting&happens&after&the&query&has&been&executed.&
The&query&matches&blog&posts&that&received&comments&in&October,&but&it&returns&blog&post&documents&as&the&result.&If&we&didn’t&include&the&
nested_filter&clause,&we&would&end&up&sorting&based&on&any&comments&that&the&blog&post&has&ever&received,&not&just&those&received&in&October.
& & & & &为什么我们要在内重复查询条件?原因是排序发生在查询执行结束后。查询匹配月收到评论的博客文章,但是它返回博客文章
文本作为结果。如果我们不包括语块,那么最终会基于返回的博客文章的所有评论进行排序,而不是在月份收到的评分。
(!!!根据自己有限的使用经验,这点非常值得各位读者重视!!!)
原文:http://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:210196次
积分:3046
积分:3046
排名:第12197名
原创:67篇
转载:20篇
译文:16篇
评论:25条
(5)(2)(6)(1)(2)(5)(3)(2)(1)(1)(4)(2)(5)(1)(1)(2)(1)(1)(4)(1)(8)(1)(2)(5)(2)(4)(2)(1)(5)(1)(16)(6)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'Hi,I have tried to update or remove the nested object in the same way assuggested , but this is not workingI am doing thisclient.update({&index&: &daffosw&,&type&: &daffo&,&id&: &2501&,&body&: {&&&&&&script& : &for (int i = 0; i & ctx._source.users.size(); i++){if(ctx._source.users[i]_id == id){ctx._source.users[i].name = 'Updated John';}}&,&&&&&&params& : {&&&&&&&&&&&&id& : &1&&&&&&}}}my users field contain the following&&&users&: [&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&code&: &abcd&,&&&&&&&&&&&&&&&&_id&: &1&,&&&&&&&&&&&&&&&&name&: &naveen&&&&&&&&&&&&&&},&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&code&: &efgh&,&&&&&&&&&&&&&&&&_id&: &2&,&&&&&&&&&&&&&&&&name&: &rajit&&&&&&&&&&&&&&},&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&code&: &ijkl&,&&&&&&&&&&&&&&&&_id&: &3&,&&&&&&&&&&&&&&&&name&: &ashu&&&&&&&&&&&&&&}&&&&&&&&&&&]and I am getting this errorVerifyError[Bad type on operand stackException Details:&&&Location:&&&&&ASMAccessorImpl_.setValue(Ljava/lang/OLjava/lang/OLorg/elasticsearch/common/mvel2/integration/VariableResolverFLjava/lang/O)Ljava/lang/O @49: invokeinterface&&&Reason:&&&&&Type 'java/lang/Object' (current frame, stack[1]) is not assignable to integer&&&Current Frame:&&&&&bci: @49&&&&&flags: { }&&&&&locals: { 'ASMAccessorImpl_', 'java/lang/Object', 'java/lang/Object', 'org/elasticsearch/common/mvel2/integration/VariableResolverFactory', 'java/lang/Object' }&&&&&stack: { 'java/util/List', 'java/lang/Object' }&&&Bytecode:&&&&&d12 0eb9
b900 1a01 00c0 001c&&&&&1e b900
001c 02&&&&&c0
b900 1a01&&&&&b9 002b d12 2f19 04b6 0035&&&&&04 b0]Error: VerifyError[Bad type on operand stackException Details:&&&Location:----------------------------------------------------------------------------------On Friday, April 12, :33 AM UTC+5:30, Martijn v Groningen wrote:Hi Andrei,I think the following should work:&script& : &for (int i = 0; i & ctx._source.users.size();i++){if(ctx._source.users[i]._id == id){ctx._source.users.remove(i);i--;}}&I added: 'i--'. Once an entry is removed, the list has been shorter and weshouldn't skip over the next entry.MartijnOn 11 April , Andrei Tolnai &dre...@ &javascript:&&wrote:Oddly enough the DELETE script seems to be working if in the array thereis only one user with a particular given *id*. :-)If I have multiple users with the same *_id* value the script is notremoving all of them. How should the script be changed to remove all theusers that have a specific *_id* value?Regards.--Met vriendelijke groet,Martijn van Groningen--You received this message because you are subscribed to the Google Groups &elasticsearch& group.To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+.To view this discussion on the web visit For more options, visit
Search Discussions
Discussion Posts
Follow ups
Related Discussions
view | post
| 9 of 12 |
categories
user style
5 users in discussion
site design / logo & 2017 GrokbaseJava client --& EC2 --& ES
8 messages
Open this post in threaded view
Java client --& EC2 --& ES
Hi,I have set up ES server in AWS, and it seems to be working fine. I am trying to connect programmatically to this server from my java client, by creating a node (java application is running outside the cloud). However, I am getting binding error :&&It this possible? Please let me know if I am doing something wrong.My Java node object is created with following attributes:cluster.name : &nameOfCluster&network.host : &public DNS name of AWS Instance:9300& (In server, I have configured this name with private IP of server)discovery.type : ec2cloud.account : &account&cloud.key:&key&
Open this post in threaded view
Re: Java client --& EC2 --& ES
Hello Achaayan,Remove :9300 part from network.hostRegards,Alexandr Vasilenko achaayan &&
Hi,I have set up ES server in AWS, and it seems to be working fine. I am trying to connect programmatically to this server from my java client, by creating a node (java application is running outside the cloud). However, I am getting binding error :
It this possible? Please let me know if I am doing something wrong.My Java node object is created with following attributes: : &nameOfCluster&
network.host : &public DNS name of AWS Instance:9300& (In server, I have configured this name with private IP of server)discovery.type : ec2cloud.account : &account&
cloud.key:&key&
Open this post in threaded view
Re: Java client --& EC2 --& ES
Thanks, Alex. I have tried this, but I get the following error:&:& & & & Invocation o nested exception is org.elasticsearch.transport.BindTransportException: Failed to bind to []I have opened all necessary ports in EC2.
Open this post in threaded view
Re: Java client --& EC2 --& ES
Probably something is already bound to these ports. Check something like lsof -iAlexandr Vasilenko achaayan &&
Thanks, Alex. I have tried this, but I get the following error: :
Invocation o nested exception is org.elasticsearch.transport.BindTransportException: Failed to bind to []
I have opened all necessary ports in EC2.
Open this post in threaded view
Re: Java client --& EC2 --& ES
Administrator
In reply to
by achaayan
If you connect to your cluster on AWS from outside AWS, use transport client, with sniff disabled (its the default).On Fri, Nov 18, 2011 at 2:27 PM, achaayan && wrote:
Thanks, Alex. I have tried this, but I get the following error: :
Invocation o nested exception is org.elasticsearch.transport.BindTransportException: Failed to bind to []
I have opened all necessary ports in EC2.
Open this post in threaded view
Re: Java client --& EC2 --& ES
I have tried this, but I get an exception:
org.elasticsearch.transport.RemoteTransportException: Failed to
deserialize response of type
[org.elasticsearch.action.admin..NodesInfoResponse]
Caused by:
org.elasticsearch.transport.TransportSerializationException: Failed to
deserialize response of type
[org.elasticsearch.action.admin..NodesInfoResponse]
& & & & at
org.elasticsearch.transport.netty.MessageChannelHandler.handleResponse(MessageChannelHandler.java:
& & & & at
org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:
& & & & at
mon.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:
It would be great if you could provide me some directions.
On Nov 20, 1:27 pm, Shay Banon && wrote:
& If you connect to your cluster on AWS from outside AWS, use transport
& client, with sniff disabled (its the default).
& On Fri, Nov 18, 2011 at 2:27 PM, achaayan && wrote:
& & Thanks, Alex. I have tried this, but I get the following error: :
Invocation o nested exception is
& & org.elasticsearch.transport.BindTransportException: Failed to bind to
& & I have opened all necessary ports in EC2.
Open this post in threaded view
Re: Java client --& EC2 --& ES
Administrator
Answered on another thread you opened, I think you use different versions of elasticsearch.On Mon, Nov 21, 2011 at 7:39 PM, achaayan && wrote:
I have tried this, but I get an exception:
org.elasticsearch.transport.RemoteTransportException: Failed to
deserialize response of type
[org.elasticsearch.action.admin..NodesInfoResponse]
Caused by:
org.elasticsearch.transport.TransportSerializationException: Failed to
deserialize response of type
[org.elasticsearch.action.admin..NodesInfoResponse]
org.elasticsearch.transport.netty.MessageChannelHandler.handleResponse(MessageChannelHandler.java:
org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:
mon.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:
It would be great if you could provide me some directions.
On Nov 20, 1:27 pm, Shay Banon && wrote:
& If you connect to your cluster on AWS from outside AWS, use transport
& client, with sniff disabled (its the default).
& On Fri, Nov 18, 2011 at 2:27 PM, achaayan && wrote:
& & Thanks, Alex. I have tried this, but I get the following error: :
Invocation o nested exception is
& & org.elasticsearch.transport.BindTransportException: Failed to bind to
& & I have opened all necessary ports in EC2.
Open this post in threaded view
Re: Java client --& EC2 --& ES
Thanks, Shay - changing client jar version fixed this problem. Both
threads described same issues, but on different scenarios.
On Nov 22, 5:39 pm, Shay Banon && wrote:
& Answered on another thread you opened, I think you use different versions
& of elasticsearch.
& On Mon, Nov 21, 2011 at 7:39 PM, achaayan && wrote:
& & Thanks.
& & I have tried this, but I get an exception:
& & org.elasticsearch.transport.RemoteTransportException: Failed to
& & deserialize response of type
& & [org.elasticsearch.action.admin..NodesInfoResponse]
& & Caused by:
& & org.elasticsearch.transport.TransportSerializationException: Failed to
& & deserialize response of type
& & [org.elasticsearch.action.admin..NodesInfoResponse]
& & org.elasticsearch.transport.netty.MessageChannelHandler.handleResponse(Mess ageChannelHandler.java:
& & org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(Mes sageChannelHandler.java:
& & mon.netty.channel.SimpleChannelUpstreamHandler.handleU pstream(SimpleChannelUpstreamHandler.java:
& & It would be great if you could provide me some directions.
& & On Nov 20, 1:27 pm, Shay Banon && wrote:
& & & If you connect to your cluster on AWS from outside AWS, use transport
& & & client, with sniff disabled (its the default).
& & & On Fri, Nov 18, 2011 at 2:27 PM, achaayan && wrote:
& & & & Thanks, Alex. I have tried this, but I get the following error: :
Invocation o nested exception is
& & & & org.elasticsearch.transport.BindTransportException: Failed to bind to
& & & & []
& & & & I have opened all necessary ports in EC2.elasticsearch中关系的处理 - 简书
elasticsearch中关系的处理
Neil Zhu,简书ID Not_GOD,University AI 创始人 & Chief Scientist,致力于推进世界人工智能化进程。制定并实施 UAI 中长期增长战略和目标,带领团队快速成长为人工智能领域最专业的力量。
作为行业领导者,他和UAI一起在2014年创建了TASA(中国最早的人工智能社团), DL Center(深度学习知识中心全球价值网络),AI growth(行业智库培训)等,为中国的人工智能人才建设输送了大量的血液和养分。此外,他还参与或者举办过各类国际性的人工智能峰会和活动,产生了巨大的影响力,书写了60万字的人工智能精品技术内容,生产翻译了全球第一本深度学习入门书《神经网络与深度学习》,生产的内容被大量的专业垂直公众号和媒体转载与连载。曾经受邀为国内顶尖大学制定人工智能学习规划和教授人工智能前沿课程,均受学生和老师好评。
handling relatioships
现实世界里,关系(relationship)是尤其重要的:博客文章包含评论,银行账号有相应的交易,顾客有银行账户,订单也由订单线,而目录则包含文件和子目录。
关系数据库便以此而设计——下面这些描述对你来说也并不陌生:
每个实体(entity,或者行 row)可以由一个主键唯一识别
实体都是正规化了的。对唯一实体的数据只会存储以此,相关的实体则只需要存储其主键。改变实体的数据只会出现在一个地方
实体在查询的时候可以被join从而支持多个实体的交叉查询
大多数的关系型数据库支持在多个实体上的ACID事务
但是关系型数据库除了不支持全文检索外还拥有自身的缺陷。在查询时进行join常常耗资源。依赖不同的硬件执行实体的join不实用。这也就给存放在一个服务器上的数据量造成了一个限制。
ES,如同大多数的NoSQL数据库,就将显示世界看做是扁平的。index 就是独立的文档的扁平集合。单一的文档应当包含确定其被搜索请求命中所需的信息。
改变ES中的单一文档的数据是ACID的,包含多个文档的事务却不是。没有办法使得可以保证事务的正常回滚。
扁平世界的优点;
索引快速且无锁
搜索快速且无锁
海量数据可以分布在若干节点上,因为每个文档都是独立于其他文档。
然而关系也是重要的。我们需要消除扁平世界和真实世界的隔阂。在 ES 中,四种通用的技术用来管理关系型数据:
Application-side joins
Data denormalization
Nested objects
Parent/Child relationships
而最终的解决方案需要这些技术的混合。
Application-side joins
我们可以部分地通过在应用中实现 join 模拟关系型数据库。例如,我们在索引用户和用户的博客文章。在关系型世界中,我们可以做下面的动作:
PUT /my_index/user/1
{ "name": "John Smith",
"email": "",
PUT /my_index/blogpost/2
{ "title": "Relationships",
"body": "It's complicated...",
index, type, id整体作为主键
blogpost通过存放用户的 id与用户相连。index和type 因为他们在应用中硬编码所以并没有强制。
在博客文章中查询用户ID为1的就很简单了:
GET /my_index/blogpost/_search
"query": {
"filtered": {
"filter": {
"term": { "user": 1 }
查询用户名是 John 的博客文章,我们需要运行两个查询:第一个查找所有叫做John的用户得到他们的ID,第二步将这些ID传入一个查询中获得作者为John的文章
GET /my_index/user/_search
{ "query": { "match": { "name": "John" } }}
GET /my_index/blogpost/_search
{ "query": { "filtered": { "filter": { "terms": { "user": [1] }
在terms过滤器中的值就是从第一个查询中得到的结果。
application-side join的主要好处就是数据的正规化。改变用户的名字只会在一个地方:user文档。而其弱点就是你需要执行额外的查询在搜索时进行join。
这个例子中,只有一个用户匹配了我们第一查询,但是在现实情形下,我们常常会碰到百万个以John为名的用户。包含所有这些 ID 在第二个查询中就产生了一个巨大的查询,包含数百万的term检索。
这个场景适用于第一个搜索结果比较小的情形,并且最好他们基本不变化。这就使得ES可以缓存结果避免频繁执行第一个查询。
Data denormalization
从ES获得最佳搜索性能的方法就是通过在索引时的去正规化数据。对每个需要获取的文档保持冗余的拷贝将会去除join的需要。
如果我们希望通过作者名找到博客文章,包含这个用户的名字在该博客文章文档本身即可:
PUT /my_index/user/1
"John Smith",
PUT /my_index/blogpost/2
"Relationships",
"It's complicated...",
"name": "John Smith"
现在,我们既可以通过单个查询找到作者为John的文章了。
GET /my_index/blogpost/_search
"query": {
{ "match": { "title":
"relationships" }},
{ "match": { "user.name": "John"
data denormalization 的优势是速度。因为每个文档包含需要确定是否满足查询的所有的信息,也就避免了额外的 join。
Field collapsing
一般要求是用一个特定的字段的 group 来展现搜索结果。我们可能希望返回最相关的博客文章,而使用用户名进行group。根据用户名进行group就代表着对 terms 聚合的要求。为了对用户的全名进行 group,name 字段就应被设置成 not_analyzed 形式,正如在
中解释的那样:
PUT /my_index/_mapping/blogpost
"properties": {
"properties": {
"type": "string",
"fields": {
"index": "not_analyzed"
user.name 字段用作全文检索
user.name.raw 字段用作terms聚合
然后添加一些数据:
PUT /my_index/user/1
"name": "John Smith",
"email": "",
PUT /my_index/blogpost/2
"title": "Relationships",
"body": "It's complicated...",
"name": "John Smith"
PUT /my_index/user/3
"name": "Alice John",
"email": "",
PUT /my_index/blogpost/4
"title": "Relationships are cool",
"body": "It's not complicated at all...",
"name": "Alice John"
现在我们就可以执行一个查询relationships的博客文章了,作者名为John,并使用作者名进行group,使用
GET /my_index/blogpost/_search?search_type=count
"query": {
{ "match": { "title":
"relationships" }},
{ "match": { "user.name": "John"
"users": {
"terms": {
"user.name.raw",
"order": { "top_score": "desc" }
"top_score": { "max":
{ "script":
"blogposts": { "top_hits": { "_source": "title", "size": 5 }}
我们感兴趣的博客文章返回在 blogposts 聚合,所以我们可以通过设置 search_type=count 来关闭常用的搜索 hits.
query 返回用户为 John 的关于 relationships 博客文章
terms 聚合对每个 user.name.raw 值创建了一个桶
top_score 聚合将在 users 聚合中的项进行按桶的排序
top_hits 聚合返回对每个用户前5个相关文章
上面查询的简化结果如下:
"max_score": 0,
"aggregations": {
"users": {
"buckets": [
"John Smith",
"doc_count": 1,
"blogposts": {
"max_score": 0.,
"_index": "my_index",
"blogpost",
"_score": 0.,
"_source": {
"title": "Relationships"
"top_score": {
"value": 0.7427
hits 数组为空,因为我们设置了 search_type = count
对每个用户都有一个桶
在每个用户的桶下面都有一个blogposts.hits的数组包含了对那个用户的前列的结果
在每个用户的桶内是按照相关度进行排序的
使用top_hits聚合等价于执行查询返回用户的名字以及相应最为相关的博客文章,接着对每个用户执行同样地查询,来获得相应最相关的博客文章。但是更加高效。
在每个桶中返回的前面几条记录是执行一个基于原初主查询的微型查询的结果。这个微查询支持常用的特征,可以通过高亮和分页的特征。
Nested objects
Parent/Child relationships
我是 Neil,简书ID Not_GOD,University AI 创始人 & Chief Scientist,致力于推进世界人工智能化进程。制定并实施 UAI 中长期增长战略和目标,带领团队快速成长为人工智能领域最专业的力量。
作为行业领导者,UAI一起在2014年创建了TASA(中国最早的人工智能社团), DL Center(深度学习知识中心全球价值网络),AI growth(行业智库培训)等,为中国的人工智能人才建设输送了大量的血液和养分。此外,我还参与或者举办过各类国际性的人工智能峰会和活动,产生了巨大的影响力,书写了60万字的人工智能精品技术内容,生产翻译了全球第一本深度学习入门书《神经网络与深度学习》,生产的内容被大量的专业垂直公众号和媒体转载与连载。曾经受邀为国内顶尖大学制定人工智能学习规划和教授人工智能前沿课程,均受学生和老师好评。
大家对我们的事业感兴趣可以通过微信联系我。}

我要回帖

更多关于 elasticsearch nested 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信