如何将两个文档插入到一个文档中

时间:2019-05-24 21:32:23

标签: solr

我有两个文件。一个文档包含人员名称,相应的职级和doc ID,此文档为csv格式。相同的屏幕截图如下。 enter image description here

另一组文档包含段落。这是其他文档的屏幕快照,这些文档被称为doc id,并且为文本格式。 enter image description here

我需要将这两个文档作为一个文档插入solr中,以便在solr中我具有以下格式的文档:

Person: arthur w cabot
KDE Rank: 5.98+108
Text: Text from the other set of documents

如何实现这一目标。另外,我想知道是否还有其他可以遵循的方法?

1 个答案:

答案 0 :(得分:3)

根据您的情况,您可以构建solr文档并将其提交给solr。 如下所示:

SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("title", fileName);
document.addField("text", contentBuilder.toString());
solr.add(document);
solr.commit();

在您的情况下,字段为personName和personRank以及documentContent。 我假设将从头开始读取csv文件,并且您将检索文档名称,并且您已经知道文档的位置。

如前所述,您可以读取csv文件,直接将personName的数据输入PersonRank。

第三个是关于现场文档的内容。因为仅获得文档文件名,所以您可以阅读文档的内容并将其作为第三个字段传递给solr文档。

我为您做了一个选择。如下所示:

String urlString = "http://localhost:8983/solr/TestCore";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();

StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get("D:/LogFolder/IB4_buildViewSchema.txt"),
StandardCharsets.UTF_8)) {
  stream.forEach(s -> contentBuilder.append(s).append("\n"));
  } catch (IOException e) {
    e.printStackTrace();
  }

try {
    File file = new File("D:/LogFolder/IB4_buildViewSchema.txt");
    String fileName = file.getName();
    SolrInputDocument document = new SolrInputDocument();
    document.addField("id", "123456");
    document.addField("title", fileName);
    document.addField("text", contentBuilder.toString());
    solr.add(document);
    solr.commit();
} catch (SolrServerException | IOException e) {
    e.printStackTrace();
}

这将以迭代方式处理csv的所有数据。

检查是否可以批量处理,并且还需要寻找优化代码。 这段代码不能完全解决您的问题。

我通过solr admin页面查询到solr来验证数据是否在solr中建立了索引。 请参考下图:

Solr Admin Page

注意:我构建了一个maven项目并编写了上面的代码。如果需要,可以使用下面的pom.xml作为参考。

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>solr</groupId>
    <artifactId>TestSolr2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestSolr2</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>7.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-cell</artifactId>
            <version>7.6.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>