solr模糊與精確查詢 關鍵字顯示高亮

2021-09-28 22:00:09 字數 2356 閱讀 4787

/**

* 文件詳情介面

* * @param docid

* @return

*/public resultgetbydocid(@pathvariable string docid, string source, string keywords, string type)

if (page < constants.default_min_page) 

if (pagenum < constants.default_min_page_num) else if (pagenum > constants.default_max_page_num)

if (stringutils.isblank(keywords))

string q;

//模糊查詢與精確查詢

if (stringutils.isnotblank(type) && "accurate".equals(type)) else

if (stringutils.isnotblank(tag))

solrquery query = new solrquery(q);

//新增高亮

if (!keywords.equals("*"))

query.setfields("docid", "title", "docnum", "id");

query.setstart((page - 1) * pagenum);

query.setrows(pagenum);

if ("level".equalsignorecase(sort)) else if (stringutils.isnotblank(tag))

try

listdocumentlist = new arraylist<>();

documentlist = getdocument(queryresponse, list, documentlist);

return result.newsuccess().withtotal((int) list.getnumfound()).withdata(documentlist);

} catch (exception e) ], tag[{}], page[{}], pagenum[{}], type[{}]", keywords, tag, page, pagenum, type);

logger.error(e.getlocalizedmessage(), e);

}return result.newerror();

}/**

* @author: mahongfei

* @description: 遍歷高亮集合和關鍵字查詢的集合,根據id將高亮集合的title、content放到條件查詢的document

*/public listgetdocument(queryresponse queryresponse, solrdocumentlist list, listdocumentlist) else if ("content".equals(innerkey)) }}

documentlist.add(document);}}

}return documentlist;

}@override

public document getbydocid(string docid, string source, string keywords, string type) else

query.sethighlight(true);

query.addhighlightfield("title");// 高亮字段

query.addhighlightfield("content");

query.sethighlight******pre("");//標記,高亮關鍵字字首

query.sethighlight******post("");

query.sethighlightfragsize(100000);

} else

query.setquery(q);

try

listdocumentlist = new arraylist<>();

documentlist = getdocument(queryresponse, list, documentlist);

for (document document : documentlist)

}} catch (exception e) : docid[{}], keywords[{}], type[{}]", e.getlocalizedmessage(), docid, keywords, type, e);

}return null;

}

多欄位模糊匹配關鍵字查詢

我們開發資料庫應用時,常常需要用到模糊查詢。如果同乙個條件需要匹配很多字段怎麼辦呢?通常,程式設計師會每個欄位都在sql中 field like cond 一次。這樣,sql語句會長得驚人,碰上覆雜一點的,甚至sql語句會因為超長而被資料庫拒絕執行。其實,這個問題只要動動腦筋就很容易解決 首先,將要...

mysql關鍵字匹配度模糊查詢

有時候專案中需要進行模糊查詢,但是要對查詢出來的結果進行匹配度排序,比如搜尋關鍵字是 北 需要把資料庫中第乙個字是北的排在前面,此時就可以用mysql的instr函式。instr 欄位名,字串 這個函式返回字串在某乙個欄位的內容中的位置,沒有找到字串返回0,否則返回位置 從1開始 例1 公升序排 s...

explicit(顯式)關鍵字

struct a operator bool const struct b explicit operator bool const void doa a a void dob b b int main ok 直接列表初始化 a a4 ok 複製列表初始化 a a5 a 1 ok 允許 static...