Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要在JavaAPI中的Solr搜索中搜索文本和围绕文本的几行_Java_Search_Solr_Lucene_Solrj - Fatal编程技术网

需要在JavaAPI中的Solr搜索中搜索文本和围绕文本的几行

需要在JavaAPI中的Solr搜索中搜索文本和围绕文本的几行,java,search,solr,lucene,solrj,Java,Search,Solr,Lucene,Solrj,我使用的是Solr7.7.2,我使用solrj在solr中编写了一个Java程序,可以在一个巨大的文本文件中搜索一个单词。我使用以下代码显示代表整个文本的搜索结果 SolrQuery params = new SolrQuery(); params.setQuery("content:word for search"); 如何仅显示一行文字中的单词? 所有代码都是这样的 public static void main(String args[]) throws SolrServerExcept

我使用的是Solr7.7.2,我使用solrj在solr中编写了一个Java程序,可以在一个巨大的文本文件中搜索一个单词。我使用以下代码显示代表整个文本的搜索结果

SolrQuery params = 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 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) + " \n");
        }
    }

publicstaticvoidmain(字符串args[])抛出SolrServerException、IOException
{
字符串URL字符串=”http://localhost:8983/solr/test_core";
SolrClient Solr=新的HttpSolrClient.Builder(urlString.build();
SolrQuery参数=新的SolrQuery();
setQuery(“内容:搜索用词”);
参数setSort(“score”,SolrQuery.ORDER.desc);
QueryResponse QueryResponse=Solr.query(参数);
SolrDocumentList结果=queryResponse.getResults();
对于(int i=0;i
突出显示
是solr的一项功能。必须传递查询参数才能实现高亮显示,如下所示:-

hl
–如果设置为true,则可以在查询响应中生成突出显示的代码段

hl.fl
–列出要突出显示的字段列表。char*将突出显示所有字段

hl.fragsize
–荧光笔创建的片段(也称为片段)的大小(以字符为单位)。在原始荧光笔中,“0”表示应使用整个字段值,且不应出现碎片。默认情况下,片段大小为100个字符

通过添加以下代码进行检查

params.setHighlight(true).setHighlightSnippets(1);
params.setParam("hl.fl", "*");
params.setParam("hl.fragsize", "0");
下面是供您尝试的完整代码

注意请忽略代码中的硬编码内容, 类似于硬编码url“solrUrl=”“和字符串“return”Success”。这些应该从属性文件和常量文件中读取。希望您也这样做。切勿在生产代码中使用System out。

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();
    }
public String getResult()抛出SolrServerException、IOException{
最终SolrClient=getSolrClient();
ModifiableSolrParams params=新的ModifiableSolrParams();
参数集(“q”,“注释:比萨饼”);
参数集(“fl”、“id、注释”);
参数集(“排序”、“id asc”);
参数设置(“hl”,真);
参数集(“hl.q”、“比萨饼”);
参数集(“hl.simple.pre”,“”;
参数集(“hl.simple.post”,“”;
参数集(“hl.fl”、“注释”);
参数集(“hl.fragsize”、“100”);
final QueryResponse response=client.query(“demo”,params);
response.getHighlighting();
最终解决方案文档列表文档=response.getResults();
System.out.println(“Found”+documents.getNumFound()+“documents”);
for(解决方案文档:文档){
最终字符串id=(字符串)document.getFirstValue(“id”);
最终字符串名称=(字符串)document.getFirstValue(“comment_t”);
System.out.println(“id:+id+”注释:+name);
if(response.getHighlighting()!=null){
System.out.println(“突出显示的文本::”+response.getHighlighting());
}
}
返回“成功”;
}
私有SolrClient getSolrClient(){
最终字符串solrUrl=”http://localhost:8983/solr";
返回新的HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000.build();
}
请查找输出的屏幕截图:


@FarzadEbrahimi:你尝试了什么?什么对你不起作用?