The "ddmFieldArray" field is a nested field that it is not returned by default.
You have to fetch it from the document source field that is stored in an internal "_source" field in Elasticsearch (see https://www.elastic.co/guide/en/elasticsearch/reference/7.9/search-fields.html )
In order to do this in Liferay, you have to use some search methods that aren't available in the old portal-kernel search classes, you have to use the new search classes that are available in the portal-search-api module that is located in modules/apps/portal-search
These are the changes you have to apply to your code:
- Before executing the search, you have to add the "fetchSource" flag to your search adding this code:
searchRequestBuilderFactory.builder(
searchContext
).fetchSource(
true
).build();
- After executing the search, you have to get the Document objects from the SearchResponse object. This SearchResponse is available in the searchContext using this code:
/* 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...
}
You have some examples about fetching source and gettings SearchResponse in following Liferay classes:
I have also implemented a groovy script example that you can execute it from: Control Panel => Server Administration => Script
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("");
}
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("");
}
In your code you should replace the RegistryUtil or BundleContext usage with the "@Reference" annotation.
Let me know if you have any problem with my example.