Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Atin Agarwal
Making custom portlet searchable in Liferay default search portlet
February 6, 2013 3:26 AM
Answer

Atin Agarwal

Rank: Junior Member

Posts: 74

Join Date: February 19, 2012

Recent Posts

Hi,
To search the contents of my custom portlet from the Liferay's default search portlet I have followed the following steps but it doesn't showing up the results -

1. Created Indexes while adding my entity in DB -

public Sample addSample(Sample sample) {
---------------code to add entity----------------
Indexer indexer = IndexerRegistryUtil.getIndexer(Sample.class);
indexer.reindex(sample);
}

Also i did the same thing while updating my entity.

2.Created my Indexer Class -

public class SampleIndexer extends BaseIndexer {

public static final String[] CLASS_NAMES = { Sample.class.getName() };

public static final String PORTLET_ID = "sample -portlet";

public String[] getClassNames() {

return CLASS_NAMES;
}

@Override
protected void doDelete(Object obj) throws Exception {

Sample sample = (Sample) obj;
Document document = new DocumentImpl();

document.addUID(PORTLET_ID, sample.getPrimaryKey());

String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
SearchEngineUtil.deleteDocument(searchEngineId, sample.getCompanyId(),
document.get(Field.UID));
}

@Override
protected Document doGetDocument(Object obj) throws Exception {

Sample sample= (Sample) obj;
long groupId = getParentGroupId(sample.getGroupId());
long scopeGroupId = sample.getGroupId();
String description = sample.getSampleDescription();

Document document = getBaseModelDocument(PORTLET_ID, sample);

document.addKeyword(Field.GROUP_ID, groupId);
document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
document.addText(Field.DESCRIPTION, description);

return document;
}

@Override
public void postProcessSearchQuery(BooleanQuery searchQuery,
SearchContext searchContext) throws Exception {

addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, false);

}

@Override
protected void doReindex(Object obj) throws Exception {

Sample sample= (Sample) obj;
String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
SearchEngineUtil.updateDocument(searchEngineId, sample.getCompanyId(),
getDocument(sample));
}

@Override
protected void doReindex(String className, long classPK) throws Exception {

Sample sample= SampleLocalServiceUtil.getSample(classPK);

doReindex(sample);
}

@Override
protected void doReindex(String[] ids) throws Exception {

long companyId = GetterUtil.getLong(ids[0]);

doReindexAll(companyId);
}

private void doReindexAll(long companyId) throws Exception {

int count = SampleLocalServiceUtil.getSamplesCount();

int pages = count / Indexer.DEFAULT_INTERVAL;

for (int i = 0; i <= pages; i++) {
int start = (i * Indexer.DEFAULT_INTERVAL);
int end = start + Indexer.DEFAULT_INTERVAL;

reindexSamples(companyId, start, end);
}
}

protected void reindexSamples(long companyId, int start, int end)
throws Exception {

List< Sample> samples= SampleLocalServiceUtil.getSamples(start,
end);

if ( samples.isEmpty()) {
return;
}

Collection<Document> documents = new ArrayList<Document>();

for ( Sample sample: Samples) {
Document document = getDocument( sample);
documents.add(document);
}

String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
SearchEngineUtil.updateDocuments(searchEngineId, companyId, documents);
}

@Override
protected String getPortletId(SearchContext searchContext) {

return PORTLET_ID;
}

public Summary doGetSummary(Document document, Locale locale,
String snippet, PortletURL portletURL) {

String title = document.get(Field.TITLE);

String content = snippet;

if (Validator.isNull(snippet)) {
content = document.get(Field.DESCRIPTION);

if (Validator.isNull(content)) {
content = StringUtil.shorten(document.get(Field.CONTENT), 200);
}
}

String id = document.get(Field.ENTRY_CLASS_PK);

portletURL.setParameter("struts_action", "/search/search");
portletURL.setParameter("id", id);

return new Summary(title, content, portletURL);
}

@Override
public String getPortletId() {
return PORTLET_ID;
}
}


3. Made an entry of my indexer in liferay-portlet.xml -
<indexer-class>com.my.search.SampleIndexer</indexer-class>

4. Created my OpenSearchImpl class -
public class SampleOpenSearchImpl extends HitsOpenSearchImpl {

public static final String SEARCH_PATH = "/search/search";

public static final String TITLE = "Sample Search: ";

@Override
public Indexer getIndexer() {
return IndexerRegistryUtil.getIndexer(Sample.class);
}

@Override
public String getPortletId() {
return SampleIndexer.PORTLET_ID;
}

@Override
public String getSearchPath() {
return SEARCH_PATH;
}

@Override
public String getTitle(String keywords) {
return TITLE + keywords;
}
}

5. Made an entry of my open search class in liferay-portlet.xml -
<open-search-class>com.my.search.SampleOpenSearchImpl</open-search-class>

-----------------------------------------------------------------------------------------------------------------------------------
This is all i did , but got no results in search portlet.

Kindly, suggest if i am missing anything or doing something wrong.
Any help would be appreciated.


Thanks in advance,
Atin Agarwal
Hitoshi Ozawa
RE: Making custom portlet searchable in Liferay default search portlet
February 6, 2013 2:29 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

I have tried the steps mentioned in the following thread?

http://www.liferay.com/community/forums/-/message_boards/message/1839079
Atin Agarwal
RE: Making custom portlet searchable in Liferay default search portlet
October 7, 2013 2:39 AM
Answer

Atin Agarwal

Rank: Junior Member

Posts: 74

Join Date: February 19, 2012

Recent Posts

Though its a bit late but found the solution, I missed out to add companyId in the document.

After adding it-
document.addKeyword(Field.COMPANY_ID, companyId);

It worked.

Regards,
Atin Agarwal
Muhammad Taha
RE: Making custom portlet searchable in Liferay default search portlet
October 1, 2013 3:03 AM
Answer

Muhammad Taha

Rank: Junior Member

Posts: 66

Join Date: May 1, 2012

Recent Posts

Thanks Atin.