Reputation: 719
I have been trying to run and try Apache solr (connecting and running Java queries) in Windows for two days, and it took me about 15 different stackoverflow searches and 5 long tutorials to overcome all the errors and questions that I was facing. This question is intended as a self-Q&A for those experiencing the same problems. I'm using: Windows 7, Apache solr 5.5.0, Maven and Eclipse. Note: I know I'm not covering absolutely everything, but I hope I can help people overcome some of their problems!
Questions that will be (hopefully) covered:
Upvotes: 1
Views: 2254
Reputation: 719
Where do I download solr? (SO independent)
You can get solr 5.5.0 from here.
How do I start solr (from the windows console)?
$ solr-5.5.0/bin/solr.cmd start
How do I create a collection (from the windows console)?
$ solr-5.5.0/bin/solr create -c gettingstarted
where "gettingstarted" is the name of your collection.
How do I create a connection from Java? (SO independent)
This part is a bit tricky, since it has changed over different solr releases. Right now, you can do it like this:
String urlString = "http://localhost:8983/solr/gettingstarted";
SolrClient solr = new HttpSolrClient(urlString);
Notice that the url doesn't contain "#/~cores". It will give you connection errors if you copy the full url like that.
How do I add a document to my collection? (SO independent)
// Adds a document containing two fields: id and first_name, with
// values "123" and "randombee" respectively
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123");
document.addField("first_name", "randombee");
solr.add(document);
solr.commit(); // you MUST do this to commit the changes to your collection
How do I delete a document from my collection? (SO independent)
There are two different ways you can do this: deleting a document by id (if you already know that field) or deleting a document by query.
// Deletes document with id="mymockid"
solr.deleteById("mymockid");
solr.commit();
// Deletes all documents with first_name:randombee
solr.deleteByQuery("first_name:randombee");
solr.commit();
How do I query my documents? (SO independent)
Two examples on how to do it. If you want to query all the documents in the collection, you can use *:* in your query to achieve it:
// Queries all documents (*:*) and shows only the id
SolrQuery query = new SolrQuery();
query.setQuery("*:*"); // actual query
QueryResponse response = null;
try {
response = solr.query(query);
} catch (SolrServerException e) {/* */ }
SolrDocumentList list = response.getResults();
System.out.println(list.toString()); // print the results of the query
Query for specific data:
// Queries documents with first_name=randombee or id=SP2514N
// and shows fields id and name
query = new SolrQuery();
query.setQuery("first_name:randombee OR id:SP2514N");
query.setFields("id", "name"); // set fields you want to show
response = null;
try {
response = solr.query(query);
} catch (SolrServerException e) {/* */ }
list = response.getResults();
System.out.println(list.toString());
which gives me this result:
{numFound=1,start=0,docs=[SolrDocument{id=123, first_name=randombee}]}
Can I access my collection from the browser?
Assuming solr is running in localhost and port 8983, accessing http://localhost:8983/solr/mycollection/browse will show you all the documents in the collection "mycollection". Note that the path is not http://localhost:8983/solr/#/~cores/gettingstarted, even if that is the path showing when you access all the cores. You have to remove the #/~cores part and add the /browse to see your collection.
Upvotes: 8