Last active
April 20, 2016 07:25
-
-
Save delip/6214406 to your computer and use it in GitHub Desktop.
Solr 4.4.0 EmbeddedSolrServer example: Indexing and Querying
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); | |
} | |
} | |
} |
Can you share this example as a full project rather than just this gist please? There aren't many examples on the internet, and the wiki only goes so far. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please can u details the jar that u are using,and the configuration that you did in solr xml files ,thanks (PS: i'm using SOLR-4.9.0)