Solr configuration, usage and installation of plug-ins developed by JAVA to synchronize data with database tables

Solr setup and operation

Solr download address: http://archive.apache.org/dist/lucene/solr/

Download the unzipped directory:

Configuration steps:

1. Unzip solr-4.7.0.zip to the path you want to save, such as the current user root directory /Users/liuxun/solr-4.7.0
2. cmd opens the command line window and enters the /Users/liuxun/solr-4.7.0/example directory
3. Execute the command: java -jar start.jar
4. After passing the third step, the system will start the jetty server that comes with solr, and you can access solr through http://localhost:8983/solr/.
At this point, solr has been successfully started

Operation interface description:

Command test management index

(1) Create index

At this point, Solr has been installed and started, but there is no index yet. Only when the index is created can the search results be obtained.
1. Command to enter the /solr-4.6.0/example/exampledocs directory
2. Execute the command: java -jar post.jar solr.xml monitor.xml. At this time, you have successfully submitted 2 solr documents.
3. After executing the second step, we can access through the browser: http://localhost:8983/solr/collection1/select?q=solr &wt=xml
If you want to import more documents, execute the command: java -jar post.jar *.xml

(2) Update index

When the command: java -jar post.jar *.xml is repeatedly executed, it is found that the search results do not contain duplicate data. The reason: the column id is specified as uniqueKey (ie: unique) in the schema.xml in the example directory, so there are duplicates. When submitting data to the index database, data with the same ID will replace the data in the original document.
If you want to get duplicate data, you can do it by modifying the id value in *.xml in the exampledocs directory.

(3) Delete the index

1. Execute the command:
java -Ddata=args -Dcommit=false -jar post.jar “SP2514N”, you can delete the document with ID SP2514N
2. After executing the first step, search again and find that there is still data with the ID SP2514N in the search results. In fact, it is not that the first step was not deleted successfully, because the command in the first step contains -Dcommit=false, so the first step is deleted. The operation was not submitted to the index.
3. Before opening a new searcher, the data deleted in the first step will always exist in the search results, so we can force a new searcher to be opened and execute the command: java -jar post.jar –

(4) Query index

Solr searches data through http in the get method, such as: http://localhost:8983/solr/collection1/select?q=solr &wt=xml
q: Query keyword (the field queried at this time is the default query field text specified in solrconfig.xml)
fl: Field returned by search results
sort: sort
wt: search result return format
q=video & amp;fl=name,id (return only name and id fields)
q=video & amp;fl=name,id,score (return relevancy score as well)
q=video & amp;fl=*,score (return all stored fields, as well as relevancy score)
q=video & amp;sort=price desc & amp;fl=name,id,price (add sort specification: sort by price descending)
q=video & amp;wt=json (return response in JSON format)

Use SolrJ to interact with Solr server

If we want to use the Solr service, we can directly use urlconnection to call the Solr service interface according to the rules. If it is a web application, in addition to using URLConnection to call in the background, it can also use AJAX technology (XmlHttpRequest) to call, but we need to encapsulate the data and parse the data by ourselves. very troublesome.

SolrJ provides APIs for various clients, which can eliminate these tedious steps and directly interact with the Solr server through API-oriented programming.

Introduction and use of SolrJ

solrj is the java client of solr, used to access the solr index library.
It provides functions such as adding, deleting, querying, and optimizing.
Jar package location
Extract the directory:
It is integrated into the solr compressed package. After decompressing the file, there is a directory /dist/, which stores the jars used by solrj ( /dist/solrj-lib/*.jar and /dist/*.jar), Add all these jars to the classpath in the project and you can develop directly.

Demo example is as follows (to upload to GitHub https://github.com/LX1993728/SolrJ_java_Test)

Product.java

public class Product {
    //The value in Field must follow the rules of <fields> in the core schema
    @Field(value="id")
    private String id;
    @Field(value="name")
    private String name;
    @Field(value="description")
    private String desc;
    @Field(value="price")
    private float price;
       // setter getter
       //......
}

@Field indicates that the current attribute corresponds to which declared field name in the index library (specific rules are specified by schema)

SolrDao.java (index addition, deletion, modification and query)

package liuxun.solrj.dao;
import java.io.IOException;
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.impl.HttpSolrServer;
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 liuxun.solrj.bean.Product;
/**
 * Use solrJ to submit requests to solr, add, delete, modify and check. The bottom page of solrJ sends the http protocol...
 *
 * @author liuxun
 */
public class SolrDao {
    // There are some default fields and dynamic field rules in solr, which are configured in the file /example/solr/collection1/conf/schema.xml
    // add default index property
    public void addDefaultField() throws SolrServerException, IOException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "A field that must be added by default to distinguish the unique identifier of the document");
        doc.addField("name", "default name attribute field");
        doc.addField("description", "The default field representing description information");
        solr.add(doc);
        solr.commit();
    }
    // add dynamic index property
    public void addDynamicField() throws SolrServerException, IOException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "adwweqwewe");
        doc.addField("nam_s", "The StringField type format of the dynamic field is *_s");
        doc.addField("desc_s", "The TextField type format of the dynamic field is *_t");
        solr.add(doc);
        solr.commit();
    }
    // add index
    public void addIndex(Product product) throws SolrServerException, IOException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        solr.addBean(product);
        solr.commit();
    }
    //update index
    public void updateIndex(Product product) throws IOException, SolrServerException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        solr.addBean(product);
        solr.commit();
    }
    // delete index
    public void delIndex(String id) throws SolrServerException, IOException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        solr.deleteById(id);
        // solr.deleteByQuery("id:*");
        solr.commit();
    }
    //find index
    public void findIndex() throws SolrServerException {
        // Declare the address to connect to the solr server
        String url = "http://localhost:8983/solr";
        SolrServer solr = new HttpSolrServer(url);
        // Query conditions
        SolrQuery solrParams = new SolrQuery();
        solrParams. setStart(0);
        solrParams. setRows(10);
        solrParams.setQuery("name: Apple + description: brand new 4G");
        // enable highlighting
        solrParams. setHighlight(true);
        solrParams.setHighlightSimplePre("<font color='red'>");
        solrParams.setHighlightSimplePost("</font>");
        // set the highlighted field
        solrParams.setParam("hl.fl", "name,description");
        // SolrParams is a subclass of SolrQuery
        QueryResponse queryResponse = solr. query(solrParams);
        // (1) Get the result set of the query
        SolrDocumentList solrDocumentList = queryResponse. getResults();
        // (2) Get the highlighted result set
        // The key of the first Map is the ID of the document, and the key of the second Map is the highlighted field name
        Map<String, Map<String, List<String>>> highlighting = queryResponse. getHighlighting();
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println("======" + solrDocument.toString());
            Map<String, List<String>> fieldsMap = highlighting. get(solrDocument. get("id"));
            List<String> highname = fieldsMap.get("name");
            List<String> highdesc = fieldsMap.get("description");
            System.out.println("highname=======" + highname);
            System.out.println("highdesc======" + highdesc);
        }
        // (3) Encapsulate the response result into a Bean
        List<Product> products = queryResponse. getBeans(Product. class);
        System.out.println(products + " + + + + + + + + + + + + ");
        for (Product product : products) {
            System.out.println(product);
        }
    }
}

test:

SolrDaoTest

package liuxun.solrj.junit;
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServerException;
import org.junit.Before;
import org.junit.Test;
import liuxun.solrj.bean.Product;
import liuxun.solrj.dao.SolrDao;
public class SolrDaoTest {
    private SolrDao solrDao = new SolrDao();
    @Test
    public void testAddDefaultField() throws SolrServerException, IOException {
        solrDao.addDefaultField();
    }
    @Test
    public void testAddDynamicField() throws SolrServerException, IOException {
        solrDao.addDynamicField();
    }
    @Test
    public void testAddIndex() throws SolrServerException, IOException {
        for(int i=1;i<=2;i + + ){
            Product product=new Product();
            product.setId(String.valueOf(i));
            product.setName("Apple (Apple) iPhone 5c 16G version 3G mobile phone (white) telecom version");
            product.setDesc("Choose the "Purchase Free Edition" Beijing users enjoy the new 4G contract package with the lowest tariff in the whole network!");
            product.setPrice(9f);
            solrDao.addIndex(product);
        }
    }
    @Test
    public void testUpdateIndex() throws IOException, SolrServerException {
        Product product=new Product();
        product.setId(String.valueOf(2));
        product.setName("Sweater sweater black sweater");
        product.setDesc("Ms. only three o'clock..");
        product.setPrice(9f);
        solrDao. updateIndex(product);
    }
    @Test
    public void testDelIndex() throws SolrServerException, IOException {
        solrDao.delIndex("2");
    }
    @Test
    public void testFindIndex() throws SolrServerException {
        solrDao. findIndex();
    }
}
Console print:

Search in Solr graphical interface:

Solr installation plug-in and database table synchronization

Modify solrconfig.xml and add the following code:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
</requestHandler>

Then add the data-config.xml declared above under the directory where the solrconfig.xml file counts. The content is as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
          driver="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/solr_test"
          user="root"
          password="root"/>
<document>
    <entity name="id" query="select id,name,manu from solr" dataSource="JdbcDataSource"></entity>
</document>
</dataConfig>

Because you need to connect to the database, you need to connect to the jar package of the database and the jar package for operating the index library. Create a lib directory under the collection and copy the jar package to the lib directory (solr-dataimporthandler-4.7.0, solr-dataimporthandler-extras-4.7 .0 and the jar package connecting mysql)

The configured directory is as follows

Test import:

Check whether the import is successful