elasticsearchsearchliferayliferay-dxpliferay-7.2

Search in Liferay


I have created one structure which is followed by web content. Structure Contains 2 fields. One is Area name and the second is Zip Code. I stored data in web content followed by this structure. I want to search for data based on zip code or area name entered by the user. I want to provide a dropdown to the user to select criteria to search like by Zipcode / by Area name.

The problem is web content data is stored in XML format. So whenever a user searches for a keyword it will return all results which contain given text. I want to restrict that.

I am using this method for search data.

List<JournalArticle> results = JournalArticleLocalServiceUtil.search( themeDisplay.getCompanyId(), //company id themeDisplay.getScopeGroupId(), // group id null, //folderIds Log list 0, //classname null, //version null,//title null, //description searchkeyword, // here put your keywords to search new Date(), // startDate new Date(), // endDate 0, //status null, //review Date QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);


Solution

  • You have to search using Liferay search API directly to Elasticsearch, filtering by DDM fields.

    Each field of webcontent structures are translated to a DDM Field in Elasticsearch side.

    You have information about how to query JournalArticle filtering by DDM fields in following links:

    (note: some links are related to 6.2 version, but 7.x queries should be very similar)

    In Liferay Portal 7.x, the names of DDM fields that you have to query are built in DDMIndexerImpl.encodeName(...) method, see:

    https://github.com/liferay/liferay-portal-ee/blob/cba537c39989bbd17606e4de4aa6b9ab9e81b30c/modules/apps/dynamic-data-mapping/dynamic-data-mapping-service/src/main/java/com/liferay/dynamic/data/mapping/internal/util/DDMIndexerImpl.java#L243-L268
    

    DDM fields names follows following pattern:

    Note: in order to get structureId, you should query DDMStructure filtering by structureKey, if you hardcode the structureId, you can have problems in case you export/import the structure because structureId is recalculated during import process.