方面从弹性搜索中的对象获取所有键 [英] Facet to get all keys from an object in elasticsearch
问题描述
{
title:Some Title,
选项:{
key5:1,
key3:0,
key1:1,
}
},
{
title:Some Title,
options:{
key2:0,
key3:0,
key5 1,
}
}
我想从 options
object using facet。
如果选项
是一个简单的数组的键作为字符串,我会简单地使用这样的方面:
facets:{
/ pre>
options :{
terms:{
field:options
}
}
}
但是在我的情况下不行。
所以如果一个
查询
返回这两个文档,我应该得到这些键:[key5,key3,key1,key2]
我实际需要什么样的方面?
解决方案你不能使用一个方面。
您有2个选项 -
保留当前文档结构,并从类型映射中获取密钥列表(请参阅 http://www.elasticsearch.org/引导/ EN / elasticsearch /参考/电流/指数-GET-mapping.html )。这带来了您遇到的所有字段的类型方案。
更改结构。保持键也作为一个字段,所以你的选项数组成为一系列文件,如:
options:
[
{key:key1,value:1},
{key:key2,value:0}
]
你可能想要保留键的上下文 - 搜索或刻面时的值对,将其配置为嵌套类型( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html )。
然后,您可以在options.key字段上获取顶部键列表。Let's say I have the following docs:
{ "title": "Some Title", options: { "key5": 1, "key3": 0, "key1": 1, } }, { "title": "Some Title", options: { "key2": 0, "key3": 0, "key5": 1, } }
I want to get all the keys from
options
object using facet.If
options
was a simple array of keys as strings, I would simple use a facet like this:"facets" : { "options" : { "terms" : { "field" : "options" } } }
But it doesn't work in my case.
So if a
query
returns those two docs, I should get these keys: ["key5","key3","key1","key2"]What kind of facet do I actually need?
解决方案You can't do that using a facet. You have 2 options -
Keep your current document structure and get the list of keys from the type mapping (see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html). This brings the scheme of your type which holds all the fields encountered.
Change your structure. Keep the key also as a field, so your option array becomes an array of documents like:
"options" :
[
{ "key" : "key1", "value" : 1},
{ "key" : "key2", "value" : 0}
]You probably will want to keep the context of the key-value pairs when searching or faceting so configure it as a nested type (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html). Then you can facet on the "options.key" field to get a list of top keys.
这篇关于方面从弹性搜索中的对象获取所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!