Reputation: 75
I want to implement custom search portlet so I can search through attributes that are part of custom structures. Let's say that I've made structure called "Literature" which contains attributes as author, content, title, annotations, section etc. I want to create "advanced" search where can I select particular structure type (Literature, Verdict or Legislative), particular sections that are represented with another attribute of these structure types (for example select only literatures with section PPV or literatures with section APS) etc.
I tried to use faceted search but I didn't find out how to include these structure's attributes into query: This is my "search" code:
SearchContext searchContext = SearchContextFactory.getInstance(httpServletRequest);
Map<String, Serializable> attributes = new HashMap<String, Serializable>();
attributes.put(Field.TITLE, "some word");
attributes.put("paginationType", "regular");
searchContext.setAttributes(attributes);
Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);
assetEntriesFacet.setStatic(true);
searchContext.addFacet(assetEntriesFacet);
Facet scopeFacet = new ScopeFacet(searchContext);
scopeFacet.setStatic(true);
searchContext.addFacet(scopeFacet);
String[] entryClassNames = { JournalArticle.class.getName() };
searchContext.setEntryClassNames(entryClassNames);
hits = FacetedSearcher.getInstance().search(searchContext);
List<Document> docs = hits.toList();
So how to include in facet search my structure specific attributes like "section" etc...
My other question is that there is a problem with punctuation. Let's say that title contains word "Právo". When I run search with word "pravo" it will bring 0 results but when I run search with "právo" it will find me somw results. Is there any workaround for search with punctuation?
Thanks, Patrik
Upvotes: 1
Views: 5893
Reputation: 619
Searching FT index is quite messy in 6.2, I managed to do it this way (groovy):
Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class.name);
SearchContext sc = new SearchContext([
companyId: PortalUtil.defaultCompanyId,
groupIds: groupId
]);
String testText = DDMIndexerUtil.encodeName(
structureId, "custom-field-name", LocaleUtil.fromLanguageId("pl_PL"));
BooleanClause testTextClause = BooleanClauseFactoryUtil.create(
sc, testText, "Search term", BooleanClauseOccur.MUST.getName())
sc.setBooleanClauses(testTextClause)
sc.setStart(QueryUtil.ALL_POS);
sc.setEnd(QueryUtil.ALL_POS);
sc.setAttribute("paginationType", "none");
hits = indexer.search(sc);
hits.toList().each { Document doc ->
JournalArticle ja = JournalArticleLocalServiceUtil.getArticle(groupId, doc.get("articleId"))
println "${ja.getTitle(LocaleUtil.fromLanguageId("pl_PL"))}"
}
To run this you need to have structureId
(check journalarticle
table) and groupId
.
Upvotes: 2