gpt4 book ai didi

java - Liferay 7.3.5GA6自定义索引搜索和ddmFieldArray

转载 作者:行者123 更新时间:2023-12-03 00:44:18 25 4
gpt4 key购买 nike

我正在尝试使用 SearchContext、IndexSearcherHelperUtil 和所有其他东西为 Liferay 7.3.5 GA6 开发一个自定义 Web 内容搜索 portlet。

我有一些具有不同字段的 DDMStructure,从我在 elasticsearch 索引上看到的情况来看,这些字段在嵌套文档中建立索引,如下所示:

"ddmFieldArray": [
{
"ddmFieldName": "ddm__text__37702__nome_it_IT",
"ddmValueFieldName": "ddmFieldValueText_it_IT",
"ddmFieldValueText_it_IT": "Nome esempio",
"ddmFieldValueText_it_IT_String_sortable": "nome esempio"
}
,
{
"ddmFieldName": "ddm__text__37702__descrizione_breve_it_IT",
"ddmValueFieldName": "ddmFieldValueText_it_IT",
"ddmFieldValueText_it_IT": "Esempio di descrizione breve da indicizzare",
"ddmFieldValueText_it_IT_String_sortable": "esempio di descrizione breve da indicizzare"
}
]

这与我以前知道的旧方式不同,其中自定义字段的索引方式类似于ddm__[keyword/text]__[structure_id]__[field_name]

现在,我明白这种不同的索引方式是为了避免弹性问题(已超出总字段限制)而进行的改进,但是...执行搜索后,没有ddmFieldArraycom.liferay.portal.kernel.search.Document .getFields 中,因此我无法从 Elasticsearch 索引中获取 ddmstruct 字段值。

代码如下:

long journalArticleClassId = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class.getName());

SearchContext searchContext = new SearchContext();
searchContext.setClassTypeIds(new long[] {journalArticleClassId});
searchContext.setCompanyId(companyId);
searchContext.setStart(QueryUtil.ALL_POS);
searchContext.setEnd(QueryUtil.ALL_POS);

BooleanQuery query = new BooleanQueryImpl();


MatchQuery approvedQuery = new MatchQuery(Field.STATUS, String.valueOf(WorkflowConstants.STATUS_APPROVED));

query.add(approvedQuery, BooleanClauseOccur.MUST.getName());


Hits resultHits = IndexSearcherHelperUtil.search(searchContext, query);

for (Document doc: resultHits.getDocs()) {
doc.getFields().forEach((k, v) -> _log.debug(k)); //No ddm structure field
}

这仍然是一种改进还是只是一种意外行为?

有什么方法可以解决或扩展这个问题吗?

谢谢

最佳答案

“ddmFieldArray”字段是一个嵌套字段,默认情况下不返回。您必须从存储在 Elasticsearch 内部“_source”字段中的文档源字段中获取它(请参阅 https://www.elastic.co/guide/en/elasticsearch/reference/7.9/search-fields.html )

为了在 Liferay 中执行此操作,您必须使用旧的 portal-kernel 搜索类中不可用的一些搜索方法,您必须使用可用的新搜索类在位于 modules/apps/portal-search

portal-search-api 模块中

以下是您必须应用于代码的更改:

  • 在执行搜索之前,您必须在搜索中添加“fetchSource”标志,并添加以下代码:
    searchRequestBuilderFactory.builder(
searchContext
).fetchSource(
true
).build();
  • 执行搜索后,您必须从 SearchResponse 对象中获取 Document 对象。使用以下代码可以在 searchContext 中使用此 SearchResponse:
    /* Execute search */
IndexSearcherHelperUtil.search(searchContext, query);

/* Get results from search response */
SearchResponse searchResponse = searchContext.getAttribute("search.response");
List<SearchHit> resultHits = searchResponse.getSearchHits().getSearchHits();

/* Iterate */
for (SearchHit searchHit : resultHits) {
Document doc = searchHit.getDocument();

...your stuff...
}

您在以下 Liferay 类中有一些关于获取源代码和获取 SearchResponse 的示例:

我还实现了一个 groovy 脚本示例,您可以从以下位置执行它:控制面板 => 服务器管理 => 脚本

  • 7.3.x 版本的代码:
import com.liferay.registry.*;
import com.liferay.portal.kernel.search.*;
import com.liferay.portal.kernel.search.generic.*;
import com.liferay.portal.search.legacy.searcher.SearchRequestBuilderFactory;
import com.liferay.portal.search.searcher.SearchResponse;
import com.liferay.portal.search.hits.SearchHit;
import com.liferay.portal.search.document.Document;

/* Get SearchRequestBuilderFactory reference using RegistryUtil, because we cannot use "@Reference" in a groovy script */
Registry registry = RegistryUtil.getRegistry();
SearchRequestBuilderFactory searchRequestBuilderFactory = registry.getService(registry.getServiceReference(SearchRequestBuilderFactory.class.getName()));

/* Create SearchContext */
SearchContext searchContext = new SearchContext();
searchContext.setCompanyId(com.liferay.portal.kernel.util.PortalUtil.getCompany(actionRequest).getCompanyId());
searchContext.setStart(-1);
searchContext.setEnd(-1);

/* Line to fetch stored source of documents */
searchRequestBuilderFactory.builder(
searchContext
).fetchSource(
true
).build();

/* Get journal articles that are approved (status = 0) */
MatchQuery approvedQuery = new MatchQuery(Field.STATUS, String.valueOf(0));
MatchQuery journalArticleQuery = new MatchQuery("entryClassName", com.liferay.journal.model.JournalArticle.class.getName());

BooleanQuery query = new BooleanQueryImpl();
query.add(approvedQuery, BooleanClauseOccur.MUST.getName());
query.add(journalArticleQuery, BooleanClauseOccur.MUST.getName());

/* Execute search */
IndexSearcherHelperUtil.search(searchContext, query);

/* Get results from search response */
SearchResponse searchResponse = searchContext.getAttribute("search.response");
List<SearchHit> resultHits = searchResponse.getSearchHits().getSearchHits();

/* Iterate */
for (SearchHit searchHit : resultHits) {
Document doc = searchHit.getDocument();
out.println("entryClassPK: " + doc.getValue("entryClassPK"));
out.println("ddmFieldArray: " + doc.getValues("ddmFieldArray"));
out.println("");
}
  • 7.4.x 版本的代码:
import com.liferay.portal.kernel.search.*;
import com.liferay.portal.kernel.search.generic.*;
import com.liferay.portal.search.legacy.searcher.SearchRequestBuilderFactory;
import com.liferay.portal.search.searcher.SearchResponse;
import com.liferay.portal.search.hits.SearchHit;
import com.liferay.portal.search.document.Document;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.liferay.portal.kernel.module.util.SystemBundleUtil;

/* Get SearchRequestBuilderFactory reference using the BundleContext, because we cannot use "@Reference" in a groovy script */
BundleContext bundleContext = SystemBundleUtil.getBundleContext();
SearchRequestBuilderFactory searchRequestBuilderFactory = bundleContext.getService(bundleContext.getServiceReferences(SearchRequestBuilderFactory.class.getName(), null)[0]);

/* Create SearchContext */
SearchContext searchContext = new SearchContext();
searchContext.setCompanyId(com.liferay.portal.kernel.util.PortalUtil.getCompany(actionRequest).getCompanyId());
searchContext.setStart(-1);
searchContext.setEnd(-1);

/* Line to fetch stored source of documents */
searchRequestBuilderFactory.builder(
searchContext
).fetchSource(
true
).build();

/* Get journal articles that are approved (status = 0) */
MatchQuery approvedQuery = new MatchQuery(Field.STATUS, String.valueOf(0));
MatchQuery journalArticleQuery = new MatchQuery("entryClassName", com.liferay.journal.model.JournalArticle.class.getName());

BooleanQuery query = new BooleanQueryImpl();
query.add(approvedQuery, BooleanClauseOccur.MUST.getName());
query.add(journalArticleQuery, BooleanClauseOccur.MUST.getName());

/* Execute search */
IndexSearcherHelperUtil.search(searchContext, query);

/* Get results from search response */
SearchResponse searchResponse = searchContext.getAttribute("search.response");
List<SearchHit> resultHits = searchResponse.getSearchHits().getSearchHits();

/* Iterate */
for (SearchHit searchHit : resultHits) {
Document doc = searchHit.getDocument();
out.println("entryClassPK: " + doc.getValue("entryClassPK"));
out.println("ddmFieldArray: " + doc.getValues("ddmFieldArray"));
out.println("");
}

在您的代码中,您应该使用“@Reference”注释替换RegistryUtil 或BundleContext 用法。

如果您对我的示例有任何疑问,请告诉我。

关于java - Liferay 7.3.5GA6自定义索引搜索和ddmFieldArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64578394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com