1

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

hazard16
  • 65
  • 2
  • 10
  • Partial answer to your question: If you keep your structure-field as index-able then Lucene/solr will store it with name as prefixed with "web_content/". You may add structure field in query with web_content/fieldname – Pankaj Kathiriya Jul 02 '14 at 06:34
  • Hi, can you write more details? I tried to add structure field to query like this: attributes.put("web_content/section", "some section"); but it returned all journal articles not only journal articles with specified structure field and value. – hazard16 Jul 02 '14 at 10:41

1 Answers1

2

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.

  • Nice, I rewrote this piece of code to java and it seems that it is returning what I want. Thanks Krzysztof. – hazard16 Jul 04 '14 at 07:25
  • Hi, I have another question for this topic: How can I include searching by categoryId to this search? I have tried add categories array into searchContext and as a BooleanClause but nothing works. It is ignoring these categoryIds. Can you give me any advice? sc.setCategoryIds(ids); => NOT WORKING BooleanClause categoryId = BooleanClauseFactoryUtil.create( sc, Field.ASSET_CATEGORY_IDS, "48472", BooleanClauseOccur.MUST.getName()); => NOT WORKING – hazard16 Jul 08 '14 at 11:42