当前位置:网站首页>Using solrj to add, delete, modify, and query Solr is too simple

Using solrj to add, delete, modify, and query Solr is too simple

2022-06-11 00:44:00 A music loving programmer

One 、SolrJ What is it? ? 

​ SolrJ yes Solr Provided Java client API. adopt SolrJ Can achieve Java Program pair Solr Operation of data in .

​ The big premise : add to SolrJ rely on . Depending on the version and Solr The version strictly corresponds to

The version I use : According to their own solr The version corresponds to

dependencies>
    <dependency>
        <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
        <version>7.7.2</version>
    </dependency>
</dependencies>

Two 、 test

1. Import dependence


2. Write code

1. Add / modify implementation : The code is as follows

Add and modify the syntax , Newly added id If the id, That will modify the existing id, If it doesn't exist, add

    @Test
    public void testAdd() throws Exception {
        //Solr Address 
        String url = "http://192.168.0.107:8983/solr/testcore";
        HttpSolrClient solrClient = null;
        try {
            //Solr client 
            solrClient = new HttpSolrClient.Builder(url).build();

            //Solr Input document 
            SolrInputDocument solrInputDocument = new SolrInputDocument();
            solrInputDocument.addField("id", "6");
            solrInputDocument.addField("name", " PHS mobile ");
            solrInputDocument.addField("price", 10000);

            // Add input document 
            solrClient.add(solrInputDocument);
            // Submit 
            solrClient.commit();
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            // close resource 
            solrClient.close();
        }
    }

design sketch :

 2. Delete implementation :

    @Test
    public void deleteTest() throws Exception {
        String url = "http://192.168.0.107:8983/solr/testcore";

        HttpSolrClient httpSolrClient = null;

        try {
            httpSolrClient = new HttpSolrClient.Builder(url).build();

            httpSolrClient.deleteById("5");

            httpSolrClient.commit();
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            httpSolrClient.close();
        }
    }

3. Query function

 // Inquire about  
  @Test
    public void showSolr() throws Exception {
        String url = "http://192.168.0.107:8983/solr/testcore";

        HttpSolrClient solrClient = null;
        try {
            solrClient = new HttpSolrClient.Builder(url).build();

            // Encapsulate all query conditions 
            SolrQuery solrQuery = new SolrQuery();
            // Query all 
            solrQuery.setQuery("*:*");
            // Query... According to the conditions 
            //solrQuery.setQuery("name= Best Sellers );
            QueryResponse response = solrClient.query(solrQuery);
            SolrDocumentList results = response.getResults();

            System.out.println(" Total number of articles " + results.getNumFound());

            for (SolrDocument list : results){
                System.out.println(list);
            }
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            solrClient.close();
        }
    }

design sketch

4. Highlight operation

    // The highlighted 
    @Test
    public void highlightest() throws IOException {
        // Connect solr Address 
        String url = "http://192.168.0.107:8983/solr/testcore";
        // establish solr client 
        HttpSolrClient solrClient = null;

        try {
            solrClient = new HttpSolrClient.Builder(url).build();
            // Encapsulating queries 
            SolrQuery solrQuery = new SolrQuery();
            // Query... According to the conditions 
            solrQuery.setQuery("name: Best Sellers ");
            // Turn on highlight 
            solrQuery.setHighlight(true);
            // Set highlight key 
            solrQuery.addHighlightField("name");
            // Prefix 
            solrQuery.setHighlightSimplePre("<span>");
            // suffix 
            solrQuery.setHighlightSimplePost("</span>");

            QueryResponse response = solrClient.query(solrQuery);
            // get data 
            SolrDocumentList results = response.getResults();
            // Get the highlighted return value 
            Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
            // Traverse 
            for (SolrDocument list : results) {
                System.out.println(list.get("id"));
                Map<String, List<String>> map = highlighting.get(list.get("id"));

                List<String> HLList = map.get("name");

                // Judge 
                if (HLList != null && HLList.size() > 0) {
                    System.out.println(HLList.get(0));
                } else {
                    System.out.println(list.get("name"));
                }
                System.out.println("===============");
                System.out.println(list.get("price"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // close resource 
            solrClient.close();
        }
    }
原网站

版权声明
本文为[A music loving programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020627519655.html