本文整理匯總了Java中org.apache.solr.client.solrj.SolrQuery.setHighlight方法的典型用法代碼示例。如果您正苦於以下問題:Java SolrQuery.setHighlight方法的具體用法?Java SolrQuery.setHighlight怎麽用?Java SolrQuery.setHighlight使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.solr.client.solrj.SolrQuery
的用法示例。
在下文中一共展示了SolrQuery.setHighlight方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: processHighlightOptions
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
/**
* Append highlighting parameters to {@link org.apache.solr.client.solrj.SolrQuery}
*
* @param solrQuery
* @param query
*/
protected void processHighlightOptions(SolrQuery solrQuery, HighlightQuery query) {
if (query.hasHighlightOptions()) {
HighlightOptions highlightOptions = query.getHighlightOptions();
solrQuery.setHighlight(true);
if (!highlightOptions.hasFields()) {
solrQuery.addHighlightField(HighlightOptions.ALL_FIELDS.getName());
} else {
for (Field field : highlightOptions.getFields()) {
solrQuery.addHighlightField(field.getName());
}
for (FieldWithHighlightParameters fieldWithHighlightParameters : highlightOptions
.getFieldsWithHighlightParameters()) {
addPerFieldHighlightParameters(solrQuery, fieldWithHighlightParameters);
}
}
for (HighlightParameter option : highlightOptions.getHighlightParameters()) {
addOptionToSolrQuery(solrQuery, option);
}
if (highlightOptions.hasQuery()) {
solrQuery.add(HighlightParams.Q, getQueryString(highlightOptions.getQuery()));
}
}
}
示例2: configureQueryForDocuments
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
private void configureQueryForDocuments(SolrQuery query) {
query.addFilterQuery("domain:document");
query.addSort("reference_date_time", ORDER.desc);
query.addField(Document.SOLR_LOCAL_TITLE_FIELD);
query.addField(Document.SOLR_DOC_DEF_UID_FIELD);
query.addField(Document.SOLR_DOCUMENT_STATUS_FIELD);
query.addField(Document.SOLR_AUTHOR_UID_FIELD);
query.addField(Document.SOLR_SIGNER_UID_FIELD);
query.addField(Document.SOLR_COSIGNER_UID_FIELD);
query.addField((Document.SOLR_ATTENDING_UID_FIELD));
query.addField(Document.SOLR_IS_INTERDISCIPLINARY_FIELD);
query.addField(Document.SOLR_INTERDISCIPLINARY_TYPE_FIELD);
query.addField(SearchService.PHRASE);
query.set(GroupParams.GROUP, true);
query.set(GroupParams.GROUP_FIELD, Document.SOLR_LOCAL_TITLE_FIELD);
query.setHighlight(true);
query.addHighlightField("body").addHighlightField("subject");
query.setHighlightFragsize(45);
query.setHighlightSnippets(5);
}
示例3: prepareQuery
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
private SolrQuery prepareQuery(PatientSearch search, boolean highlight) {
SolrQuery query = search.initQuery();
query.addFilterQuery("domain:med");
query.addSort("overall_stop", ORDER.desc);
query.addField("qualified_name");
query.addField("va_type");
query.addField("last_filled");
query.addField("last_give");
query.addField(CLASS_FIELD);
query.set(GroupParams.GROUP, true);
query.set(GroupParams.GROUP_FIELD, "kind");
query.add(GroupParams.GROUP_FIELD, "qualified_name");
if (highlight) {
query.setHighlight(true);
query.addHighlightField("administration_comment");
query.addHighlightField("prn_reason");
query.setHighlightFragsize(72);
query.setHighlightSnippets(5);
}
return query;
}
示例4: transform
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
@Override
public SolrQuery transform(final Query query) {
final SolrQuery solrQuery = new SolrQuery(query.getQuery());
solrQuery.setParam("q.op", query.getDefaultOperator().name());
if (query.getCursorMark() != null) {
solrQuery.setParam(CursorMarkParams.CURSOR_MARK_PARAM, query.getCursorMark());
} else {
solrQuery.setStart(query.getOffset());
}
solrQuery.setRows(query.getRows());
if (query.getReturnFields() != null) {
query.getReturnFields().stream().forEach(f -> solrQuery.addField(f.getName()));
}
if (query.getHighlightFields() != null && !query.getHighlightFields().isEmpty()) {
solrQuery.setHighlight(true);
query.getHighlightFields().stream().forEach(hf -> solrQuery.addHighlightField(hf.getName()));
}
if (query.getFacetFields() != null) {
query.getFacetFields().stream().forEach(ff -> solrQuery.addFacetField(ff.getName()));
}
if (query.getSorts() != null) {
for (Sort sort : query.getSorts()) {
SolrQuery.ORDER solrOrder = sort.getSortOrder() == SortOrder.ASC ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc;
SolrQuery.SortClause sortClause = new SolrQuery.SortClause(sort.getField().getName(), solrOrder);
solrQuery.addSort(sortClause);
}
}
if (query.getFilterQueries() != null) {
query.getFilterQueries().stream().forEach(fq -> solrQuery.addFilterQuery(fq.getField().getName() + ":" + fq.getValue()));
}
if (query.getParams() != null) {
query.getParams().entrySet().stream().forEach(e -> solrQuery.add(e.getKey(), e.getValue()));
}
return solrQuery;
}
示例5: autocomplete
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
public List<Match> autocomplete(String query, Integer pageSize, Map<String, String> selectedFacets) throws SolrServerException {
SolrQuery solrQuery = new SolrQuery();
if (query != null && !query.trim().equals("")) {
//String searchString = query.trim().replace(" ", "+");
solrQuery.setQuery(query);
} else {
return new ArrayList<Match>();
}
// Filter the searchable objects out
solrQuery.addFilterQuery("base.class_searchable_b:" + isSearchableObject());
// Set additional result parameters
solrQuery.setRows(pageSize);
if(selectedFacets != null && !selectedFacets.isEmpty()) {
for(String facetName : selectedFacets.keySet()) {
solrQuery.addFilterQuery(facetName + ":" + selectedFacets.get(facetName));
}
}
solrQuery.set("defType","edismax");
solrQuery.set("qf", "autocomplete^3 autocompleteng");
solrQuery.set("pf", "autocompletenge");
solrQuery.set("fl","autocomplete,id");
solrQuery.setHighlight(true);
solrQuery.set("hl.fl", "autocomplete");
solrQuery.set("hl.snippets",3);
solrQuery.setHighlightSimplePre("<b>");
solrQuery.setHighlightSimplePost("</b>");
solrQuery.set("group","true");
solrQuery.set("group.field", "autocomplete");
QueryResponse queryResponse = solrServer.query(solrQuery);
List<Match> results = new ArrayList<Match>();
Map<String,Match> matchMap = new HashMap<String,Match>();
for(GroupCommand groupCommand : queryResponse.getGroupResponse().getValues()) {
for (Group group : groupCommand.getValues()) {
for (SolrDocument solrDocument : group.getResult()) {
Match match = new Match();
String label = filter((String) solrDocument.get("autocomplete"));
match.setLabel(label);
match.setValue(label);
matchMap.put((String) solrDocument.get("id"), match);
results.add(match);
}
}
}
for(String documentId : matchMap.keySet()) {
if(queryResponse.getHighlighting().containsKey(documentId)) {
Map<String, List<String>> highlightedTerms = queryResponse.getHighlighting().get(documentId);
if(highlightedTerms.containsKey("autocomplete")) {
matchMap.get(documentId).setLabel(highlightedTerms.get("autocomplete").get(0));
}
}
}
return results;
}
示例6: getSolrQuery
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
/**
* 分頁查詢,包含查詢,分頁,高亮及獲取高亮處摘要等內容;不同於數據庫的查詢分頁,
* solr的查詢返回值中有文檔總數,所以無需再次查詢總條數。
*
* @param solrServer, page 自定義的翻頁對象,包含查詢信息及當前頁數據列表。
* @return List<FileModel>
*/
public PageModel getSolrQuery(SolrServer solrServer, PageModel pageModel, Map filterQuery) {
String type = (String) filterQuery.get("type");
if(type == null || "".equals(type)) {
type = "*";
}
String role = (String) filterQuery.get("role");
if(role == null || "".equals(role)) {
role = "anonymous";
}
SolrQuery query = new SolrQuery();
// 獲取查詢參數
String para = pageModel.getParameter();
// System.out.println("para: " + para + "; type: " + type);
//TODO
// 過濾文件與權限類型
// query.setQuery("content_text:" + para);
query.setQuery(para);
query.setFilterQueries(
"(" + getTypeFilter(type) + ")"
+ "AND (" + getRoleFilter(role) + ")"
);
query.set("defType","edismax");
query.set("qf","file_name^10 content_text^1");
// query.addSort("upload_time", SolrQuery.ORDER.desc);
query.setStart((int)pageModel.getStart());
query.setRows(pageModel.getSize());
// System.out.println(page.getStart() + " " + page.getSize());
//設置高亮
query.setHighlight(true);// 開啟高亮組件
query.addHighlightField("file_name");// 高亮字段
query.addHighlightField("content_text");// 高亮字段
query.setHighlightSimplePre("<hlt>");//標記,高亮關鍵字前綴
query.setHighlightSimplePost("</hlt>");//後綴
query.setHighlight(true).setHighlightSnippets(2); //獲取高亮分片數,一般搜索詞可能分布在文章中的不同位置,其所在一定長度的語句即為一個片段,默認為1,但根據業務需要有時候需要多取出幾個分片。 - 此處設置決定下文中titleList, contentList中元素的個數
query.setHighlightFragsize(150);//每個分片的最大長度,默認為100。適當設置此值,如果太小,高亮的標題可能會顯不全;設置太大,摘要可能會太長。
return getPageModel(solrServer, pageModel, query);
}
示例7: testGetFieldsPhaseError
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public void testGetFieldsPhaseError() throws SolrServerException {
BadResponseWriter.failOnGetFields = true;
BadResponseWriter.failOnGetTopIds = false;
SolrQuery query = new SolrQuery();
query.setQuery("subject:batman OR subject:superman");
query.addField("id");
query.addField("subject");
query.set("distrib", "true");
query.set("shards", shard1 + "," + shard2);
query.set(ShardParams.SHARDS_INFO, "true");
query.set("debug", "true");
query.set("stats", "true");
query.set("stats.field", "id");
query.set("mlt", "true");
query.set("mlt.fl", "title");
query.set("mlt.count", "1");
query.set("mlt.mintf", "0");
query.set("mlt.mindf", "0");
query.setHighlight(true);
query.addFacetField("id");
query.setFacet(true);
ignoreException("Dummy exception in BadResponseWriter");
try {
collection1.query(query);
fail("Should get an exception");
} catch (Exception e) {
//expected
}
query.set(ShardParams.SHARDS_TOLERANT, "true");
QueryResponse response = collection1.query(query);
assertTrue(response.getResponseHeader().getBooleanArg("partialResults"));
NamedList<Object> shardsInfo = ((NamedList<Object>)response.getResponse().get("shards.info"));
boolean foundError = false;
for (int i = 0; i < shardsInfo.size(); i++) {
if (shardsInfo.getName(i).contains("collection2")) {
assertNotNull(((NamedList<Object>)shardsInfo.getVal(i)).get("error"));
foundError = true;
break;
}
}
assertTrue(foundError);
assertEquals(1, response.getResults().get(0).getFieldValue("id"));
assertEquals("batman", response.getResults().get(0).getFirstValue("subject"));
unIgnoreException("Dummy exception in BadResponseWriter");
}
示例8: testGetTopIdsPhaseError
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public void testGetTopIdsPhaseError() throws SolrServerException {
BadResponseWriter.failOnGetTopIds = true;
BadResponseWriter.failOnGetFields = false;
SolrQuery query = new SolrQuery();
query.setQuery("subject:batman OR subject:superman");
query.addField("id");
query.addField("subject");
query.set("distrib", "true");
query.set("shards", shard1 + "," + shard2);
query.set(ShardParams.SHARDS_INFO, "true");
query.set("debug", "true");
query.set("stats", "true");
query.set("stats.field", "id");
query.set("mlt", "true");
query.set("mlt.fl", "title");
query.set("mlt.count", "1");
query.set("mlt.mintf", "0");
query.set("mlt.mindf", "0");
query.setHighlight(true);
query.addFacetField("id");
query.setFacet(true);
ignoreException("Dummy exception in BadResponseWriter");
try {
collection1.query(query);
fail("Should get an exception");
} catch (Exception e) {
//expected
}
query.set(ShardParams.SHARDS_TOLERANT, "true");
QueryResponse response = collection1.query(query);
assertTrue(response.getResponseHeader().getBooleanArg("partialResults"));
NamedList<Object> shardsInfo = ((NamedList<Object>)response.getResponse().get("shards.info"));
boolean foundError = false;
for (int i = 0; i < shardsInfo.size(); i++) {
if (shardsInfo.getName(i).contains("collection2")) {
assertNotNull(((NamedList<Object>)shardsInfo.getVal(i)).get("error"));
foundError = true;
break;
}
}
assertTrue(foundError);
assertEquals(1, response.getResults().get(0).getFieldValue("id"));
assertEquals("batman", response.getResults().get(0).getFirstValue("subject"));
unIgnoreException("Dummy exception in BadResponseWriter");
}
示例9: createQuery
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
protected SolrQuery createQuery(String q, String fq, String qt,
boolean highlight, String facetFields, String sort, String sortOrder, Integer rows, Integer start,
String otherParams) throws QueryException {
SolrQuery query = new SolrQuery();
if(q != null) {
query.setQuery(q);
}
if(fq != null) {
List<String> filterQueries = this.getFilterQueries(fq);
for(String filterQuery:filterQueries) {
query.addFilterQuery(filterQuery);
}
}
if(qt != null) {
query.setQueryType(qt);
}
query.setHighlight(highlight);
if(facetFields == null || "".equals(facetFields)) {
query.setFacet(false);
}else {
query.setFacet(true);
List<String> facets = this.getFacets(facetFields);
for(String facet:facets) {
query.addFacetField(facet);
}
}
if(sort != null && !"".equals(sort)) {
query.setSortField(sort, ORDER.valueOf(sortOrder));
}
if(rows != null && rows < 0) {
throw new QueryException("Rows can't be less than 0");
}else if(rows != null) {
query.setRows(rows);
}
if(start != null && start < 0) {
throw new QueryException("Rows can't be less than 0");
}else if(start != null) {
query.setStart(start);
}
if(otherParams != null) {
List<String> params = this.getOtherParams(otherParams);
for(String param:params) {
query.add(getParamName(param), getParamValue(param));
}
}
return query;
}
示例10: search
import org.apache.solr.client.solrj.SolrQuery; //導入方法依賴的package包/類
@Override
public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
SearchResult searchResult = new SearchResult();
SolrQuery query = new SolrQuery();
//設置查詢條件
query.setQuery(queryString);
//設置分頁
query.setStart((page - 1) * rows);
query.setRows(rows);
//設置默認搜素域
query.set("df", "item_keywords");
query.setHighlight(true);
query.addHighlightField("item_title");
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
QueryResponse response = solrClient.query(query);
SolrDocumentList results = response.getResults();
searchResult.setRecordCount(results.getNumFound());
List<SolrItem> solrItems = new ArrayList<SolrItem>();
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for (SolrDocument result : results) {
SolrItem solrItem = new SolrItem();
solrItem.setId((String) result.get("id"));
List<String> strings = highlighting.get(result.get("id")).get("item_title");
if (strings != null && strings.size() > 0) {
solrItem.setTitle(strings.get(0));
} else {
solrItem.setTitle((String) result.get("item_title"));
}
solrItem.setCategory_name((String) result.get("item_category_name"));
solrItem.setImage((String) result.get("item_image"));
solrItem.setSell_point((String) result.get("item_sell_point"));
solrItem.setItem_desc((String) result.get("item_desc"));
solrItem.setPrice((Long) result.get("item_price"));
solrItems.add(solrItem);
}
searchResult.setItemList(solrItems);
searchResult.setCurPage(page);
long recordCount = searchResult.getRecordCount();
long pageCount = recordCount / rows;
if (recordCount % rows > 0) {
pageCount++;
}
searchResult.setPageCount(pageCount);
return searchResult;
}