Solr Integration with Grails using SolrJ API
Posted By : Varun Sharma | 27-Sep-2012
Solr - Apache Solr is a fast open-source Java search server. Solr enables you to easily create search engines which searches websites, databases and files.
Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index.
Configuring Solr - The 2 most important files in the Solr configuration are:
1. schema.xml
schema.xml
schema.xml is usually the first file you configure when setting up a new Solr installation. The schema declares:
1. what kinds of fields there are
2. which field should be used as the unique/primary key
3. which fields are required
4. how to index and search each field
For e.g. If you create facet field name “product” you configure facet in schema.xml like as
Field-
Valid attributes for fields:
1. name: mandatory - the name for the field
2. type: mandatory - the name of a previously defined type from the
3. indexed: true if this field should be indexed (searchable or sortable)
4. stored: true if this field should be retrievable
5. multiValued: true if this field may contain multiple values per document
6. omitNorms: (expert) set to true to omit the norms associated with this field (this disables length normalization and index-time boosting for the field, and saves some memory). Only full-text fields or fields that need an index-time boost need norms.
7. termVectors: [false] set to true to store the term vector for a given field. When using more like this, fields used for similarity should be stored for best performance.
8. termPositions: Store position information with the term vector. This will increase storage costs.
9. termOffsets: Store offset information with the term vector. This will increase storage costs.
10. default: a value that should be used if no value is specified when adding a document.
11. copyField- copyField commands copy one field to another at the time a document is added to the index. It's used either to index the same field differently, or to add multiple fields to the same field for easier/faster searching.
solrconfig.xml
solrconfig.xml is usually the second file you configure when setting up a new Solr installation, after schema.xml. The more commonly-used elements in solrconfig.xml are:
1. data directory location
2. cache parameters
3. request handlers
4. search components
SolrJ API-
1. CommonsHttpSolrServer – Return a solr server
CommonsHttpSolrServer server = new CommonsHttpSolrServer
("http://localhost:8983/solr");
CommonsHttpSolrServer allows setting connection properties.
String url = "http://localhost:8983/solr" CommonsHttpSolrServer server = new CommonsHttpSolrServer(url) server.setSoTimeout(1000); // socket read timeout server.setConnectionTimeout(100); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); server.setFollowRedirects(false); // defaults to false server.setAllowCompression(true); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. server.setParser(new XMLResponseParser()); // binary parser is used by default
2. SolrInputDocument – Adding Data to Solr (Add documents)
SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField( "id", "id1"); doc1.addField( "name", "doc1"); doc1.addField( "price", “10”); server.add(doc1); server.commit() //Performs an explicit commit, causing pending documents to be committed for indexing
3. SolrQuery - Solr Search
String url = "http://localhost:8983/solr"
CommonsHttpSolrServer server = new CommonsHttpSolrServer(url)
def solrQuery = new SolrQuery( params.searchTerms )
solrQuery.setRows(10)
solrQuery.setStart(params.start as int)
solrQuery.setFacet(true)
solrQuery.setFilterQueries('/' +filterQuery+ '/')
solrQuery.setSortField("order_group",SolrQuery.ORDER.asc)
server.query( solrQuery, SolrRequest.METHOD.POST).getResults()
4. Delete everything from Solr Document
server.deleteByQuery (“*:*”)
>Hope it helps !
Varun Sharma [email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Sharma
Varun is an experienced Groovy and Grails developer and has worked extensively on designing and developing applications with FaceBook , Linkedin and Twitter integrations using Grails technologies. Varun loves painting and photography.