我正在使用 solr 7.7.2,我使用 solrj 在 Solr 中编写了一个 Java 程序,该程序在一个巨大的文本文件中搜索一个单词.我使用以下代码来显示代表整个文本的搜索结果.
SolrQuery 参数 = new SolrQuery();params.setQuery("content:word for search");
如何在该行中只显示一行文字?
所有代码都是这样的
public static void main(String args[]) throws SolrServerException, IOException{String urlString = "http://localhost:8983/solr/test_core";SolrClient Solr = new HttpSolrClient.Builder(urlString).build();SolrQuery 参数 = 新 SolrQuery();params.setQuery("content:word for search");params.setSort("score", SolrQuery.ORDER.desc);QueryResponse queryResponse = Solr.query(params);SolrDocumentList 结果 = queryResponse.getResults();for (int i = 0 ; i < result.size(); i++ ){System.out.println(result.get(i) + "
");}}
Highlighting
是 solr 的一项功能.你必须通过查询参数来实现高亮如下:-
hl
– 设置为 true,它可以在查询响应中生成突出显示的片段.
hl.fl
– 提及要突出显示的字段列表.char * 将突出显示所有字段
hl.fragsize
– 荧光笔创建的片段(又名片段)的大小(以字符为单位).在原来的 Highlighter 中,0"表示应该使用整个字段值,不要分片.默认情况下,片段大小为 100 个字符
通过添加以下代码进行检查.
params.setHighlight(true).setHighlightSnippets(1);params.setParam("hl.fl", "*");params.setParam("hl.fragsize", "0");
这里有完整的代码供你尝试.
注意:请忽略代码中硬编码的内容,像硬编码的 url "solrUrl = "
i'm using solr 7.7.2 and I wrote a Java program in Solr using solrj that searches for a word in a huge text file. I use following code to show the search results that represent the whole text.
SolrQuery params = new SolrQuery();
params.setQuery("content:word for search");
How to display only one line of text where the word is in that line?
All code is like this
public static void main(String args[]) throws SolrServerException, IOException
{
String urlString = "http://localhost:8983/solr/test_core";
SolrClient Solr = new HttpSolrClient.Builder(urlString).build();
SolrQuery params = new SolrQuery();
params.setQuery("content:word for search");
params.setSort("score", SolrQuery.ORDER.desc);
QueryResponse queryResponse = Solr.query(params);
SolrDocumentList result = queryResponse.getResults();
for (int i = 0 ; i < result.size(); i++ )
{
System.out.println(result.get(i) + "
");
}
}
Highlighting
is one of solr feature. You have to pass query Parameters to achieve highlighting are as follows:-
hl
– set to true, it enables highlighted snippets to be generated in the query response.
hl.fl
– mention a list of fields to highlight. char * will highlight all the fields
hl.fragsize
– The size, in characters, of the snippets (aka fragments) created by the highlighter. In the original Highlighter, "0" indicates that the whole field value should be used with no fragmenting. By default fragment is of size 100 characters
Check by adding the below code.
params.setHighlight(true).setHighlightSnippets(1);
params.setParam("hl.fl", "*");
params.setParam("hl.fragsize", "0");
Here is the full code for you to try.
Note : Please ignore the hardcoded things from the code, like hardcoded url "solrUrl = "http://localhost:8983/solr" and string "return "Success"". Those should be read from the properties file and from a constant file. Expect you do the same. Never use System out in your production code.
public String getResult() throws SolrServerException, IOException {
final SolrClient client = getSolrClient();
ModifiableSolrParams params = new ModifiableSolrParams ();
params.set ("q", "comment_t:pizza");
params.set ("fl", "id, comment_t");
params.set ("sort", "id asc");
params.set("hl", true);
params.set("hl.q", "pizza");
params.set("hl.simple.pre", "<strong>");
params.set("hl.simple.post", "</strong>");
params.set("hl.fl", "comment_t");
params.set("hl.fragsize", "100");
final QueryResponse response = client.query("demo", params);
response.getHighlighting();
final SolrDocumentList documents = response.getResults();
System.out.println("Found " + documents.getNumFound() + " documents");
for (SolrDocument document : documents) {
final String id = (String) document.getFirstValue("id");
final String name = (String) document.getFirstValue("comment_t");
System.out.println("id: " + id + "; comment_t: " + name);
if(response.getHighlighting() != null){
System.out.println("highlighted text :: " + response.getHighlighting());
}
}
return "Success";
}
private SolrClient getSolrClient() {
final String solrUrl = "http://localhost:8983/solr";
return new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
}
Please find the screenshot of the output :
这篇关于在 Java api 中的 Solr 搜索中需要搜索的文本和围绕它的几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!