List of usage examples for com.liferay.portal.kernel.search QueryConfig getLocale
public Locale getLocale()
From source file:com.rknowsys.portal.search.elastic.ElasticsearchIndexSearcher.java
License:Open Source License
private void addHighlights(Query query, SearchRequestBuilder searchRequestBuilder) { QueryConfig queryConfig = query.getQueryConfig(); if (queryConfig.isHighlightEnabled()) { String localizedContentName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.CONTENT); String localizedTitleName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.TITLE); int fragmentSize = queryConfig.getHighlightFragmentSize(); int numberOfFragments = queryConfig.getHighlightSnippetSize(); searchRequestBuilder.addHighlightedField(Field.CONTENT, fragmentSize, numberOfFragments); searchRequestBuilder.addHighlightedField(Field.TITLE, fragmentSize, numberOfFragments); searchRequestBuilder.addHighlightedField(localizedContentName, fragmentSize, numberOfFragments); searchRequestBuilder.addHighlightedField(localizedTitleName, fragmentSize, numberOfFragments); }/*from w w w .j a v a 2 s .c o m*/ }
From source file:com.rknowsys.portal.search.elastic.ElasticsearchIndexSearcher.java
License:Open Source License
protected String getSnippet(SearchHit searchHit, QueryConfig queryConfig, Set<String> queryTerms, Map<String, HighlightField> highlights, String field) { if (highlights == null) { return StringPool.BLANK; }// w w w. j av a2 s.c o m boolean localizedSearch = true; String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault()); String queryLanguageId = LocaleUtil.toLanguageId(queryConfig.getLocale()); if (defaultLanguageId.equals(queryLanguageId)) { localizedSearch = false; } if (localizedSearch) { String localizedName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), field); if (searchHit.fields().containsKey(localizedName)) { field = localizedName; } } HighlightField hField = highlights.get(field); if (hField == null) { return StringPool.BLANK; } List<String> snippets = new ArrayList<String>(); Text[] txtArr = hField.getFragments(); if (txtArr == null) { return StringPool.BLANK; } for (Text txt : txtArr) { snippets.add(txt.string()); } String snippet = StringUtil.merge(snippets, "..."); if (Validator.isNotNull(snippet)) { snippet = snippet + "..."; } else { snippet = StringPool.BLANK; } Pattern pattern = Pattern.compile("<em>(.*?)</em>"); Matcher matcher = pattern.matcher(snippet); while (matcher.find()) { queryTerms.add(matcher.group(1)); } snippet = StringUtil.replace(snippet, "<em>", ""); snippet = StringUtil.replace(snippet, "</em>", ""); return snippet; }
From source file:org.rsc.liferay.solr.SolrIndexSearcher.java
License:Open Source License
protected String getSnippet(SolrDocument solrDocument, QueryConfig queryConfig, Set<String> queryTerms, Map<String, Map<String, List<String>>> highlights, String field) { if (highlights == null) { return StringPool.BLANK; }/* www. j a v a2s . c om*/ String key = (String) solrDocument.getFieldValue(Field.UID); Map<String, List<String>> uidHighlights = highlights.get(key); boolean localizedSearch = true; String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault()); String queryLanguageId = LocaleUtil.toLanguageId(queryConfig.getLocale()); if (defaultLanguageId.equals(queryLanguageId)) { localizedSearch = false; } if (localizedSearch) { String localizedName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), field); if (solrDocument.containsKey(localizedName)) { field = localizedName; } } List<String> snippets = uidHighlights.get(field); String snippet = StringUtil.merge(snippets, "..."); if (Validator.isNotNull(snippet)) { snippet = snippet + "..."; } else { snippet = StringPool.BLANK; } Pattern pattern = Pattern.compile("<em>(.*?)</em>"); Matcher matcher = pattern.matcher(snippet); while (matcher.find()) { queryTerms.add(matcher.group(1)); } snippet = StringUtil.replace(snippet, "<em>", ""); snippet = StringUtil.replace(snippet, "</em>", ""); return snippet; }
From source file:org.rsc.liferay.solr.SolrIndexSearcher.java
License:Open Source License
protected SolrQuery translateQuery(long companyId, Query query, Sort[] sorts, int start, int end) throws Exception { QueryConfig queryConfig = query.getQueryConfig(); SolrQuery solrQuery = new SolrQuery(); if (queryConfig.isHighlightEnabled()) { solrQuery.setHighlight(true);//from w w w. j a v a 2s .co m solrQuery.setHighlightFragsize(queryConfig.getHighlightFragmentSize()); solrQuery.setHighlightSnippets(queryConfig.getHighlightSnippetSize()); String localizedContentName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.CONTENT); String localizedTitleName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.TITLE); solrQuery.setParam("hl.fl", Field.CONTENT, localizedContentName, Field.TITLE, localizedTitleName); } solrQuery.setIncludeScore(queryConfig.isScoreEnabled()); QueryTranslatorUtil.translateForSolr(query); String queryString = query.toString(); StringBundler sb = new StringBundler(6); sb.append(queryString); sb.append(StringPool.SPACE); sb.append(StringPool.PLUS); sb.append(Field.COMPANY_ID); sb.append(StringPool.COLON); sb.append(companyId); solrQuery.setQuery(sb.toString()); if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) { solrQuery.setRows(0); } else { solrQuery.setRows(end - start); solrQuery.setStart(start); } if (sorts != null) { for (Sort sort : sorts) { if (sort == null) { continue; } String sortFieldName = sort.getFieldName(); if (DocumentImpl.isSortableTextField(sortFieldName)) { sortFieldName = DocumentImpl.getSortableFieldName(sortFieldName); } ORDER order = ORDER.asc; if (Validator.isNull(sortFieldName) || !sortFieldName.endsWith("sortable")) { sortFieldName = "score"; order = ORDER.desc; } if (sort.isReverse()) { order = ORDER.desc; } solrQuery.addSort(new SortClause(sortFieldName, order)); } } return solrQuery; }