Searching with Solr Tom Hill [email_address] eBig Java SIG, June 18th, 2008
Tonight's Talk Tonight's Talk should run about 1 1/2 hours About Solr  Background & overview Installing & Bringing Up Solr Rest Interface & Java Client Configuring Solr
Why Implement Search? Does your site need search? Do you need to implement it, or  is Google enough? Just text or Structured Data? Do you need to control ranking?
What is Solr? Web application for text search A wrapper around Apache Lucene  Lucene is a library (.jar file) Solr is a web app (.war file) Written at CNet, now at Apache
What is Lucene? Text search  library  in Java Fast, feature rich. Written by Doug Cutting Active Apache development community Versions also in C++, C#, Ruby, Python, Delphi, Lisp, etc...
Why Solr? Reliable Fast Supported Open Source Tunable Scoring
Solr Versions Current Version is 1.2 A year old 1.3 is coming "sometime" Large number of features in HEAD Use the latest from subversion for new projects
Alternatives to Solr Just Use Google Use Lucene Use Your Database Commercial Libraries Write your own
What Solr is Not A replacement for a relational database An embedded database* Fully cross platform :-( Replication depends on unix FS Admin scripts are bash(minor)
Solr Sites CNet (Reviews & Products) Internet Archive (Collections) Netflix (Movies) Zvents (Events) StripSearch.ws (Comics) And many more
Features Here's a quick look at some of the features of Solr, as implemented on Zvents.com
 
Faceted Navigation Groups the results by category Can do multiple facets at once  Returns matching counts
Additional Constraints
Synonyms, etc.
Solr Overview
Simple Webapp Web Servers[1..n] Database Master Database Slaves[0..n] Solr Master Solr Slaves[0..n]
Scaling Solr Master/Slave architecture Writes to master/reads to slaves Replication: Periodic transfers, not continuous Rsync
Updates Updates flush caches, bad for performance Master therefor much slower than slaves So send all queries to slaves Depends on your update rates
Solr's Data Model Solr maintains a collection of documents A document is a collection of fields & values A field can occur multiple times in a document Documents are immutable.  They can be deleted, and a new version added, however.
Querying Http request http://localhost:8080/comix/select/?q=java
Solr Query Syntax Lucene Query Syntax + a bit paris city:paris title:"The Right Way" AND text:go id:[* TO *]
Solr Query Syntax II -inStock:false te?t theat* te*t test~
Using Solr Getting data into Solr Getting data out of Solr
Getting Data Into Solr POST it. <add> <doc> <field name=&quot;employeeId&quot;>05991</field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </doc> [<doc> ... </doc>[<doc> ... </doc>]] </add>
Getting Data Into Solr POST it. <add> < doc > <field name=&quot;employeeId&quot;>05991</field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </ doc > [<doc> ... </doc>[<doc> ... </doc>]] </add>
Getting Data Into Solr POST it. <add> <doc> <field name=&quot; employeeId &quot;> 05991 </field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </doc> [<doc> ... </doc>[<doc> ... </doc>]] </add>
Committing Nothing shows up in the index until you commit You can just POST <commit/> to  http:// host : port /solr/update
Getting Data Out http://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str>  </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> </result> </response>
Getting Data Out http://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str> </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> </result> </response>
Getting Data Out http://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str> </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> ... </result> </response>
Getting Data Out http://localhost:8080/comix/select/?q=data&indent=on { &quot;responseHeader&quot;:{ &quot;status&quot;:0, &quot;QTime&quot;:1, &quot;params&quot;:{ &quot;wt&quot;:&quot;json&quot;, &quot;rows&quot;:[&quot;1&quot;,   &quot;1&quot;], &quot;start&quot;:&quot;0&quot;, &quot;indent&quot;:&quot;on&quot;, &quot;q&quot;:&quot;data&quot;, &quot;version&quot;:&quot;2.2&quot;}}, &quot;response&quot;:{&quot;numFound&quot;:2,&quot;start&quot;:0,&quot;docs&quot;:[ {   &quot;feature_id&quot;:&quot;3&quot;,   &quot;release_date&quot;:&quot;1992-05-07&quot;,   &quot;id&quot;:&quot;strip.3136&quot;,   &quot;timestamp&quot;:&quot;2008-02-28T10:06:01.682Z&quot;}] }} JSON format
Debug Query Option Add  &debugQuery=on  to request params Returns parsed form of query <str name=&quot;rawquerystring&quot;>c.i.a</str><str name=&quot;querystring&quot;>c.i.a</str><str name=&quot;parsedquery&quot;>PhraseQuery(text:&quot;c i a&quot;)</str><str name=&quot;parsedquery_toString&quot;>text:&quot;c i a&quot;</str>
Debug Query Option II Add  &debugQuery=on  to request params Returns scoring information <str name=&quot;id=strip.2781,internal_docid=29854&quot;> 2.6219895 = (MATCH) fieldWeight(text:calvin in 29854), product of: 1.0 = tf(termFreq(text:calvin)=1) 2.6219895 = idf(docFreq=6222) 1.0 = fieldNorm(field=text, doc=29854) </str> <str name=&quot;id=strip.4078,internal_docid=31151&quot;> 2.6219895 = (MATCH) fieldWeight(text:calvin in 31151), product of: 1.0 = tf(termFreq(text:calvin)=1) 2.6219895 = idf(docFreq=6222) 1.0 = fieldNorm(field=text, doc=31151) </str>
Deleting Data POST  <delete><id>35</id></delete> <delete><query>city:paris</query></delete>
Command Line Control curl  http://localhost:8983/solr/update  -H &quot;Content-type: text/xml&quot; --data-binary '<commit/>' <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><response><lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>20</int> </lst></response> </lst></response> </lst></response>
Solr in 3 minutes! Download Solr from Apache Untar &quot;ant example&quot; Start the example app Load data into Solr Query
Solr in Ten Minutes <Context docBase=&quot;/var/solr/apache-solr-1.2.0.war&quot; debug=&quot;0&quot; crossContext=&quot;true&quot; >  <Environment name=&quot;solr/home&quot; type=&quot;java.lang.String&quot; value=&quot;/var/solr&quot; override=&quot;true&quot; /></Context> Copy Solr's example/solr dir to /var/solr  Edit schema.xml and solrconfig.xml Load data into Solr In $CATALINA_HOME/conf/Catalina/localhost/foo.xml
Directory Layout ${solr.home}/conf schema.xml solrconfig.xml ${solr.home}/data ${solr.home}/logs ${solr.home}/bin
Java Solr Client Called SolrJ Not in Solr 1.2.  I grabbed from the HEAD from svn Works with Solr 1.2 Add/Delete/Query/Commit/Optimize
Adding Docs w/SolrJ Given Map<String, String> fields; CommonsHttpSolrServer  server  =  new  CommonsHttpSolrServer( url ); SolrInputDocument doc= new  SolrInputDocument(); for  (Map.Entry<String, String> e : fields.entrySet()){ doc.addField(e.getKey(), e.getValue()); } UpdateResponse res =  server .add( doc);
Deleting Docs w/SolrJ CommonsHttpSolrServer  server  =  new  CommonsHttpSolrServer( url ); UpdateResponse res; res = server .deleteById(&quot;100&quot;); res = server .deleteByQuery(&quot;city:paris&quot;);
Simple Query CommonsHttpSolrServer  server = new  CommonsHttpSolrServer( url ); SolrQuery query =  new  SolrQuery(); query.setQuery(&quot;dance&quot;); QueryResponse rsp =  server .query(query);
More Interesting Query CommonsHttpSolrServer  server  =  new  CommonsHttpSolrServer( url ); SolrQuery query =  new  SolrQuery(); query.setQuery(&quot;dance&quot;); query.setFacet( true ); query.addFacetField(&quot;city&quot;); query.setFacetMinCount(1); query.addSortField( &quot;price&quot;, SolrQuery.ORDER.asc ); QueryResponse rsp =  server .query(query);
Query Responses QueryResponse qr =  server .query(query); SolrDocumentList docs = qr.getResults(); List<FacetField> lf = qr.getFacetFields(); for  (FacetField ff: lf) { String fieldName = ff.getName(); List<FacetField.Count> lc = ff.getValues(); for  (FacetField.Count c: lc) { String countName = c.getName(); long count = c.getCount();   } }
Other Commands Commit server.commit() Optimize server.optimize() Not too complicated!
Request Handlers Request handler define how the query is processed. Two main types StandardRequestHandler DisMaxRequestHandler You can implement your own Changing in Solr 1.3
&quot;Standard&quot; Request Handler Accepts Solr Query Syntax I tend to use it for my queries, not user queries.
DisMaxRequestHandler Recommended for user queries Allows simple users keywords to be applied to multiple fields, with weighting. Boost Functions Boost Queries
Boost Functions Allow you to influence scoring at run time Computationally Expensive! Really useful for tuning scoring linear(x,2,4) returns 2*x+4 x is a field
The Solr Schema schema.xml Defines types used in this webapp Defines the fields and their types Defines &quot;copyFields&quot; READ THE EXAMPLE SCHEMA.XML
Types Types define processing for a field How the words are split (Whitespace? Punctuation? CIA != C.I.A.) Stemming Case Folding, etc Predefined date, int, float, etc c
Analysis: Index and Query Time Types have two modes Index Time Query Time
Simple Text Field <fieldType name=&quot;text&quot; class=&quot;solr.TextField&quot;  positionIncrementGap= &quot;100&quot;>  <analyzer type=&quot;index&quot;>  <tokenizer class=&quot;solr.WhitespaceTokenizerFactory&quot;/>  <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer><analyzer type=&quot;query&quot;><tokenizer class=&quot;solr.WhitespaceTokenizerFactory&quot;/> <filter class=&quot;solr.SynonymFilterFactory&quot; synonyms=&quot;synonyms.txt&quot;  ignoreCase=&quot;true&quot; expand=&quot;true&quot;/> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType>
Analysis & Facets Make sure to use an untokenized field for faceting. &quot;San Jose&quot; != &quot;San&quot; &quot;Jose&quot;
Fields Elements of a document Both predefined & dynamic Fields may occur multiple times Maybe indexed and/or stored
Example Fields <field name=&quot;id&quot; type=&quot;string&quot; indexed=&quot;true&quot; stored=&quot;true&quot; required=&quot;true&quot; /><field name=&quot;name&quot; type=&quot;text&quot; indexed=&quot;true&quot; stored=&quot;true&quot;/><field name=&quot;alphaNameSort&quot; type=&quot;alphaOnlySort&quot; indexed=&quot;true&quot; stored=&quot;false&quot;/>
Copy Fields Two main uses To analyze a field in two different ways To concatenate fields
The Solr Config File solrconfig.xml Defines request handlers, defaults, caches,  Read the example solrconfig.xml
Configuring DisMax Parameter defaults set in solrconfig.xml Can be overridden in each request Except for params labeled invariant
DisMax Config Example <requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; >  <lst name=&quot;defaults&quot;>  <str name=&quot;echoParams&quot;>explicit</str>  <float name=&quot;tie&quot;>0.01</float>  <str name=&quot;qf&quot;>  text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4  </str>  <str name=&quot;pf&quot;>  text^0.2 features^1.1 name^1.5 manu^1.4 manu_exact^1.9  </str> ... </requestHandler>
DisMax Config Example <requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; >  ... <str name=&quot;bf&quot;>  ord(poplarity)^0.5 recip(rord(price),1,1000,1000)^0.3  </str>  <str name=&quot;fl&quot;>  id,name,price,score  </str>...  </requestHandler>
DisMax Config Example <requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; >  ... <str name=&quot;mm&quot;>  2&lt;-1 5&lt;-2 6&lt;90%  </str>  <int name=&quot;ps&quot;>100</int>  <str name=&quot;q.alt&quot;>*:*</str>  </lst>  </requestHandler>
Wrap Up
Resources Solr  http://lucene.apache.org/solr/ wiki, mailing list, jira (bugs/features) Lucene  http://lucene.apache.org /
Lucene In Action
Building Search Applications with Lucene, lingpipe and Gate Manu Konchady Manu Konchady Manu Konchady
Other Presentations Yonik Seely's Solr & Lucene http://people.apache.org/~yonik/presentations/ Slideshare.net Search for solr, or search for lucene
Thanks! Thanks for coming. Feel free to email me if you have questions about Solr Tom Hill [email_address]
Extra Slides Things I didn't have time for in the presentation. Some of them unfinished.
Search Engines are not the Same as Users Search engines have different usage patterns than users
Response Writers http://localhost:8983/solr/select/?q=text_t%3Atiger&version=2.2&start=0&rows=10&indent=on& wt=ruby http://localhost:8983/solr/select/?q=text_t%3Atiger&version=2.2&start=0&rows=10&indent=on& wt=xml
Explain Just why did the documents come up in that order?
Data Matters Gigo The better the data is, the better the search will be.
Watch Your Caches Just like any other app, check your statistics What's the hit rate for your caches?
Setting Up Replication Run rsyncd on the master Run snapshot on the master at intervals Run snappuller on the slaves at (different) intervals. Scripts don't print errors! Check the logs Use bash -xv
Autowarming Runs after an update to the index Updates flush caches Runs some queries to populate caches again Can be a problem, with frequent updates Don't autowarm master, if updating lots
Tour Of Solr's Web UI
Programming Collective Intelligence A Really Fun Book
Geographic Searching Local Lucene & Local Solr http://locallucene.wiki.sourceforge.net There's also geolucene, but it's not being actively developed, as far as I can tell. http://www.gossamer-threads.com/lists/l ucene/java-dev/53378
http://localhost:8983/solr/admin/stats.jsp#update Are there commits pending?
http://localhost:8983/comix/admin/analysis.jsp?name=text&val=wi-fi Analysis Explanation

An Introduction to Solr

  • 1.
    Searching with SolrTom Hill [email_address] eBig Java SIG, June 18th, 2008
  • 2.
    Tonight's Talk Tonight'sTalk should run about 1 1/2 hours About Solr Background & overview Installing & Bringing Up Solr Rest Interface & Java Client Configuring Solr
  • 3.
    Why Implement Search?Does your site need search? Do you need to implement it, or is Google enough? Just text or Structured Data? Do you need to control ranking?
  • 4.
    What is Solr?Web application for text search A wrapper around Apache Lucene Lucene is a library (.jar file) Solr is a web app (.war file) Written at CNet, now at Apache
  • 5.
    What is Lucene?Text search library in Java Fast, feature rich. Written by Doug Cutting Active Apache development community Versions also in C++, C#, Ruby, Python, Delphi, Lisp, etc...
  • 6.
    Why Solr? ReliableFast Supported Open Source Tunable Scoring
  • 7.
    Solr Versions CurrentVersion is 1.2 A year old 1.3 is coming &quot;sometime&quot; Large number of features in HEAD Use the latest from subversion for new projects
  • 8.
    Alternatives to SolrJust Use Google Use Lucene Use Your Database Commercial Libraries Write your own
  • 9.
    What Solr isNot A replacement for a relational database An embedded database* Fully cross platform :-( Replication depends on unix FS Admin scripts are bash(minor)
  • 10.
    Solr Sites CNet(Reviews & Products) Internet Archive (Collections) Netflix (Movies) Zvents (Events) StripSearch.ws (Comics) And many more
  • 11.
    Features Here's aquick look at some of the features of Solr, as implemented on Zvents.com
  • 12.
  • 13.
    Faceted Navigation Groupsthe results by category Can do multiple facets at once Returns matching counts
  • 14.
  • 15.
  • 16.
  • 17.
    Simple Webapp WebServers[1..n] Database Master Database Slaves[0..n] Solr Master Solr Slaves[0..n]
  • 18.
    Scaling Solr Master/Slavearchitecture Writes to master/reads to slaves Replication: Periodic transfers, not continuous Rsync
  • 19.
    Updates Updates flushcaches, bad for performance Master therefor much slower than slaves So send all queries to slaves Depends on your update rates
  • 20.
    Solr's Data ModelSolr maintains a collection of documents A document is a collection of fields & values A field can occur multiple times in a document Documents are immutable. They can be deleted, and a new version added, however.
  • 21.
    Querying Http requesthttp://localhost:8080/comix/select/?q=java
  • 22.
    Solr Query SyntaxLucene Query Syntax + a bit paris city:paris title:&quot;The Right Way&quot; AND text:go id:[* TO *]
  • 23.
    Solr Query SyntaxII -inStock:false te?t theat* te*t test~
  • 24.
    Using Solr Gettingdata into Solr Getting data out of Solr
  • 25.
    Getting Data IntoSolr POST it. <add> <doc> <field name=&quot;employeeId&quot;>05991</field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </doc> [<doc> ... </doc>[<doc> ... </doc>]] </add>
  • 26.
    Getting Data IntoSolr POST it. <add> < doc > <field name=&quot;employeeId&quot;>05991</field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </ doc > [<doc> ... </doc>[<doc> ... </doc>]] </add>
  • 27.
    Getting Data IntoSolr POST it. <add> <doc> <field name=&quot; employeeId &quot;> 05991 </field> <field name=&quot;office&quot;>Bridgewater</field> <field name=&quot;skills&quot;>Perl</field> <field name=&quot;skills&quot;>Java</field> </doc> [<doc> ... </doc>[<doc> ... </doc>]] </add>
  • 28.
    Committing Nothing showsup in the index until you commit You can just POST <commit/> to http:// host : port /solr/update
  • 29.
    Getting Data Outhttp://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str> </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> </result> </response>
  • 30.
    Getting Data Outhttp://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str> </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> </result> </response>
  • 31.
    Getting Data Outhttp://localhost:8080/comix/select/?q=data&indent=on <response> <lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>0</int> <lst name=&quot;params&quot;> <str name=&quot;indent&quot;>on</str> <str name=&quot;q&quot;>data</str> </lst> </lst> <result name=&quot;response&quot; numFound=&quot;2&quot; start=&quot;0&quot;> <doc> <str name=&quot;id&quot;>strip.3136</str> <str name=&quot;release_date&quot;>1992-05-07</str> <date name=&quot;timestamp&quot;>2008-02-28T10:06:01.682Z</date> <str name=&quot;type&quot;>strip</str> </doc> ... </result> </response>
  • 32.
    Getting Data Outhttp://localhost:8080/comix/select/?q=data&indent=on { &quot;responseHeader&quot;:{ &quot;status&quot;:0, &quot;QTime&quot;:1, &quot;params&quot;:{ &quot;wt&quot;:&quot;json&quot;, &quot;rows&quot;:[&quot;1&quot;, &quot;1&quot;], &quot;start&quot;:&quot;0&quot;, &quot;indent&quot;:&quot;on&quot;, &quot;q&quot;:&quot;data&quot;, &quot;version&quot;:&quot;2.2&quot;}}, &quot;response&quot;:{&quot;numFound&quot;:2,&quot;start&quot;:0,&quot;docs&quot;:[ { &quot;feature_id&quot;:&quot;3&quot;, &quot;release_date&quot;:&quot;1992-05-07&quot;, &quot;id&quot;:&quot;strip.3136&quot;, &quot;timestamp&quot;:&quot;2008-02-28T10:06:01.682Z&quot;}] }} JSON format
  • 33.
    Debug Query OptionAdd &debugQuery=on to request params Returns parsed form of query <str name=&quot;rawquerystring&quot;>c.i.a</str><str name=&quot;querystring&quot;>c.i.a</str><str name=&quot;parsedquery&quot;>PhraseQuery(text:&quot;c i a&quot;)</str><str name=&quot;parsedquery_toString&quot;>text:&quot;c i a&quot;</str>
  • 34.
    Debug Query OptionII Add &debugQuery=on to request params Returns scoring information <str name=&quot;id=strip.2781,internal_docid=29854&quot;> 2.6219895 = (MATCH) fieldWeight(text:calvin in 29854), product of: 1.0 = tf(termFreq(text:calvin)=1) 2.6219895 = idf(docFreq=6222) 1.0 = fieldNorm(field=text, doc=29854) </str> <str name=&quot;id=strip.4078,internal_docid=31151&quot;> 2.6219895 = (MATCH) fieldWeight(text:calvin in 31151), product of: 1.0 = tf(termFreq(text:calvin)=1) 2.6219895 = idf(docFreq=6222) 1.0 = fieldNorm(field=text, doc=31151) </str>
  • 35.
    Deleting Data POST <delete><id>35</id></delete> <delete><query>city:paris</query></delete>
  • 36.
    Command Line Controlcurl http://localhost:8983/solr/update -H &quot;Content-type: text/xml&quot; --data-binary '<commit/>' <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><response><lst name=&quot;responseHeader&quot;> <int name=&quot;status&quot;>0</int> <int name=&quot;QTime&quot;>20</int> </lst></response> </lst></response> </lst></response>
  • 37.
    Solr in 3minutes! Download Solr from Apache Untar &quot;ant example&quot; Start the example app Load data into Solr Query
  • 38.
    Solr in TenMinutes <Context docBase=&quot;/var/solr/apache-solr-1.2.0.war&quot; debug=&quot;0&quot; crossContext=&quot;true&quot; > <Environment name=&quot;solr/home&quot; type=&quot;java.lang.String&quot; value=&quot;/var/solr&quot; override=&quot;true&quot; /></Context> Copy Solr's example/solr dir to /var/solr Edit schema.xml and solrconfig.xml Load data into Solr In $CATALINA_HOME/conf/Catalina/localhost/foo.xml
  • 39.
    Directory Layout ${solr.home}/confschema.xml solrconfig.xml ${solr.home}/data ${solr.home}/logs ${solr.home}/bin
  • 40.
    Java Solr ClientCalled SolrJ Not in Solr 1.2. I grabbed from the HEAD from svn Works with Solr 1.2 Add/Delete/Query/Commit/Optimize
  • 41.
    Adding Docs w/SolrJGiven Map<String, String> fields; CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); SolrInputDocument doc= new SolrInputDocument(); for (Map.Entry<String, String> e : fields.entrySet()){ doc.addField(e.getKey(), e.getValue()); } UpdateResponse res = server .add( doc);
  • 42.
    Deleting Docs w/SolrJCommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); UpdateResponse res; res = server .deleteById(&quot;100&quot;); res = server .deleteByQuery(&quot;city:paris&quot;);
  • 43.
    Simple Query CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); SolrQuery query = new SolrQuery(); query.setQuery(&quot;dance&quot;); QueryResponse rsp = server .query(query);
  • 44.
    More Interesting QueryCommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); SolrQuery query = new SolrQuery(); query.setQuery(&quot;dance&quot;); query.setFacet( true ); query.addFacetField(&quot;city&quot;); query.setFacetMinCount(1); query.addSortField( &quot;price&quot;, SolrQuery.ORDER.asc ); QueryResponse rsp = server .query(query);
  • 45.
    Query Responses QueryResponseqr = server .query(query); SolrDocumentList docs = qr.getResults(); List<FacetField> lf = qr.getFacetFields(); for (FacetField ff: lf) { String fieldName = ff.getName(); List<FacetField.Count> lc = ff.getValues(); for (FacetField.Count c: lc) { String countName = c.getName(); long count = c.getCount(); } }
  • 46.
    Other Commands Commitserver.commit() Optimize server.optimize() Not too complicated!
  • 47.
    Request Handlers Requesthandler define how the query is processed. Two main types StandardRequestHandler DisMaxRequestHandler You can implement your own Changing in Solr 1.3
  • 48.
    &quot;Standard&quot; Request HandlerAccepts Solr Query Syntax I tend to use it for my queries, not user queries.
  • 49.
    DisMaxRequestHandler Recommended foruser queries Allows simple users keywords to be applied to multiple fields, with weighting. Boost Functions Boost Queries
  • 50.
    Boost Functions Allowyou to influence scoring at run time Computationally Expensive! Really useful for tuning scoring linear(x,2,4) returns 2*x+4 x is a field
  • 51.
    The Solr Schemaschema.xml Defines types used in this webapp Defines the fields and their types Defines &quot;copyFields&quot; READ THE EXAMPLE SCHEMA.XML
  • 52.
    Types Types defineprocessing for a field How the words are split (Whitespace? Punctuation? CIA != C.I.A.) Stemming Case Folding, etc Predefined date, int, float, etc c
  • 53.
    Analysis: Index andQuery Time Types have two modes Index Time Query Time
  • 54.
    Simple Text Field<fieldType name=&quot;text&quot; class=&quot;solr.TextField&quot; positionIncrementGap= &quot;100&quot;> <analyzer type=&quot;index&quot;> <tokenizer class=&quot;solr.WhitespaceTokenizerFactory&quot;/> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer><analyzer type=&quot;query&quot;><tokenizer class=&quot;solr.WhitespaceTokenizerFactory&quot;/> <filter class=&quot;solr.SynonymFilterFactory&quot; synonyms=&quot;synonyms.txt&quot; ignoreCase=&quot;true&quot; expand=&quot;true&quot;/> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType> <filter class=&quot;solr.StopFilterFactory&quot; ignoreCase=&quot;true&quot; words=&quot;stopwords.txt&quot;/></analyzer></fieldType>
  • 55.
    Analysis & FacetsMake sure to use an untokenized field for faceting. &quot;San Jose&quot; != &quot;San&quot; &quot;Jose&quot;
  • 56.
    Fields Elements ofa document Both predefined & dynamic Fields may occur multiple times Maybe indexed and/or stored
  • 57.
    Example Fields <fieldname=&quot;id&quot; type=&quot;string&quot; indexed=&quot;true&quot; stored=&quot;true&quot; required=&quot;true&quot; /><field name=&quot;name&quot; type=&quot;text&quot; indexed=&quot;true&quot; stored=&quot;true&quot;/><field name=&quot;alphaNameSort&quot; type=&quot;alphaOnlySort&quot; indexed=&quot;true&quot; stored=&quot;false&quot;/>
  • 58.
    Copy Fields Twomain uses To analyze a field in two different ways To concatenate fields
  • 59.
    The Solr ConfigFile solrconfig.xml Defines request handlers, defaults, caches, Read the example solrconfig.xml
  • 60.
    Configuring DisMax Parameterdefaults set in solrconfig.xml Can be overridden in each request Except for params labeled invariant
  • 61.
    DisMax Config Example<requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; > <lst name=&quot;defaults&quot;> <str name=&quot;echoParams&quot;>explicit</str> <float name=&quot;tie&quot;>0.01</float> <str name=&quot;qf&quot;> text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4 </str> <str name=&quot;pf&quot;> text^0.2 features^1.1 name^1.5 manu^1.4 manu_exact^1.9 </str> ... </requestHandler>
  • 62.
    DisMax Config Example<requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; > ... <str name=&quot;bf&quot;> ord(poplarity)^0.5 recip(rord(price),1,1000,1000)^0.3 </str> <str name=&quot;fl&quot;> id,name,price,score </str>... </requestHandler>
  • 63.
    DisMax Config Example<requestHandler name=&quot;dismax&quot; class=&quot;solr.DisMaxRequestHandler&quot; > ... <str name=&quot;mm&quot;> 2&lt;-1 5&lt;-2 6&lt;90% </str> <int name=&quot;ps&quot;>100</int> <str name=&quot;q.alt&quot;>*:*</str> </lst> </requestHandler>
  • 64.
  • 65.
    Resources Solr http://lucene.apache.org/solr/ wiki, mailing list, jira (bugs/features) Lucene http://lucene.apache.org /
  • 66.
  • 67.
    Building Search Applicationswith Lucene, lingpipe and Gate Manu Konchady Manu Konchady Manu Konchady
  • 68.
    Other Presentations YonikSeely's Solr & Lucene http://people.apache.org/~yonik/presentations/ Slideshare.net Search for solr, or search for lucene
  • 69.
    Thanks! Thanks forcoming. Feel free to email me if you have questions about Solr Tom Hill [email_address]
  • 70.
    Extra Slides ThingsI didn't have time for in the presentation. Some of them unfinished.
  • 71.
    Search Engines arenot the Same as Users Search engines have different usage patterns than users
  • 72.
    Response Writers http://localhost:8983/solr/select/?q=text_t%3Atiger&version=2.2&start=0&rows=10&indent=on&wt=ruby http://localhost:8983/solr/select/?q=text_t%3Atiger&version=2.2&start=0&rows=10&indent=on& wt=xml
  • 73.
    Explain Just whydid the documents come up in that order?
  • 74.
    Data Matters GigoThe better the data is, the better the search will be.
  • 75.
    Watch Your CachesJust like any other app, check your statistics What's the hit rate for your caches?
  • 76.
    Setting Up ReplicationRun rsyncd on the master Run snapshot on the master at intervals Run snappuller on the slaves at (different) intervals. Scripts don't print errors! Check the logs Use bash -xv
  • 77.
    Autowarming Runs afteran update to the index Updates flush caches Runs some queries to populate caches again Can be a problem, with frequent updates Don't autowarm master, if updating lots
  • 78.
  • 79.
  • 80.
    Geographic Searching LocalLucene & Local Solr http://locallucene.wiki.sourceforge.net There's also geolucene, but it's not being actively developed, as far as I can tell. http://www.gossamer-threads.com/lists/l ucene/java-dev/53378
  • 81.
  • 82.