从记录列表中获取一组字段的不同值

问题描述 投票:0回答:1

我们使用Liferay(6.2 CE GA4)与Lucene进行自定义资产搜索。目前,我们可以检索正确的点击和完整的文件。

我们希望为自定义资产返回特定字段的唯一组合。

为了更清楚,我们想要做类似于以下SQL查询的事情,但在Liferay中使用Lucene:

SELECT DISTINCT
    field01, field02, field03 
FROM
    FieldsTable
WHERE
    someOtherField04 LIKE "%test%";
ORDER BY
    field01 ASC, field02 ASC, field03 ASC;

How we are doing it currently

目前,我们通过迭代所有文档然后过滤重复组合来手动获取字段值。当每个请求要处理的记录超过5k时,此过程需要时间。而不同的字段值大多是几百条记录。

任何帮助深表感谢。谢谢

P.S。:也在Liferay论坛上交叉发布:https://www.liferay.com/community/forums/-/message_boards/message/55513210

lucene liferay liferay-6
1个回答
2
投票

首先,您需要为查询创建SearchContext(仅作为参考):

SearchContext searchContext = new SearchContext();
searchContext.setAndSearch(true);

// Add any specific attributes for your use case below:
Map<String, Serializable> attributes = new HashMap<>();

attributes.put(Field.CLASS_NAME_ID, 0L);
attributes.put(Field.DESCRIPTION, null);
attributes.put(Field.STATUS, String.valueOf(WorkflowConstants.STATUS_APPROVED));
attributes.put(Field.TITLE, null);
attributes.put(Field.TYPE, null);
attributes.put("articleId", null);

attributes.put("ddmStructureKey", ...);
attributes.put("ddmTemplateKey", ...);

attributes.put("params", new LinkedHashMap<String, Object>());
searchContext.setAttributes(attributes);

searchContext.setCompanyId(... the ID of my portal instance ..);
searchContext.setGroupIds(new long[] { ... the ID of the site ... });
searchContext.setFolderIds(new long[] {});

现在,您可以找到一个或多个特定字段的所有值列表:

// We don't need any result document, just the field values
searchContext.setStart(0);
searchContext.setEnd(0);

// A facet is responsible for collecting the values
final MultiValueFacet fieldFacet = new MultiValueFacet(searchContext);
String fieldNameInLucene = "ddm/" + structureId + "/" + fieldName + "_" + LocaleUtil.toLanguageId(locale);
fieldFacet.setFieldName(fieldNameInLucene);
searchContext.addFacet(fieldFacet);

// Do search
IndexerRegistryUtil.getIndexer(JournalArticle.class).search(searchContext);

// Retrieve all terms
final List<String> terms = new ArrayList<>();
for (final TermCollector collector : fieldFacet.getFacetCollector().getTermCollectors()) {
    terms.add(collector.getTerm());
}

最后,terms将包含所有找到的文档中所有字段的条款。

© www.soinside.com 2019 - 2024. All rights reserved.