To install solr on a VM, download the Linux installation package of solr from the official website. Decompress the package and add port 8983 in /etc/sysconfig/iptables (default solr).


2.Solr installation:


Step 1: Decompress the zip file:


[root@localhost Desktop]# tar -zvxf solr-7.3.1. TGZ -c /opt/


Step 2: Start Solr


[root@localhost Desktop]# /opt/solr-7.3.1/bin/solr start-force


Step 3: test type http://localhost:8983/solr in your browser


If the AdminUI interface is correct, otherwise fail


Step 4: Create Admin Core


There is a bug:


Fixed:


[root @ localhost Desktop] # cp – rf/opt/solr – 7.3.1 / server/solr/configsets _default/conf / / opt/solr – 7.3.1 / server/solr/new_core /


Move the configuration file to the new new_core


After the repair is complete, re-creating new_core succeeds


3. After the startup, the Windows browser accesses the Solr page using IP address: 8983


Note: When storing data in solr, the entity class must have a String ID field annotated with solr field, so that the solr ID is the database ID




  • @Field







  • private

    String id;






  • @Field







  • private

    String sid;






  • @Field







  • private

    String sname;






  • @Field







  • private
    Double

    sprice;






  • @Field







  • @DateTimeFormat(pattern=”yyyy-MM-dd”)







  • private

    Date stime;





4. Solr needs to inject two entity classes in Spring, one for connecting to virtual machines and one for solr’s template classes




  • <! — Configure solr factory to get solr client object –>







  • <bean id=”solrClientFactory” class=”org.springframework.data.solr.server.support.HttpSolrClientFactoryBean”>



  • The < property name = “url” value = “http://192.168.133.131:8983/solr” / >



  • <property name=”timeout” value=”15000″/>






  • <property name=”maxConnections” value=”100″/>






  • </bean>






  • <! — Simplified template object for manipulating solr –>







  • <bean id=”solrTemplate” class=”org.springframework.data.solr.core.SolrTemplate”>






  • <constructor-arg index=”0″ ref=”solrClientFactory”/>






  • </bean>





Highlight:


Here is a solr utility class that encapsulates solr’s ACDI operations




  • public

    class SolrUtil {






  • // Singleton slob







  • //1. Privatized static class attributes







  • private
    static

    SolrUtil solrUtil;






  • //2. Privatize the constructor







  • private SolrUtil() {






  • }






  • //3. Provide public access methods externally







  • public static SolrUtil getSolrUtil(){






  • if

    (solrUtil==

    null

    ) {






  • return
    new

    SolrUtil();






  • }






  • return

    solrUtil;






  • }













  • private
    static

    String coreName =

    “new_core”

    ;






  • / * *







  • * Add collection data







  • *







  • * @param entities







  • * /







  • public void initData(SolrTemplate solrTemplate,List entities) {






  • solrTemplate.setSolrCore(coreName);






  • solrTemplate.saveBeans(entities);






  • solrTemplate.commit(coreName);






  • }













  • / * *







  • * Add data to solr







  • * @param solrTemplate Solr’s template object







  • * @param obj adds that object







  • * /







  • public void insertBean(SolrTemplate solrTemplate,Object obj) {






  • solrTemplate.setSolrCore(coreName);






  • solrTemplate.saveBean(obj);






  • solrTemplate.commit(coreName);






  • }













  • / * *







  • * Delete a piece of data from solr by Id







  • * @param solrTemplate







  • * @param id Specifies the ID to be deleted







  • * /







  • public void deleteBean(SolrTemplate solrTemplate,String id) {






  • solrTemplate.setSolrCore(coreName);






  • solrTemplate.deleteById(coreName,id);






  • solrTemplate.commit(coreName);






  • }













  • / * *







  • * Batch delete data from solr based on Id







  • * @param solrTemplate







  • * @param ids







  • * /







  • public void deleteBeans(SolrTemplate solrTemplate,Collection ids) {






  • solrTemplate.setSolrCore(coreName);






  • solrTemplate.deleteById(coreName,ids);






  • solrTemplate.commit(coreName);






  • }













  • / * *







  • * To modify data from solr, first delete the data by Id, then add the modified data







  • * @param solrTemplate







  • * @param id







  • * @param obj







  • * /







  • public void updateBeans(SolrTemplate solrTemplate,String id,Object obj) {






  • solrTemplate.setSolrCore(coreName);






  • solrTemplate.deleteById(coreName,id);






  • solrTemplate.saveBean(obj);






  • solrTemplate.commit(coreName);






  • }













  • / * *







  • * the query







  • * Query all data from solr







  • * @param model is used to load data to the front table







  • * @param solrTemplate Solr template object, used to query solr data







  • * @param cpage Current page







  • * @param pageSize Page unit







  • * @param mohu wants to highlight the query field







  • * @param clazz class object used to query objects in solr







  • * @param fieldName queries highlighted fields







  • * @param listdName The name of the collection to send to the front desk







  • * @throws Exception







  • * /







  • public void selectObjects(Model model, SolrTemplate solrTemplate, Integer cpage,






  • Integer pageSize, String mohu, Class clazz, String fieldName,String listName)
    throws

    Exception {






  • // Declare the return value variable







  • List entities =
    new

    ArrayList<>();






  • // Count the number of start records







  • Integer startIndex = (cpage –
    1

    ) * pageSize;






  • // Set the operation core







  • solrTemplate.setSolrCore(coreName);






  • // Highlight the query criteria







  • HighlightQuery query =
    new

    SimpleHighlightQuery();






  • // Start counting







  • query.setOffset(startIndex);






  • // Display the number of entries







  • query.setRows(pageSize);






  • // Set the condition







  • Criteria criteria =
    new

    Criteria(fieldName);






  • // Check whether the query condition is null







  • if

    (mohu! =

    “”

    && mohu! =

    null

    ) {






  • if

    (mohu.contains(

    “”

    )) {






  • String[] split = mohu.split(
    “”

    );






  • criteria.contains(split);






  • }
    else
    if

    (mohu.contains(

    “,”

    )) {






  • String[] split = mohu.split(
    “,”

    );






  • criteria.contains(split);






  • }
    else

    {






  • criteria.contains(mohu);






  • }






  • }






  • // Conditional processing







  • query.addCriteria(criteria);













  • // Sets the highlighted parameter handling object







  • HighlightOptions highlightOptions =
    new

    HighlightOptions();






  • highlightOptions.addField(fieldName);






  • highlightOptions.setSimplePrefix(
    “<font color=’red’>”

    );






  • highlightOptions.setSimplePostfix(
    “</font>”

    );






  • query.setHighlightOptions(highlightOptions);













  • // Perform the highlighted query







  • HighlightPage highlightPage = solrTemplate.queryForHighlightPage(query, clazz);






  • // Get the total page count







  • Integer pageCount = highlightPage.getTotalPages();






  • // Get the current page data







  • List<HighlightEntry> highlighted = highlightPage.getHighlighted();






  • // Get the highlighted object







  • for

    (HighlightEntry cc : highlighted) {






  • Object entity = cc.getEntity();






  • // Get highlighted results







  • List<Highlight> highlights = cc.getHighlights();






  • // Set the highlighted values







  • if

    (highlights ! =

    null

    && highlights.size() >

    0

    && highlights.get(

    0

    ) !=

    null







  • && highlights.get(
    0

    ).getSnipplets() ! =

    null

    && highlights.get(

    0

    ).getSnipplets().size() >

    0

    ) {






  • // Get the field name







  • Field field = clazz.getDeclaredField(fieldName);






  • // Set the highlighting







  • field.setAccessible(
    true

    );






  • field.set(entity, highlights.get(
    0

    ).getSnipplets().get(

    0

    ));






  • }






  • // Add to the collection







  • entities.add(entity);






  • }













  • // Store it in the model domain







  • model.addAttribute(
    “cpage”

    , cpage);






  • model.addAttribute(
    “pageCount”

    , pageCount);






  • model.addAttribute(
    “mohu”

    , mohu);






  • model.addAttribute(listName, entities);






  • }






  • }