Insert data into SOLr

{

“id”: 536563,

“tb_item_cid”: 560,

“Tb_item_cname “:” phone “,

“Tb_item_title “:” NEW2 – Alcatel (OT-927) Carbon Black Unicom 3G Mobile Phone Dual card dual waiting “,

“Tb_item_sell_point “:” Clearance! Only Beijing, Wuhan warehouse goods!”

}


Data inserted in Solr, the fields of the data need to be “registered” in SolR, the “registered” fields can support word segmentation, so the data can be queried.

1. Register in SOLR

1) What is the data after the search, so that such data can be stored in SOLR

The data that comes out of the search needs to have partial fields,

Therefore, by designing a model (Javabean) specifically for storing the data in SOLR, all such data is first captured and then stored in SolR.

2) Design javabeans


Product id

Name of commodity

Actual selling price

Commodity images

Commodity description


3) Design SQL statements according to properties in Javabean

SELECT

a.pid as id,

a.pname as t_product_name,

a.sale_price as t_product_sale_price,

a.pimage as t_product_pimage,

b.pdesc as t_product_pdesc

FROM

t_product a

LEFT JOIN t_product_desc b

ON a.pid = b.`pid`


4) Register fields in SOLr


Edit the IK configuration file: managed_SCHEMA

<field name=”t_product_name” type=”text_ik” indexed=”true” stored=”true”/>

<field name=”t_product_sale_price” type=”pfloat” indexed=”true” stored=”true”/>

<field name=”t_product_pimage” type=”text_general” indexed=”true” stored=”true”/>

<field name=”t_product_pdesc” type=”text_ik” indexed=”true” stored=”true”/>

Make the configuration file valid in the container

Copy to container

docker cp managed-schema solr:/opt/solr/server/solr/ik_core/conf


# restart container

docker-compose restart



5) Insert data into solr’s browser client


6) Query data


7) Delete data

Delete it based on the query result



Delete by ID







Two, in springboot using solR to achieve add, delete, change and check

1. Add dependencies

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-solr</artifactId>

</dependency>

yml:

Spring: data: solr: host: http://192.168.2.143:8983/solr/ik_coreCopy the code


2. Write a javabean



3. Insert data into solr

@Autowired private TProductSearchDTOMapper mapper; @Autowired private SolrClient solrClient; @test public void insertDataToSolr() throws IOException, SolrServerException {//1. List<TProductSearchDTO> products = mapper.selectAll(); List<SolrInputDocument> Documents = new ArrayList<>(); List<SolrInputDocument> Documents = new ArrayList<>(); // Encapsulate the Document objectfor (TProductSearchDTO product : products) {

        SolrInputDocument document = new SolrInputDocument();

        document.addField("id",product.getId());

        document.addField("t_product_name",product.gettProductName());

        document.addField("t_product_sale_price",product.gettProductSalePrice().floatValue());

        document.addField("t_product_pimage",product.gettProductPimage());

        document.addField("t_product_pdesc",product.gettProductPdesc()); Documents. Add (document); } // Submit to solr client solrclient.add (documents); //commit solrClient.commit(); }Copy the code



4. Query data in SOLr

1) Ordinary query and encapsulation

@Autowired private SolrClient solrClient; // Query solr library @test public voidtestQueryFromSolr() throws IOException, SolrServerException {

  

  //        String keyword = "T_product_name: mobile phone"; // Search for specified fields // Search for replication domains String keyword ="Mobile phone"; SolrQuery = new SolrQuery(); SolrQuery = new SolrQuery(); // Set query.set("df"."t_product_keywords"); // Replicate domain query query.setquery (keyword); query.setStart(0); query.setRows(10); / / highlighting query. AddHighlightField ("t_product_name");

        query.setHighlight(true);

        query.setHighlightSimplePre("<span style='color:red'>");

        query.setHighlightSimplePost("</span>"); QueryResponse response = solrClient.query(query); // System.out.println(response); // Encapsulate result set ==> List<TProductSearchDTO> List<TProductSearchDTO> products = new ArrayList<>(); SolrDocumentList results = response.getResults();for (SolrDocument document : results) {

            TProductSearchDTO product = new TProductSearchDTO();

            Long id = Long.parseLong((String) document.getFieldValue("id"));

            product.setId(id);

            String t_product_name = (String) document.getFieldValue("t_product_name");

            product.settProductName(t_product_name);

            Float t_product_sale_price = (Float) document.getFieldValue("t_product_sale_price");

            product.settProductSalePrice(new BigDecimal(t_product_sale_price));

            String t_product_pimage = (String) document.getFieldValue("t_product_pimage");

            product.settProductPimage(t_product_pimage);

            String t_product_pdesc = (String) document.getFieldValue("t_product_pdesc");

            product.settProductPdesc(t_product_pdesc);

  

            products.add(product);

  

        }

        System.out.println(products);

  

  

    }Copy the code



2) Query and encapsulation with highlighting

@Autowired private SolrClient solrClient; // Query solr library @test public voidtestQueryFromSolr() throws IOException, SolrServerException {

  

  //        String keyword = "T_product_name: mobile phone"; // Search for specified fields // Search for replication domains String keyword ="Mobile phone"; SolrQuery = new SolrQuery(); SolrQuery = new SolrQuery(); // Set query.set("df"."t_product_keywords"); // Replicate domain query query.setquery (keyword); query.setStart(0); query.setRows(10); / / highlighting query. AddHighlightField ("t_product_name");

        query.setHighlight(true);

        query.setHighlightSimplePre("<span style='color:red'>");

        query.setHighlightSimplePost("</span>"); QueryResponse response = solrClient.query(query); // System.out.println(response); // Encapsulate result set ==> List<TProductSearchDTO> List<TProductSearchDTO> products = new ArrayList<>(); SolrDocumentList results = response.getresults (); SolrDocumentList results = response.getresults (); // Get a new highlighting set Map<String, Map<String, List<String>>> highlighting = Response.gethighlighting ();for (SolrDocument document : results) {

            TProductSearchDTO product = new TProductSearchDTO();

            String stringId = (String) document.getFieldValue("id"); Long id = Long.parseLong(stringId ); //89983 product.setId(id); / / = = = = = = = = = = from highlight the result set that highlight the effect of t_product_name = = = = = = = = = = = = = Map < String, List<String>> stringListMap = highlighting.get(stringId); List<String> t_product_name1 = stringListMap.get("t_product_name");

            String t_product_name = t_product_name1.get(0);

            product.settProductName(t_product_name);

  

            Float t_product_sale_price = (Float) document.getFieldValue("t_product_sale_price");

            product.settProductSalePrice(new BigDecimal(t_product_sale_price));

            String t_product_pimage = (String) document.getFieldValue("t_product_pimage");

            product.settProductPimage(t_product_pimage);

            String t_product_pdesc = (String) document.getFieldValue("t_product_pdesc");

            product.settProductPdesc(t_product_pdesc);

  

            products.add(product);

  

        }

        System.out.println(products);

  

  

    }Copy the code





5. Delete data in solr