-
-
Save lianyi/56116e527ba1ca86ac88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.io.filefilter.TrueFileFilter; | |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; | |
import org.apache.solr.client.solrj.response.QueryResponse; | |
import org.apache.solr.common.SolrDocument; | |
import org.apache.solr.common.SolrInputDocument; | |
import org.apache.solr.common.params.CommonParams; | |
import org.apache.solr.common.params.ModifiableSolrParams; | |
import org.apache.solr.common.params.SolrParams; | |
import org.apache.solr.core.CoreContainer; | |
import org.apache.solr.servlet.SolrRequestParsers; | |
import java.io.File; | |
import java.util.Collection; | |
public class EmbeddedSolrExample { | |
public static void main(String [] args) throws Exception { | |
String solrDir = "sandbox/solr"; | |
CoreContainer container = new CoreContainer(solrDir); | |
container.load(); | |
EmbeddedSolrServer server = new EmbeddedSolrServer(container, "shakespeare"); | |
Collection<File> files = FileUtils.listFiles(new File("sandbox/sonnets"), | |
TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); | |
for (File file : files) { | |
String name = file.getName(); | |
String content = FileUtils.readFileToString(file); | |
SolrInputDocument document = new SolrInputDocument(); | |
document.addField("name", name); | |
document.addField("text", content); | |
document.addField("id", name.hashCode()); | |
server.add(document); | |
} | |
server.commit(); | |
Thread.sleep(5000); | |
container.shutdown(); | |
server.shutdown(); | |
container = new CoreContainer(solrDir); | |
container.load(); | |
server = new EmbeddedSolrServer(container, "shakespeare"); | |
ModifiableSolrParams solrParams = new ModifiableSolrParams(); | |
solrParams.add(CommonParams.Q, "*:*"); | |
QueryResponse queryResponse = server.query(solrParams); | |
for (SolrDocument document : queryResponse.getResults()) { | |
System.out.println(document); | |
} | |
server.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment