简体   繁体   中英

Liferay Elastic Search Query: Search for DLFileEntries that have a Custom Document Type

I work with Liferay 7.2 and I need to make an Elasticsearch query that finds als DLFileEntries that have the Document Type "XY". Currently I need to do this in Postman.

I am already able to find all DLFileEntry:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "entryClassName": "com.liferay.document.library.kernel.model.DLFileEntry"
          }
        }
      ]
    }
  }
}

But I need to find only these DLFileEntry that have Document Type "XY".

How can I do this?

You can simply add another match to the field fileEntryTypeId , where its value must be equal to the created Document Type id. You can find this id on table dlfileentrytype on column fileentrytypeid . Considering the id equals 37105 , the query would be like this

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "entryClassName": "com.liferay.document.library.kernel.model.DLFileEntry"
          }
        },
        {
          "match": {
            "fileEntryTypeId": "37105"
          }
        }
      ]
    }
  }
}

edit: Responding to your comment about how to search the DLFileEntry by its DLFileEntryType name, there is no direct way to do this as the DLFileEntryType is not indexed on Elastic Search by default. It would also probably need sub queries to achieve this and Elastic Search doesn't support sub queries.

With that in mind, the easiest approach I can think of is to customize the way DLFileEntry is indexed on Elastic Search, adding the field fileEntryTypeName . For that, you only need to implement a ModelDocumentContributor for DLFileEntry and add the fileEntryTypeName field to the document.

Basically, you just need to create a class like this:

package com.test.liferay.override;

import com.liferay.document.library.kernel.model.DLFileEntry;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.search.spi.model.index.contributor.ModelDocumentContributor;
import org.osgi.service.component.annotations.Component;

@Component(
        immediate = true,
        property = "indexer.class.name=com.liferay.document.library.kernel.model.DLFileEntry",
        service = ModelDocumentContributor.class
)
public class DLFileEntryModelDocumentContributor
        implements ModelDocumentContributor<DLFileEntry> {

    @Override
    public void contribute(Document document, DLFileEntry dlFileEntry) {
        try {
            document.addText(
                    "fileEntryTypeName", dlFileEntry.getDLFileEntryType().getName());
        } catch (PortalException e) {
            // handle error
        }
    }
}


As the DLFileEntryType name is localized , you should probably index it as a localized value:

package com.test.liferay.override;

import com.liferay.document.library.kernel.model.DLFileEntry;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.search.spi.model.index.contributor.ModelDocumentContributor;
import org.osgi.service.component.annotations.Component;

import java.util.Locale;

@Component(
        immediate = true,
        property = "indexer.class.name=com.liferay.document.library.kernel.model.DLFileEntry",
        service = ModelDocumentContributor.class
)
public class DLFileEntryModelDocumentContributor
        implements ModelDocumentContributor<DLFileEntry> {

    @Override
    public void contribute(Document document, DLFileEntry dlFileEntry) {
        try {
            Locale siteDefaultLocale = LocaleUtil.getSiteDefault();

            String localizedName = dlFileEntry
                    .getDLFileEntryType().getName(siteDefaultLocale);
            String localizedField = Field.getLocalizedName(
                    siteDefaultLocale, "fileEntryTypeName");

            document.addText(localizedField, localizedName);
        } catch (PortalException e) {
            // handle error
        }
    }
}

Now your query will be something like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "entryClassName": "com.liferay.document.library.kernel.model.DLFileEntry"
          }
        },
        {
          "match": {
            "fileEntryTypeName_en_US": "XY"
          }
        }
      ]
    }
  }
}

The name fileEntryTypeName_en_US depends on your site default locale. For example, if it is pt_BR, the name would be fileEntryTypeName_pt_BR .

Obs.: The fileEntryType name field is not unique, as it is localized, so you might find files with the same fileEntryType name but different fileEntryType .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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