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

前端 未结 1 478
孤独总比滥情好
孤独总比滥情好 2021-01-28 09:23

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

相关标签:
1条回答
  • 2021-01-28 09:56

    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 :

    0 讨论(0)
提交回复
热议问题