本文介绍了弹性搜索:如何获取3个或多个字段的多个组合的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在弹性搜索中,如果我有文档,请注意 Foo Bar Baz Qux 和一堆其他属性。



我需要以这种方式查询我以下面的方式获得结果



Foo Bar Baz Qux

foo1 bar1 baz1 qux1

foo1 bar1 baz1 qux2

foo1 bar1 baz2 qux1

foo1 bar1 baz2 qux2

foo1 bar2 baz1 qux1



基本上,我需要doc_count可用的所有组合。使用这个结果,可以这么说,使用Foo = foo1,Bar = bar1,Baz = baz1,Qux = qux1,有20个文档/记录



Crude Way是使用术语聚合术语聚合(在这种情况下为4次)。 / p>

应该有一些更简单的方法来做到这一点。



感谢提前

解决方案

你提到的4个嵌入的术语聚合是一个很好的选择。



另一种方法是使用单个术语聚合与脚本创建一个由您要汇总的四个术语组成的假期,如下所示:

  {
大小:0,
aggs:{
组合:{
terms:{
script:[doc.foo.value, bar.value,doc.baz.value,doc.qux.value] .join(',')
}
}
}
}
$ / pre>

注意:请确保,以便使用上述脚本聚合。



另一种解决方案是为了在搜索中节省一些周期时间将是在另一个字段中的索引时间创建该合计术语,然后您可以使用单个术语聚合。



编辑:
如果结果的顺序很重要,并且应该根据 foo


$ b的doc_count进行排序$ b

  {
size:0,
aggs:{
primary:{
:{
field:foo
},
aggs:{
组合:{
条款:{
script:[doc.foo.value,doc.bar.value,doc.baz.value,doc.qux.value] .join(',')
}
}
}
}
}


In Elastic search, If i have documents with attribtutes say Foo Bar Baz Qux and a bunch of other attributes.

I need to query in such a way that i get the results in below manner

Foo Bar Baz Qux
foo1 bar1 baz1 qux1
foo1 bar1 baz1 qux2
foo1 bar1 baz2 qux1
foo1 bar1 baz2 qux2
foo1 bar2 baz1 qux1

Basically, I need all the combinations available with doc_count. Using the result, it can be said that With Foo=foo1, Bar=bar1, Baz=baz1, Qux=qux1, there are 20 documents/records

Crude Way is, to use terms aggregation inside termsaggregation(4 times in this case).

There should be some easier way to do this.

Thanks in Advance

解决方案

The way your mention with 4 embedded terms aggregation is a good one.

Another way would be to use a single terms aggregation with a script that would create a fake term made up of the four terms you're trying to aggregate, like this:

{
   "size": 0,
   "aggs": {
      "combinations": {
          "terms": {
              "script": "[doc.foo.value, doc.bar.value, doc.baz.value, doc.qux.value].join(',')"
          }
      }
   }
}

Note: make sure to enable dynamic scripting in order to use the above script aggregation.

Yet another solution in order to spare some cycles at search time would be to create that aggregate term at indexing time in another field, which you would then use in a single terms aggregation.

EDIT: If the order of results are important, and should be sorted based on doc_count of foo

{
   "size": 0,
   "aggs": {
        "primary": {
            "terms": {
                "field": "foo"
            },
           "aggs": {
               "combinations": {
                   "terms": {
                   "script": "[doc.foo.value, doc.bar.value, doc.baz.value, doc.qux.value].join(',')"
                   }
               }
           }
     }
}

这篇关于弹性搜索:如何获取3个或多个字段的多个组合的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:43