如何将两个文档作为一个文档插入到solr

How to insert two docs to solr as one document

我有两个文件。一份文件包含人员姓名、相应级别和文档 ID,此文件为 csv 格式。相同的屏幕截图如下。

另一组文档包含段落。这是一组其他文件的截图,这些文件被命名为 doc id 并且是文本格式。

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

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

我怎样才能做到这一点。另外,我想知道是否有其他方法可以遵循?

在您的情况下,您可以构建 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 管理页面查询到 solr 来验证数据是否在 solr 中建立索引。 请参考下图:

注意:我建了一个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>