我以前从未使用过 Solr,但这是使用 JavaCPP 和 Maven 解决此问题的一种方法。首先,创建这个pom.xml 构建文件:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>solrtest</artifactId>
<version>0</version>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<configuration>
<header>true</header>
<includePath>.</includePath>
<linkPath>.</linkPath>
<preloadPath>.</preloadPath>
</configuration>
<executions>
<execution>
<id>process-classes</id>
<phase>process-classes</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<artifactId>solr-core</artifactId>
<groupId>org.apache.solr</groupId>
<version>1.4.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
还有这个src/main/java/SolrTest.java源文件:
import java.io.*;
import org.apache.solr.core.*;
import org.apache.solr.client.solrj.*;
import org.apache.solr.client.solrj.embedded.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
public class SolrTest {
static SolrServer createServer(String coreName) throws Exception {
CoreContainer coreContainer = new CoreContainer();
CoreDescriptor discriptor = new CoreDescriptor(coreContainer, coreName, new File("/some/path/", coreName).getAbsolutePath());
SolrCore solrCore = coreContainer.create(discriptor);
coreContainer.register(solrCore, false);
return new EmbeddedSolrServer(coreContainer, coreName);
}
static class Query extends FunctionPointer {
public @Name("query") String call(String string) throws Exception {
createServer(string);
return string;
}
}
}
然后我们可以执行mvn package,在它为我们下载了一大堆东西并为我们构建了东西之后,我们将得到target/solrtest-0.jar以及一个头文件和一个本地库文件target/classes 子目录。现在,我们可以在 C++ 中使用这样的程序将所有这些链接在一起:
#include <iostream>
#include "target/classes/jniSolrTest.h"
int main() {
const char *argv[] = { "-Djava.class.path=target/solrtest-0.jar" };
JavaCPP_init(1, argv);
try {
query("test");
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
JavaCPP_uninit();
}
例如,我们可以在 Linux x86_64 下以这种方式编译和运行,但我们可以在其他平台上做同样的事情:
$ g++ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/ target/classes/linux-x86_64/libjniSolrTest.so query.cpp -o query
$ ./query
我们从 Solr 得到一个适当的异常:
[main] INFO org.apache.solr.core.SolrResourceLoader - JNDI not configured for solr (NoInitialContextEx)
[main] INFO org.apache.solr.core.SolrResourceLoader - solr home defaulted to 'solr/' (could not find system property or JNDI)
[main] INFO org.apache.solr.core.SolrResourceLoader - Solr home set to '/some/path/test/'
java.lang.RuntimeException: Can't find resource 'solrconfig.xml' in classpath or '/some/path/test/conf/', cwd=/home/saudet/solrtest
我们可以通过为 Solr 安装东西来解决一些问题...