SOLR Connection Database

Source: Internet
Author: User
Tags solr

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
  1. <requesthandler name="/dataimport" class=" Org.apache.solr.handler.dataimport.DataImportHandler ">
  2. <lst name="Defaults">
  3. <str name="config">data-config.xml</str>
  4. </lst>
  5. </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
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <dataconfig>
  3. <dataSource type="Jdbcdatasource"
  4. driver="Net.sourceforge.jtds.jdbc.Driver"
  5. url="Jdbc:jtds:sqlserver://localhost/myhousekeeper"
  6. user="sa"
  7. password="123456"/>
  8. <document>
  9. <entity name="pay" query="Select Payid,payname,paymoney,paydescription,paydatetime from T_pay ">
  10. <field column="Payid" name= "id" />
  11. <field column="payname" name= "name" />
  12. <field column= "Paymoney" name= "Money" /> /c7>
  13. <field column= "paydescription" name="description" />
  14. <field column="Paydatetime" name="datetime" />
  15. </Entity>
  16. </Document>
  17. </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
  1. <types>
  2. <fieldtype name="string" class="SOLR. Strfield " sortmissinglast=" true " omitnorms=" true "/>
  3.    <fieldtype  name= "long"      class= SOLR. Trielongfield "  precisionstep=" 0 " positionincrementgap= "0" />  
  4.    <fieldtype name = "float"    class= precisionstep=" 0 " positionincrementgap=" 0 "/>&NBSP;&NBSP;
  5. <fieldtype name="date" class="SOLR. Triedatefield " precisionstep=" 0 " positionincrementgap=" 0 "/>
  6. <fieldtype name="Text_ik" class= "SOLR. TextField ">
  7. <analyzer class="Org.wltea.analyzer.lucene.IKAnalyzer"/>
  8. </fieldtype>
  9. </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
  1. <fields>
  2. <field name="_version_" type="Long" indexed="true" stored="true" />
  3.   <field name= "id"                type= "long"         indexed= "true"    Stored= "true"   multivalued= "false"  required= " True "/>  "
  4.   <field  Name= "name"              type= "string"      indexed= "true"   stored= "true"   multivalued= " False " />   
  5.   <field name= "money"             type= "float"       indexed= "true"   stored= "true"   multivalued= " False " />   
  6.   <field  Name= "description"      type=< Span class= "Attribute-value" > "Text_ik"     indexed=" true "  stored=" true "   multivalued= "false"  />    
  7. <field name="datetime" type="date" indexed="true" stored="true" multivalued="false" />
  8. </fields>


Where the following field is required for marking version information, maintained by SOLR internally itself.

[HTML]View Plaincopy
    1. <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.