Installation

Download Apache Solr from this link.
Unzip the Solr release and change your working directory to the subdirectory where Solr was installed

ls solr*
# solr-5.3.1.zip
unzip -q solr-5.3.1.zip
cd solr-5.3.1/

To launch Solr, run: bin/solr start -e cloud -noprompt

/solr-5.3.1:$ bin/solr start -e cloud -noprompt

Welcome to the SolrCloud example!

Starting up 2 Solr nodes for your example SolrCloud cluster.
...

Started Solr server on port 8983 (pid=8404). Happy searching!
...

Started Solr server on port 7574 (pid=8549). Happy searching!
...

SolrCloud example running, please visit http://localhost:8983/solr

/solr-5.3.1:$ _

You can load the Solr Admin UI in your web browser: http://localhost:8983/solr/

Indexing Data

Solr server is up and running, but it doesn’t contain any data. The Solr install includes the bin/post* tool in order to facilitate getting various types of documents easily into Solr from the start. We’ll be using this tool for the indexing examples below.

bin/post -c gettingstarted docs/

Here’s what it’ll look like:

/solr-5.3.1:$ bin/post -c gettingstarted docs/
java -classpath /solr-5.3.1/dist/solr-core-5.3.1.jar -Dauto=yes -Dc=gettingstarted -Ddata=files -Drecursive=yes org.apache.solr.util.SimplePostTool docs/
SimplePostTool version 5.3.1
Posting files to [base] url http://localhost:8983/solr/gettingstarted/update...
Entering auto mode. File endings considered are xml,json,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
Entering recursive mode, max depth=999, delay=0s
Indexing directory docs (3 files, depth=0)
POSTing file index.html (text/html) to [base]/extract
POSTing file quickstart.html (text/html) to [base]/extract
POSTing file SYSTEM_REQUIREMENTS.html (text/html) to [base]/extract
Indexing directory docs/changes (1 files, depth=1)
POSTing file Changes.html (text/html) to [base]/extract
...
3248 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/gettingstarted/update...
Time spent: 0:00:41.660

The command-line breaks down as follows:

-c gettingstarted: name of the collection to index into
docs/: a relative path of the Solr install docs/ directory

Searching

Solr can be queried via REST clients, cURL, wget, Chrome POSTMAN, etc., as well as via the native clients available for many programming languages.

The Solr Admin UI includes a query builder interface – see the gettingstarted query tab at http://localhost:8983/solr/#/gettingstarted_shard1_replica1/query. If you click the Execute Query button without changing anything in the form, you’ll get 10 documents in JSON format (: in the q param matches all documents):

Search for a single term

To search for a term, give it as the q param value in the core-specific Solr Admin UI Query section, replace : with the term you want to find. To search for “foundation”:

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=foundation"

You’ll see:

/solr-5.3.1$ curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=foundation"
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"indent":"true",
"q":"foundation",
"wt":"json"}},
"response":{"numFound":2812,"start":0,"docs":[
{
"id":"0553293354",
"cat":["book"],
"name":"Foundation",
...

The response indicates that there are 2,812 hits (“numFound”:2812), of which the first 10 were returned, since by default start=0 and rows=10. You can specify these params to page through results, where start is the (zero-based) position of the first result to return, and rows is the page size.

q=foundation matches nearly all of the docs we’ve indexed, since most of the files under docs/ contain “The Apache Software Foundation”. To restrict search to a particular field, use the syntax “q=field:value”, e.g. to search for foundation only in the name field:

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=name:foundation"

The above request returns only one document (“numFound”:1) – from the response:

...
"response":{"numFound":1,"start":0,"docs":[
{
"id":"0553293354",
"cat":["book"],
"name":"Foundation",
...

Phrase search

To search for a multi-term phrase, enclose it in double quotes: q=”multiple terms here”. E.g. to search for “CAS latency” – note that the space between terms must be converted to “+” in a URL (the Admin UI will handle URL encoding for you automatically):

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=\"CAS+latency\""

You’ll get back:

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"indent":"true",
"q":"\"CAS latency\"",
"wt":"json"}},
"response":{"numFound":2,"start":0,"docs":[
{
"id":"VDBDB1A16",
"name":"A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM",
"manu":"A-DATA Technology Inc.",
"manu_id_s":"corsair",
"cat":["electronics", "memory"],
"features":["CAS latency 3,\t 2.7v"],
...

You can require that a term or phrase is present by prefixing it with a “+”; conversely, to disallow the presence of a term or phrase, prefix it with a “-“.

To find documents that contain both terms “one” and “three”, enter +one +three in the q param in the core-specific Admin UI Query tab. Because the “+” character has a reserved purpose in URLs (encoding the space character), you must URL encode it for curl as “%2B”:

curl http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=%2Bone+%2Bthree

Refrence

Solr Quick Start

Share:
Reading time: 4 min