Solr:如何在JSON和CSV导入期间指定模式?

Solr:如何在JSON和CSV导入期间指定模式?,json,csv,solr,Json,Csv,Solr,我是Solr的新手,我正在尝试测试它的功能。我来自RDBMS世界,想知道Solr将如何处理我的数据 我创建了一个新的核心: $bin/solr create-c测试 并使用以下命令成功加载JSON文件: $bin/post-c测试文件.json file.json的第一条记录如下所示: {“attr”:“01234”} 但Solr将其存储为: {“attr”:1234} 为了正确存储数据,我开始定义一个数据导入处理程序,发现DIH无法处理JSON。我一直停留在data config.xml的定义

我是Solr的新手,我正在尝试测试它的功能。我来自RDBMS世界,想知道Solr将如何处理我的数据

我创建了一个新的核心:

$bin/solr create-c测试

并使用以下命令成功加载JSON文件:

$bin/post-c测试文件.json

file.json的第一条记录如下所示:

{“attr”:“01234”}

但Solr将其存储为:

{“attr”:1234}

为了正确存储数据,我开始定义一个数据导入处理程序,发现DIH无法处理JSON。我一直停留在
data config.xml
的定义上,因为本教程使用
xpathenticprocessor
处理xml文件,但找不到JSON甚至CSV处理器(我可以轻松检索
file.JSON
的CSV版本,因此加载CSV或JSON对我来说是一样的)。官方文档有点混乱,没有提供很多有用的例子。唯一可能处理JSON和CSV文档的处理器是
LineEntityProcessor
PlainTextEntityProcessor
()

从Solr Wiki中可以看出:

目标

可以插入任何类型的数据源(ftp、scp等)和用户选择的任何其他格式(JSONcsv等)

所以我想这确实是可能的,但是怎么可能呢

我在2014年的帖子中发现了一个没有人回答的问题,所以我想知道2016年,对于更新版本的Solar,是否有一个众所周知的解决方案

所以问题是:如何使用特定的数据模式导入JSON和CSV文档

更新 执行
http://localhost:8983/solr/test/dihupdate?command=full-导入
不会触发任何错误,但不会加载任何文档。以下是位于核心目录中的各种xml文件:

solrconfig.xml

...
<schemaFactory class="ClassicIndexSchemaFactory" />
...
<requestHandler name="/dihupdate" class="org.apache.solr.handler.dataimport.DataImportHandler" startup="lazy">
  <lst name="defaults">
    <str name="config">data-config.xml</str>
  </lst>
</requestHandler>
...
...
<field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
<field name="attr1" type="string" indexed="true" stored="true" required="true" multiValued="true" />
<field name="date" type="date" indexed="true" stored="false" multiValued="true" />
<field name="attr2" type="string" indexed="true" stored="true"  multiValued="true" />
<field name="attr3" type="string" indexed="true" stored="true" multiValued="true" />
<field name="attr4" type="int" indexed="false" stored="true" multiValued="true" />
<uniqueKey>id</uniqueKey>
...
<dataConfig>
    <dataSource type="FileDataSource" />
    <document>
        <entity name="f" processor="FileListEntityProcessor"
                fileName="test.json"
                rootEntity="false"
                dataSource="null"
                recursive="true"
                baseDir="/path/to/data/"/>
    </document>
</dataConfig>
数据配置.xml

...
<schemaFactory class="ClassicIndexSchemaFactory" />
...
<requestHandler name="/dihupdate" class="org.apache.solr.handler.dataimport.DataImportHandler" startup="lazy">
  <lst name="defaults">
    <str name="config">data-config.xml</str>
  </lst>
</requestHandler>
...
...
<field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
<field name="attr1" type="string" indexed="true" stored="true" required="true" multiValued="true" />
<field name="date" type="date" indexed="true" stored="false" multiValued="true" />
<field name="attr2" type="string" indexed="true" stored="true"  multiValued="true" />
<field name="attr3" type="string" indexed="true" stored="true" multiValued="true" />
<field name="attr4" type="int" indexed="false" stored="true" multiValued="true" />
<uniqueKey>id</uniqueKey>
...
<dataConfig>
    <dataSource type="FileDataSource" />
    <document>
        <entity name="f" processor="FileListEntityProcessor"
                fileName="test.json"
                rootEntity="false"
                dataSource="null"
                recursive="true"
                baseDir="/path/to/data/"/>
    </document>
</dataConfig>

conf
目录中的
schema.xml
中定义模式-这是。如果您使用的是当前默认的“托管架构”模式,则必须。然后,您可以按照示例模式或web上描述schema.xml文件结构的任何可用资源(定义字段类型,然后定义使用该字段类型的字段)定义
schema.xml中的字段

另一个选项是托管模式——这是最新版本中的默认模式,该模式通过Solr提供的API进行操作。启动时,它从schema.xml(如果存在)中读取初始模式,但之后必须通过API或管理接口对其进行修改。《Solr指南》中的中介绍了此API(带有示例)


使用StrField(字段类型
string
使用的)存储
012345
将导致Solr只存储文本值
012345
,而不将其转换为整数。这可能是一个很好的起点。

在Solr发行版中,有一个films示例(在示例/films中)展示了如何索引JSON,并利用精确的字段定义和类型自动检测。说明(README.txt)包括如果忘记执行其中一个步骤,您将看到的结果


我建议您尝试一下,然后将这些知识应用到您自己的用例中。

我遵循了您的指示,但我必须指定JSON/CSV数据的位置。我用各种XML参数更新了我的问题。谢谢如果您有JSON数据,您可以直接将其提交给更新处理程序,只要它符合Solr期望的JSON格式,并且符合您定义的模式。这正是我试图做的,作为缺少处理器的替代方法,但我遇到了以下错误:
{“responseHeader”:{“status”:400,“QTime”:4},“error:{”metadata:[“error class”,“org.apache.solr.common.SolrException”,“root error class”,“org.apache.solr.common.SolrException”],“msg:“Unknown command'id'at[5],“code”:400}
。JSON语法正确(文件由PostgreSQL生成),前面的行不会触发该错误。.生成错误的行是
{”id“:400,…}
并且没有嵌套的属性。哪个版本的Solr?该版本可能假定文档包装在{'add':…}中元素。请参阅下面的@Alexandre Rafalovitch建议,我使用API和托管模式在导入数据之前添加字段定义。感谢您的帮助,我学到了很多新东西:)我通过
{“add field”:{…}添加字段定义实现了这一点
README.txt
文件所述。谢谢