In the previous article we introduced the SOLRJ tool, in this article we introduced the SOLR data source. We use SOLR as the server for Full-text indexing, then we have to provide a data source for SOLR, and the small-format SOLR server summarizes three sources of data: Using commands to provide a data source, SOLRJ providing a data source, and a configuration file configuration data source, which is described in three different ways.
Configure a data source with a command
1 environment
This article introduces the Linux environment as an example of this feature, the operating environment CentOS.
2 Data sources
As an example data source, the XML file provided by the Apache official demo is available.
3 implementation
Java-jar Post.jar Solr.xml Monitor.xml
Monitor data
4 test
Note: We use the NAME=3007WFP condition to query the data and get the result as shown in the above figure.
Two uses the SOLRJ configuration data source 1 environment
uses the Httpsolrserver object, needs to introduce the Solr-solrj-4.0.0.jar file.
2 data sources
We construct the Solrinputdocument object directly as the data source for the SOLR engine.
3 Implementation
String url = "HTTP://192.168.22.216:8983/SOLR";
Httpsolrserver Server = new Httpsolrserver (URL); Server.setsotimeout (3000);
Socket read Timeout server.setconnectiontimeout (1000);
Server.setdefaultmaxconnectionsperhost (1000);
Server.setmaxtotalconnections (10); Server.setfollowredirects (FALSE);
Defaults to False server.setallowcompression (true);
Server.setmaxretries (1);
Tectonic Document1 solrinputdocument Doc1 = new Solrinputdocument ();
Doc1.addfield ("id", "id1", 1.0f);
Doc1.addfield ("name", "Doc1", 1.0f);
Doc1.addfield ("Price", 10);
Tectonic Document2 solrinputdocument doc2 = new Solrinputdocument ();
Doc2.addfield ("id", "id2", 1.0f);
Doc2.addfield ("name", "Doc2", 1.0f);
Doc2.addfield ("Price", 20);
Construct Document Set collection<solrinputdocument> docs = new arraylist<solrinputdocument> ();
Docs.add (Doc1);
Docs.add (DOC2);
Submit documents to SOLR try {server.add (docs); catch (Solrserverexception E1) {E1.priNtstacktrace ();
catch (IOException E1) {e1.printstacktrace ();
//Submit a Commit (method one) try {server.commit ();
catch (Solrserverexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace (); }
4 test
Description
As you can see, we query the first record by using the Name=doc1 query criteria. Configuring a data source using a configuration file
1 environment
Operating system: CentOS
Database: MySQL
2 Data sources
Use a table of the database as the data source for the SOLR server.
3 implementation
1> Create MySQL Database
Database:jfinal_demo
Table Name:user
The SQL script is as follows:
SET foreign_key_checks=0;
------------------------------
--
Table structure for ' user '
------------------------------
DROP TABLE IF EXISTS ' user ';
CREATE TABLE ' user ' (
' id ' int () NOT NULL auto_increment,
' userName ' varchar ' DEFAULT null,
' Userage ' in T (one) default null,
' useraddress ' varchar default NULL,
PRIMARY KEY (' id ')
Engine=innodb Auto_increment=6 DEFAULT Charset=utf8;
-------------------------------
-
Records of user
------------------------------
INSERT into ' User ' VALUES (' 1 ', ' test1 ', ' One ', ' Suzhou ');
INSERT into ' user ' VALUES (' 2 ', ' test2 ', ' n ', ' Shanghai ');
INSERT into ' user ' VALUES (' 3 ', ' test3 ', ', ', ' Guangzhou ');
INSERT into ' user ' VALUES (' 4 ', ' test4 ', ' n ', ' Shenzhen ');
INSERT into ' user ' VALUES (' 5 ', ' test5 ', ', ', ' Beijing ');
Database screenshot:
2> configuration Data source according to the/usr/local/solr-4.7.2/example/solr/collection1/conf/directory, locate solr-config.xml configuration file, in <requesthandler
Name= "/select" class= "SOLR. Searchhandler "> Front, add dataimport processing handler.
<span style= "White-space:pre" > </span><requesthandler name= "/dataimport" Org.apache.solr.handler.dataimport.DataImportHandler ">
<lst name=" Defaults ">
<str name=" Config ">data-config.xml</str>
</lst>
</requestHandler>
As shown in the following illustration:
Description
This profile is used to configure the data source for the SOLR server, that is, the data sources can be obtained from the database.
3> Configuration Data source
Find Data-config.xml in the same directory, if not, add one and configure as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<dataConfig>
<datasource type= "Jdbcdatasource" Driver= "Com.mysql.jdbc.Driver" url= "Jdbc:mysql://192.168.21.20:3306/jfinal_demo" user= "root" password= " 123456 "batchsize="-1 "/>
<document name=" Testdoc ">
<entity name=" user "pk=" id "
query=" SELECT * from user "> <field column=" id "name=" id "/> <field column=" UserName "Name=" UserName "/> <field column=" userage "name=" Userage "/> <field column=" useraddress "name="
UserAddress "/> </entity>
</document>
</dataConfig>
As shown in the following illustration:
Description
This configuration is used to configure database data sources, similar to persistent layer configuration files.
DataSource is used to configure servers, database drivers, users, passwords, etc.
Entity: Used to configure a table, corresponding to an entity, PK as the primary key, query for the search statement.
Field: The fields of the corresponding entity, column corresponds to the field name in the database, and name corresponds to the index name of the SOLR service.
4> Configuration Index
Locate the Schema.xml under the same directory, configured as follows:
(1) Keep _version_ this field;
(2) Add an indexed field: Here the name of each field is consistent with the name of the entity field in Data-config.xml.
<span style= "White-space:pre" > </span><field name= "UserName" type= "Text_general" indexed= "true" Stored= "true"/>
<field name= "userage" type= "int" indexed= "true" stored= "true"/>
As shown in the following illustration:
Description
Because the id attribute has already been configured, there is no need to configure it again, otherwise the SOLR service will be restarted with an error.
(3) Remove the extra field and remove the settings from the Copyfield, which are not available. Note: text This field cannot be deleted, or the SOLR service reboot fails.
(4) Setting unique PRIMARY key: <uniqueKey>id</uniqueKey>
Note: The primary key of the index in the SOLR service only supports type= "String" By default, Workaround: Modify the Elevate.xml in the same directory and comment out the following 2 lines.
As shown in the figure:
5> Configure Jar pack copies Mysql-connector-java-5.1.22-bin.jar and Solr-dataimporthandler-4.10.3.jar to/usr/local/solr-4.7.2\ Example\solr-webapp\webapp\web-inf\lib. One is the Java driver for MySQL, and the other is the jar where Org.apache.solr.handler.dataimport.DataImportHandler is located in the/usr/local/solr-4.7.2\dist directory.
6> Restart SOLR Service
4 test
Four summary
The first way is simple, and the data is usually not changed, suitable for beginners test server; The second way the data is uncertain, but the range is small, is usually used to simulate a situation, is suitable for the debugger, the third way data is uncertain, the index maintenance is complex, but the function is powerful, it is suitable for the general Full-text indexing program to use.
Statement: If no special statement, this series of blogs to solr-4.7.2 version For example, if there is a mistake, please treatise.