Build an example to import the index data to the core for later experiments.
1. Create a database
Create a database and create a few data, table structure:
ID self-increment
S_user User Information
S_text fill in some content
Note: Character Set UTF8
Add a few data
2. Configure Solrconfig.xml
Sorlconfig.xml files and Managed-shema files are frequently modified files. In the Config folder in the created core directory. For example:
D:\solrhome\core_demo
Add the following information after the Solrconfig.xml file:
<!--introduced the Dataimporthandler class of Jar--><lib dir= "${solr.install.dir:. /.. /.. /..} /dist/"regex=" Solr-dataimporthandler-.*\.jar "/><requesthandler name="/dataimport "class=" Org.apache.solr.handler.dataimport.DataImportHandler "><lst name=" defaults "><str name=" config "> Data-config.xml</str></lst></requesthandler>
where data-config.xml needs to be manually created in the Solrconfig.xml sibling directory
3. In the directory D:\solrhome\core_demo\conf create Data-config.xml temporarily do not fill in the content, later write.
4. Write the Managed_schema directory D:\solrhome\core_demo\conf
Managed_schema defines a number of domains, in fact, using the fields in Lucene.
What is a domain? The role of a domain is to define what type of field is in a SOLR index, what to do, and how to do it. A bit like the type of field in the database. But the meaning of the expression is more abundant.
Definition of a domain
Name: The names of this domain, the entire Managed_schema file needs to be unique, can not be duplicated, here is defined as the name of the database table field, easy to use. Of course, you can define other names as well.
Type: Indicates what kind of this field is, string is a string type, int is the shaping data type, date is a time type, equivalent to the timestamp in the database
Indexed: whether to index
Stored: whether to store
Multivalued: Whether it is multi-valued. Store multiple values under a domain. An array is stored under a domain.
Add the following code after Managed_schema
<!--There is no need to define an ID here, because the Managed_schema file is already defined at the beginning of the previous position, the ID is required, and the unique--><field name= "S_user" type= "string" indexed= " True " stored=" true "/><field name=" S_text "type=" string "indexed=" true "stored=" true "/>
5. Write the data-config.xml you created earlier
<dataConfig> <datasource type= "Jdbcdatasource" driver= "Com.mysql.jdbc.Driver" url= "jdbc:mysql:// 127.0.0.1:3306/test "user=" root "password=" 123456 "batchsize="-1 "/> <document> <entity name=" Mycore _test "query=" select Id,s_user,s_text from Test "> <!--column ID is the id,name ID of the database is Managed_schema inside Id,id is required and unique--- <field column= "id" name= "id"/> <field column= "S_user" nam= "S_user"/> < Field column= "S_text" name= "S_text"/> </entity> </document> </dataConfig>
dataSource Configuration database Information
document Configuration database query statement corresponds to the Managed_schema domain. The goal is that when the core imports data, it will be linked to the database through the configuration information to query the data through the query statement, through the database field and Managed_schema Domain Association to create an index
Start importing Data
Configure the previous information, you can import data in the background, configuration information need to reload the core to take effect. If there is an error in the configuration file, there will be an error message when reload.
After the reload is finished, start importing
Click Execute to start the import
If the amount of data is large can be checked, automatic refresh
If the index was established successfully.
Here are a few things to be aware of .
1.JAVA and MySQL connection jar package matches, if the mismatch will appear the following error, import failure, even if the prompt extraction succeeds, but also cannot be indexed, because the jar version is not correct.
Tomcat will also have an error message as follows:
The final solution is to replace the jar package, my native version of MySQL is
MySQL Jar package Address
D:\tomcat\webapps\solr\WEB-INF\lib
The Java version is
Now we query to see if the index is successful.
solr6.6 Tutorial-Import data from the MySQL database (iii)