SOLR Study Notes (2) Examples of SOLR
The database I use here is mysql. First, integrate MySQL
1. Create a table
-- ------------------------------ Table structure for `documents`-- ----------------------------DROP TABLE IF EXISTS `documents`;CREATE TABLE `documents` ( `id` int(11) NOT NULL auto_increment, `date_added` datetime NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of documents-- ----------------------------INSERT INTO `documents` VALUES ('1', '2012-01-11 23:15:59', 'world', 'test1');INSERT INTO `documents` VALUES ('2', '2012-01-11 23:16:30', 'hello', 'test');
2. Add dataimporthandler in SOLR \ conf \ solrconfig. xml.
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst></requestHandler>
(1) If the following error occurs,
Severe: org. Apache. SOLR. Common. solrexception: error loading class 'org. Apache. SOLR. handler. dataimport. dataimporthandler'
You need to add a configuration similar to this in the previous section:
<Lib dir = "E:/WinDriver/SOLR/apache-solr-3.6.0/Dist/" RegEx = "Apache-SOLR-dataimporthandler-\ D. * \. Jar"/>
3. Create a data-config.xml under the SOLR/conf directory at the same time
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="test" password="test" /> <document name="documents1" > <entity name="documents" query="select id,title,content,date_added from documents" deltaImportQuery="select id,title,content,date_added from documents where ID='${dataimporter.delta.id}'" deltaQuery="select id from documents where date_added > '${dataimporter.last_index_time}'" deletedPkQuery="select id from documents where id=0"> <field column="id" name="id" /> <field column="title" name="title" /> <field column="content" name="content" /> <field column="date_added" name="date_added" /> </entity> </document></dataConfig>
The above specifies the database connection path.
Query is the SQL statement used to import data to the index for the first time.
Deltaimportquery obtains the index data according to the ID.
Deltaquery is an SQL statement used to obtain the ID of an index.
Deletedpkquery is used to retrieve the ID of the document to be deleted from the index.
4. Specify the index type in schema. xml
<field name="id" type="string" indexed="true" stored="true" required="true" /><field name="title" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/><field name="content" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/><field name="date_added" type="date" indexed="false" stored="true"/>
(1) The following error is reported:
Severe: org. Apache. SOLR. Common. solrexception: Undefined field text
You need to add a Section
<Field name = "text" type = "text_general" stored = "false" indexed = "true" multivalued = "true"/>
<Defasearchsearchfield> text </defasearchsearchfield>
<Copyfield source = "title" DEST = "text"/>
<Copyfield source = "content" DEST = "text"/>
5. Run the URL
Http: // localhost: 8080/SOLR/dataimport? Command = Full-Import