DataImportHandler is not indexing mysql table in s

2019-05-10 11:57发布

问题:

I am trying to index the mysql table in solr using DataImportHandler, but it's seems not indexing

data-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
    <dataConfig>
      <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
                  url="jdbc:mysql://localhost/solr_tut" 
                  user="root" 
                  password=""/>
      <document>
        <entity name="product_id" 
                query="select product_id,name,description from products">
        </entity>
      </document>
    </dataConfig>

solrconfig.xml

<lib dir="../../../contrib/dataimporthandler/lib/" regex=".*\.jar" />
<lib dir="../../../dist/" regex="solr-dataimporthandler-\d.*\.jar" />


<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
       <lst name="defaults">
          <str name="config">data-config.xml</str>
      </lst>
 </requestHandler>

When i try to index in solr admin(http://localhost:8080/solr/dataimport?command=full-import) i am getting this response

<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">19</int>
</lst>
<lst name="initArgs">
 <lst name="defaults">
  <str name="config">data-config.xml</str>
 </lst>
</lst>
<str name="command">full-import</str>
<str name="status">idle</str>
<str name="importResponse"/>
<lst name="statusMessages">
   <str name="Total Requests made to DataSource">1</str>
   <str name="Total Rows Fetched">4</str>
   <str name="Total Documents Skipped">0</str>
   <str name="Full Dump Started">2014-01-10 10:38:00</str>
   <str name="">
     Indexing completed. Added/Updated: 0 documents. Deleted 0 documents.
   </str>
   <str name="Committed">2014-01-10 10:38:00</str>
   <str name="Total Documents Processed">0</str>
   <str name="Time taken">0:0:0.33</str>
</lst>
<str name="WARNING">
This response format is experimental. It is likely to change in the future.
</str>
</response>

After if i search(http://localhost:8080/solr/select?q=*:*), i am getting 0 result.

Update-1: schema.xml

回答1:

You just missed the mapping of the columns in the result set to the documents fields. You need to do that within the entity element of your data-config.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
  <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost/solr_tut" 
              user="root" 
              password=""/>
  <document>
    <entity name="product_id" 
            query="select product_id,name,description from products">

 <!-- this is the place where you map the columns of your result set
      to fields of the new solr document -->

        <field column="PRODUCT_ID" name="id" />
        <field column="NAME" name="name" />
        <field column="DESCRIPTION" name="description" />

    </entity>
  </document>
</dataConfig>

In your case there is one vital mapping you missed. product_id to id. Solr can autodetect mappings, if the column name and the name of the field in the schema are equal, as is written in the wiki

In the above example, there are mappings of fields to Solr fields. It is possible to totally avoid the field entries in entities if the names of the fields are same (case does not matter) as those in Solr schema.

But as said, in your situation that is not the case. product_id and id do differ. Since your id field is required those documents will not make it into the index.

More information can be found in Solr's Wiki about the DataImportHandler or in the reference guide.