使用solr对MySQL进行索引和搜索

4

大家好,我想使用solr对我的MySQL数据库表进行索引。我已经安装了必要的Java组件/适配器等。我的数据库名为“test_db”,其中的表名为“table_tb”。该表包含两列(字段)

-第1个字段名为“ID”,是自增的主键整数 -第2个字段名为“COLA”,是文本

该表有两行记录,ID = 1和ID = 2,每行都对应第二列中的一些文本。我已经设置了以下配置文件(它们位于正确的目录中):

data-config.xml

<dataConfig>
  <dataSource type="JdbcDataSource"
              driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost/test_db"
              user="username"
              password="db_pwd"/>

<document name="doc">

<entity name="test_tb" query="select ID from test_tb">
        <field column="ID" name="ID" />
        <field column="COLA" name="COLA" />
</entity>

  </document>
</dataConfig>

solrconfig.xml

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

schema.xml

 <fields>
        <field name="ID" type="int" indexed="true" stored="true" required="true"/>
        <field name="COLA" type="string" indexed="true" stored="true" required="true"/>
 </fields>

 <uniqueKey>ID</uniqueKey>

当我在浏览器中输入"[URL]:8983/solr/dataimport?command=full-import",我会得到以下输出:

(1) 浏览器输出(xml)

<response><lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</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">2</str>
<str name="Total Documents Skipped">0</str>
<str name="Full Dump Started">2010-08-03 16:15:51</str><str name="">

Indexing completed. Added/Updated: 0 documents. Deleted 0 documents.
</str>
<str name="Committed">2010-08-03 16:15:51</str>
<str name="Optimized">2010-08-03 16:15:51</str>
<str name="Total Documents Processed">0</str>
<str name="Total Documents Failed">2</str>
<str name="Time taken ">0:0:0.32</str>
</lst><str name="WARNING">
This response format is experimental.  It is likely to change in the future.
</str>
</response>

建议已读取2条记录但未索引

服务器端输出

WARNING: Error creating document : SolrInputDocument[{ID=ID(1.0)={1}}]
org.apache.solr.common.SolrException: Document [null] missing required field: id


WARNING: Error creating document : SolrInputDocument[{ID=ID(1.0)={2}}]
org.apache.solr.common.SolrException: Document [null] missing required field: id

有人知道我做错了什么吗?

提前感谢任何帮助!!!


请使用 Code Sample 按钮格式化 XML。 - Mauricio Scheffer
我遇到了类似的问题。你在哪里看到这些错误信息? - Muc
@Muc,错误信息会输出在Tomcat日志中: yourTomcatdir/logs/catalina.somedate.log - Osvaldo Mercado
3个回答

3
文档中其他地方已经存在一个名为“id”的字段。我将其注释掉后,它就可以正常工作了。

2

您需要同步:scheme.xml和data-config.xml中的内容(字段定义需要相同)


0
<entity name="test_tb" query="select ID from test_tb">
        <field column="ID" name="ID" />
        <field column="COLA" name="COLA" />
</entity>

我认为应该是query="select * from test_tb"。

如果你执行查询语句select id from test_tb,你只会得到一列,而你想要两列。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接