简体   繁体   English

在java api的Solr搜索中需要搜索文本和它周围的几行

[英]Need searched text and a few lines around it in Solr search in java api

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.我正在使用 solr 7.7.2 并且我使用 solrj 在 Solr 中编写了一个 Java 程序,该程序在一个巨大的文本文件中搜索一个单词。 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) + " \n");
        }
    }

Highlighting is one of solr feature. Highlighting是 solr 功能之一。 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 – 设置为 true,它可以在查询响应中生成突出显示的片段。

hl.fl – mention a list of fields to highlight. hl.fl – 提及要突出显示的字段列表。 char * will highlight all the fields char * 将突出显示所有字段

hl.fragsize – The size, in characters, of the snippets (aka fragments) created by the highlighter. hl.fragsize – 荧光笔创建的片段(又名片段)的大小(以字符为单位)。 In the original Highlighter, “0” indicates that the whole field value should be used with no fragmenting.在原始的荧光笔中,“0”表示应该使用整个字段值,没有碎片。 By default fragment is of size 100 characters默认片段大小为 100 个字符

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.注意请忽略代码中的硬编码内容,例如硬编码的 url "solrUrl = " http://localhost:8983/solr " 和字符串 "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();
    }

Please find the screenshot of the output :请找到输出的屏幕截图:

高亮输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Solr查询结果-需要搜索的文本及其周围的几行 - Solr query results - need searched text and a few lines around it Java 在文本中搜索并在文本周围返回多行 - Java search in a text and returning multiple lines around it 搜索并替换Java TextArea中的所有搜索文本 - Search and Replace All the Searched Text in Java TextArea 为什么在搜索“春季生育力”时,Web应用程序的Solr搜索引擎会返回“ Java Spring”结果? 如何解决这个问题? - Why would a web app's Solr search engine return ‘Java Spring’ results when searched for ‘Spring Fertility’? How can one fix this? 如果先前搜索过,则使用缓存搜索建议(Java) - Search suggestions with caching if searched was made previously (Java) 如何在搜索的SearchViews文本字段中保留搜索文本? - How do I persist searched text in the SearchViews text field on search? MongoDB Java API:全文搜索 - MongoDB Java API: Full-text search ElasticSearch使用Java API进行全文搜索 - ElasticSearch full text search using Java API App Engine的全文搜索API(Java) - Full Text Search API for App Engine (Java) Elasticsearch java api用于使用过滤器进行全文搜索 - Elasticsearch java api for full text search with filters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM