简体   繁体   English

Elasticsearch查询多种类型并按类型分组?

[英]Elasticsearch querying multiple types and grouped by types?

Suppose I am to search against two types [cars] and [buildings], and I would want the results to be separated. 假设我要搜索两种类型的[汽车]和[建筑物],并且我希望结果分开。 Is there a way one can group results by types? 有没有一种方法可以按类型对结果进行分组?

I understand one simple way will be to query each types separately, but for other use cases one may actually need to query tens or hundreds of types together. 我知道一种简单的方法是分别查询每种类型,但是对于其他用例,实际上可能需要一起查询数十种或数百种类型。 Is there a native way or hacky way(like using sort) to achieve this? 是否有本机方式或黑客方式(例如使用sort)来实现这一目标?

This type of grouping behavior is (currently) not available in elasticsearch. 这种类型的分组行为(当前)在elasticsearch中不可用。 It has been a long standing request: https://github.com/elasticsearch/elasticsearch/issues/256 这是一个长期存在的要求: https : //github.com/elasticsearch/elasticsearch/issues/256

There are two approaches that can help, both of which are far from perfect, but may be good enough for some use cases. 有两种方法可以帮助您,两种方法都不尽完善,但对于某些用例可能已经足够了。

  • Client side aggregation. 客户端聚合。 Request a lot more results than you plan on displaying and the then bucket those. 请求比您计划显示的结果更多的内容,然后将其分类。
  • Using multi-query. 使用多重查询。 This allows you to easily pass down some number of queries in a single batch, but will have potential scaling problems if the number of queries gets to large. 这使您可以轻松地在一个批次中传递一些查询,但是如果查询数量变大,则可能会出现扩展问题。

This is one feature that Solr has that elasticsearch doesn't, but I have never tried it. 这是Solr所没有的Elasticsearch功能之一,但我从未尝试过。 I used a similar feature with Autonomy IDOL years back, but the performance was abysmal. 几年前,我在Autonomy IDOL中使用了类似的功能,但是性能却很糟糕。

If you want the results separated in groups of documents, you're going to have to restructure your documents, since, elasticsearch is focused on finding matching documents. 如果要将结果分成文档组,则必须重新组织文档,因为elasticsearch专注于查找匹配的文档。 You might get around this by designing a document that has child documents then you can query for matches on the parent document that represents your type. 您可以通过设计包含子文档的文档来解决此问题,然后可以在代表您的类型的父文档中查询匹配项。

I guess there might be some common field (let's say it's [price]) if you want to search against different types. 我想如果要针对不同类型进行搜索,可能会有一些共同的领域(假设是[价格])。 Then it would be reasonable to add some different type like [price_aggregator] and put into it fields [type] and [price]. 然后,添加一些不同的类型(例如[price_aggregator])并将[type]和[price]字段放入其中是合理的。 And then you could easily build your query against just one type. 然后,您可以轻松针对一种类型构建查询。 This requires some additional work while indexing and more memory to store index but it's much performant when you search. 这需要在索引时进行一些额外的工作,并需要更多的内存来存储索引,但是在搜索时性能会很高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM