Using SOLR to create an index from a database

Source: Internet
Author: User
Tags solr

SOLR is very powerful and can be used to create indexes for various types of data. In common enterprise applications, you can create database indexes. You can use a Write Program to read 100 pieces of data, if your memory is large enough, it can be 1000 or more entries, put them into the collection, and submit them to SOLR in batches. Or read the data and write it into the XML file, and then submit the file to SOLR. However, SOLR only provides the database index creation function. The steps are as follows:

1. example provided by SOLR
Preparations:
1. Download apache-solr-1.3.0.zip on Apache Official Website
2. Download the MySQL and hsql drivers (which will be used in the following example)

Environment Description:
1. tomcat version 6.0.29 location: D:/tomcat6.0.29 set the environment variable: catalina_home = D:/tomcat6.0.29
2. jdk1.5

Steps:
1、uncompress apache-solr-1.3.0.zip copy apache-solr-1.3.0/example/SOLR folder to D:/tomcat6.0.29, directory structure: D:/tomcat6.0.29/SOLR
2, copy apache-solr-1.3.0/Dist/apache-solr-1.3.0.war to D:/tomcat6.0.29/webapps/, Bing will change the apache-solr-1.3.0.war to SOLR. war, Tomcat will automatically decompress the file, generate the SOLR folder, copy the MySQL, hsql driver package downloaded above to D:/tomcat6.0.29/webapps/SOLR/WEB-INF/lib
3. Copy apache-solr-1.3.0/example/SOLR content to D: /tomcat6.0.29/SOLR inside -- (the third step of the original article ends here-but because the fourth step cannot continue, so I downloaded the content in the apache-solr-1.3.0/example-dih/SOLR copy to D: /tomcat6.0.29/SOLR)

4. delete D:/tomcat6.0.29/SOLR/RSS, and modify D:/tomcat6.0.29/sorl/SOLR. XML file, remove the line <core default = "true" instancedir = "RRS" name = "RRS"> </Core>
5. Copy the hsqldb.zip directory of the apache-solr-1.3.0/example/example-dihto D:/tomcat6.0.29. decompress hsqldb.zip to the current folder and change the file to the hsql database file.
6. Now, complete,

Run the test:
Start-run cmd to enter the command line, switch the directory to D:/tomcat6.0.29, and enter bin/startup. BAT: Start tomcat6. If the configuration above is correct, check the Tomcat log to see if there is any error. After confirming that there is no error, enter http: // 127.0.0.1: 8080/SOLR/DB/dataimport in the browser? Command = Full-import, you can see the output

Ii. Test the MySQL database
Environment and the created folder
Steps:
1. Create the mysqltest folder under D:/tomcat6.0.29/SOLR, Copy D:/tomcat6.0.29/SOLR/dB to D:/tomcat6.0.29/SOLR/mysqltest
2. Modify the D:/tomcat6.0.29/SOLR. xml file
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<SOLR sharedlib = "lib" persistent = "true">
<Cores adminpath = "/admin/cores">
<Core default = "true" instancedir = "mysqltest" name = "mysqltest"> </Core>
</Cores>
</SOLR>
3, modify D:/tomcat6.0.29/SOLR/mysqltest/CONF/db-data-config.xml content is:
<Dataconfig>
<Datasource type = "jdbcdatasource" driver = "com. mySQL. JDBC. driver "url =" JDBC: mysql: // 127.0.0.1: 3306/test "user =" root "Password =" "/>
<Document name = "mybook">
<Entity name = "book" PK = "ID" query = "select * from book">
<Field column = "ID" name = "ID"/>
<Field column = "bookname" name = "bookname"/>
<Field column = "bookathor" name = "bookathor"/>
</Entity>
</Document>
</Dataconfig>
4. Because the bookname and bookathor2 fields do not exist in D:/tomcat6.0.29/SOLR/mysqltest/CONF/Schema. XML, two fields must be added.
<Field name = "bookname" type = "string" indexed = "true" stored = "true" required = "true"/>
<Field name = "bookathor" type = "string" indexed = "true" stored = "true" required = "true"/>
Note: Add <field to the file,

5. Set D:/tomcat6.0.29/SOLR/mysqltest/CONF/solrconfig. xml
<Datadir >$ {SOLR. Data. dir:./SOLR/DB/Data} </datadir>
Change to <datadir >$ {SOLR. Data. dir:./SOLR/mysqltest/Data} </datadir>

6. This completes
Run the test:
Start-run cmd to enter the command line, switch the directory to D:/tomcat6.0.29, and enter bin/startup. bat, start tomcat6. If the above configuration is correct, you can check the Tomcat log to see if there is any error. After confirming that there is no error, enter http: // 127.0.0.1: 8080/SOLR/MySQL in the browser... command = Full-import, you can see the output. Through the above access, the database index is created
You can access http: // 127.0.0.1: 8080/SOLR/mysqltest/admin/And Enter ID: search content in query string (Format: Search Keyword) to test whether the data in the data table has been indexed. You can also modify the search parameters on this page.

Finally, let's talk about this function. The above example is just a simple part. For SOLR multicore, you can also achieve it by configuring the db-data-config.xml, there are multiple tables, or multi-Table Association, and so on, as long as the db-data-config.xml configuration is clear, you can import data.
There are more extensions in solr1.4, which provide convenient operations for re-indexing. In addition, datasource not only refers to databases, but also XML files and networks.

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.