Spring Boot-- use Solr

First, the addition of dependence:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>

Second, in application.yml in:

spring:
  data:
    solr:
      host: http://192.168.2.155:8983/solr/ik_core

Third, the test:

@RunWith (. SpringRunner class ) 
@SpringBootTest 
public  class MystoreSearchServiceApplicationTests { 

    @Autowired 
    Private SolrClient Client; 

    @Test 
    public  void testQueryDataByHighLighting () { 
        SolrQuery Query = new new SolrQuery ();
         // Set query 
        query.setQuery ( "dual card" );
         // set page 
        query.setStart (0 ); 
        query.setRows ( 10 );
         // set the replication domain 
        query.set ( "DF", "t_product_keywords" ); 

        //Open highlight 
        query.setHighlight ( to true );
         // add the highlighted field 
        query.addHighlightField ( "t_product_name" );
         // set highlighted prefix and suffix 
        query.setHighlightSimplePre ( "<span style = ' color: red;'> " ); 
        query.setHighlightSimplePost ( " </ span> " ); 

        the try { 
            QueryResponse Response = client.query (Query);
             // result set 
            SolrDocumentList results = response.getResults ();
             // highlights 
            Map <String, Map <String, List <String >>> highlighting = response.getHighlighting();
            for (SolrDocument result : results) {
                Object id = result.getFieldValue("id");
                // 根据id拿当前字段的高亮部分;
                Map<String, List<String>> stringListMap = highlighting.get(id);
                if (stringListMap != null) {
                    List<String> t_product_name = stringListMap.get("t_product_name");
                    String s = t_product_name.get(0);
                    System.out.println(s);
                }
            }
        } catch(SolrServerException E) { 
            e.printStackTrace (); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 

    @Test 
    public  void testQueryData () { 
        SolrQuery Query = new new SolrQuery ();
         // Set query 
        query.setQuery ( "double" );
         // set page 
        query.setStart (0 ); 
        query.setRows ( 10 );
         // set the replication domain 
        query.set ( "DF", "t_product_keywords" ); 

        the try {
            QueryResponse response = client.query(query);
            SolrDocumentList results = response.getResults();
            for (SolrDocument result : results) {
                System.out.println(result);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDeleteData() {
        String query = "id:003";
        try {
            client.deleteByQuery(query);
            client.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testAddData() {
        SolrInputDocument document = new SolrInputDocument();

        document.addField("id", "003");
        document.addField("t_product_name", "huawei p30 牛逼 荣耀之最 双卡双待");
        document.addField("t_product_age", "1212");

        try {
            client.add(document);
            client.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/Tractors/p/11323982.html