Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[learning Notes] how to configure Solr index library

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to configure the Solr index library. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Spring integrates Solr

Spring configuration:

Java code manipulates the Solr index library:

Package cn.xing.test;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.SolrInputDocument;import org.springframework.beans.factory.annotation.Autowired Import org.springframework.stereotype.Service;import cn.xing.pojo.SearchItem;import cn.xing.pojo.SearchResult;/** * operate solr index database * * @ author Xing * * / @ Servicepublic class Operation {@ Autowired private SolrServer solrServer / / the configured httpSolrServer / * imports all goods into the index database, and the commodity object SearchItem must be the same as the business domain field and type configured by solr * / public void imprtSolrItem () {try {List list = new ArrayList () List.add (new SearchItem ("1", "Hubei", "Yellow Crane Tower", 12, "image1", "provinces and cities"); list.add (new SearchItem ("2", "Henan", "Kaifeng", 12, "image2", "provinces and cities")) List.add (new SearchItem ("3", "3", "3", 12, "image2", "222")); / / put list into the index library for (SearchItem searchItem: list) {SolrInputDocument doc = new SolrInputDocument () Doc.addField ("id", searchItem.getId ()); doc.addField ("item_title", searchItem.getTitle ()); doc.addField ("item_sell_point", searchItem.getSell_point ()) Doc.addField ("item_price", searchItem.getPrice ()); doc.addField ("item_image", searchItem.getImage ()); doc.addField ("item_category_name", searchItem.getCategory_name ()); solrServer.add (doc) } solrServer.commit (); System.out.println ("data imported successfully");} catch (Exception e) {e.printStackTrace () }} / / according to the query condition SolrQuery, query the index library public List querySolr (SolrQuery query) throws SolrServerException {/ / query the index library QueryResponse queryResponse = solrServer.query (query); SolrDocumentList results = queryResponse.getResults () / / take out the total number of records long num = results.getNumFound (); System.out.println ("number of data entries of query results:" + num); / / take out the product list, highlight the keywords / / Map Map highlighting = queryResponse.getHighlighting (); List itemList = new ArrayList () For (SolrDocument solrDocument: results) {SearchItem item = new SearchItem (); item.setId ((String) solrDocument.get ("id")); item.setCategory_name ((String) solrDocument.get ("item_category_name")); item.setImage ((String) solrDocument.get ("item_image")) Item.setPrice ((long) solrDocument.get ("item_price")); item.setSell_point ((String) solrDocument.get ("item_sell_point")); / / highlight result, item title List list = highlighting.get (solrDocument.get ("id")) .get ("item_title") If (list! = null & & list.size () > 0) {item.setTitle (list.get (0)); / / get the title} else {item.setTitle ((String) solrDocument.get ("item_title")) } itemList.add (item);} return itemList;} / * search the solr index database for items and page them based on keywords. * / public SearchResult search (String keyword, int page, int rows) throws Exception {/ / set query condition, call querySolr () method to query SolrQuery query = new SolrQuery (); query.setQuery (keyword); / / set query condition if (page 0) totalpages++; rs.setTotalPages ((long) * totalpages) * / return rs;}}

Paging query data encapsulation:

Package cn.xing.pojo;import java.io.Serializable;import java.util.List;/** * results of search for goods. The encapsulation of the data needed by the page. * * @ author Xing * * / public class SearchResult implements Serializable {private Long totalPages; private Integer recourdCount; private List itemList;}

Pojo corresponding to Solr data:

Package cn.xing.pojo;import java.io.Serializable;import org.apache.commons.lang3.StringUtils;public class SearchItem implements Serializable {@ Override public String toString () {return "SearchItem [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price + ", image=" + image + ", category_name=" + category_name + "]" } / / the type and the type configured in schema.xml in solr's Chinese parser must be the same as private String id; private String title; private String sell_point; private long price; private String image; private String category_name / / the search results page displays an image public String [] getImages () {String image2 = this.getImage (); if (StringUtils.isNotBlank (image2)) {String [] imagesArr = image2.split (","); return imagesArr;} return null } public String getId () {return id;} public void setId (String id) {this.id = id;}}

Java test code:

Package cn.xing.test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.xing.pojo.SearchItem;import cn.xing.pojo.SearchResult;public class test {public static void main (String [] args) throws Exception {ApplicationContext ac = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml"); Operation operation = ac.getBean (Operation.class) / / Import data operation.imprtSolrItem (); / / SearchResult search = operation.search ("obviously", 1, 1); SearchResult search = operation.search ("3", 1, 10); List itemList = search.getItemList () For (SearchItem searchItem: itemList) {System.out.println (searchItem);} the above content is [Learning Notes] how to configure the Solr index library. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report