In practical engineering applications, it is common to create an index from a database to export data. Now try to create an index from the database import data.
First, Release Notes
SOLR version: 4.7.0
Database: sqlserver2005
Second, the configuration steps
1. Prepared jar Package
1) Solr-dataimporthandler-extras-4.7.0.jar; in the SOLR release package solr-4.7.0\dist there are
2) Solr-dataimporthandler-4.7.0.jar; in the SOLR release package solr-4.7.0\dist there are
3) Jtds-1.2.2.jar; find yourself online
2. Modify SOLR's core configuration
Which core should be modified to change the configuration of the core from the database when it is imported into the data index.
2.1 Modifying Solrconfig.xml
Add the following configuration:
[HTML]View Plaincopy
- <requesthandler name="/dataimport" class=" Org.apache.solr.handler.dataimport.DataImportHandler ">
- <lst name="Defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requesthandler>
2.2 Adding Data-config.xml
Create the Data-config.xml in the same folder in Solrconfig.xml, which is configured as follows:
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="UTF-8" ?>
- <dataconfig>
- <dataSource type="Jdbcdatasource"
- driver="Net.sourceforge.jtds.jdbc.Driver"
- url="Jdbc:jtds:sqlserver://localhost/myhousekeeper"
- user="sa"
- password="123456"/>
- <document>
- <entity name="pay" query="Select Payid,payname,paymoney,paydescription,paydatetime from T_pay ">
- <field column="Payid" name= "id" />
- <field column="payname" name= "name" />
- <field column= "Paymoney" name= "Money" /> /c7>
- <field column= "paydescription" name="description" />
- <field column="Paydatetime" name="datetime" />
- </Entity>
- </Document>
- </dataconfig>
2.3 Modifying Schema.xml
The purpose of this modification is to let SOLR know which field it is, whether it needs to be indexed, whether it needs to store the original text in the index library, and the field type. There are a number of data types in the SQL above.
Payid: Integral type
Payname: Character type
Paymoney: Floating point
Paydescription: Large text
Paydatetime: Date Time format
First, declare the field type in Schema.xml and configure it in <types> </types>. The following configuration of the sring type is not done word processing, as a complete word, text_ik is a Chinese word breaker ik-analyzer, specializing in Chinese word segmentation.
As follows:
[HTML]View Plaincopy
- <types>
- <fieldtype name="string" class="SOLR. Strfield " sortmissinglast=" true " omitnorms=" true "/>
- <fieldtype name= "long" class= SOLR. Trielongfield " precisionstep=" 0 " positionincrementgap= "0" />
- <fieldtype name = "float" class= precisionstep=" 0 " positionincrementgap=" 0 "/>&NBSP;&NBSP;
- <fieldtype name="date" class="SOLR. Triedatefield " precisionstep=" 0 " positionincrementgap=" 0 "/>
- <fieldtype name="Text_ik" class= "SOLR. TextField ">
- <analyzer class="Org.wltea.analyzer.lucene.IKAnalyzer"/>
- </fieldtype>
- </Types>
Then declare that the name of the Field,field should be the same as the SQL query result set column name, if not consistent, you need to specify the column and field in the entity tag in the Data-config.xml field of the corresponding relationship.
The field is configured as follows:
[HTML]View Plaincopy
- <fields>
- <field name="_version_" type="Long" indexed="true" stored="true" />
- <field name= "id" type= "long" indexed= "true" Stored= "true" multivalued= "false" required= " True "/> "
- <field Name= "name" type= "string" indexed= "true" stored= "true" multivalued= " False " />
- <field name= "money" type= "float" indexed= "true" stored= "true" multivalued= " False " />
- <field Name= "description" type=< Span class= "Attribute-value" > "Text_ik" indexed=" true " stored=" true " multivalued= "false" />
- <field name="datetime" type="date" indexed="true" stored="true" multivalued="false" />
- </fields>
Where the following field is required for marking version information, maintained by SOLR internally itself.
[HTML]View Plaincopy
- <field name="_version_" type="Long" indexed="true" stored="true" />
Third, import test
Access to the SOLR management interface, command select Full-import all import, entity needs to import entities, that is, which SQL to configure, click Execute to perform the import, if there is a lot of data need to lead a period of time, occasionally point to refresh Status Refresh to see if the import is complete and the import will tell you how much data was imported and how long it took. Such as
To do a query test,
Q,description: M; query description The field name of the meter related data
Wt,json; return format of query results, default JSON
Execute query, execute the queries, and see the results of the returned JSON-formatted query.
Reference documents:
1) http://www.chepoo.com/solr4-database-import-create-index.html
2) http://blog.csdn.net/bruce128/article/details/17796705
SOLR Connection Database