Skip to content
JackSparrow414
Go back

Using Solr (Part 1): Installation, Core Management, Queries, and Fixing CoreContainer Initialization Errors

Table of contents

Open Table of contents

Article body

Official tutorial

Before we begin: I highly recommend the three sections of Solr’s official introductory tutorial. They cover the essentials, from basic queries and phrase queries to importing data. The tutorial is very comprehensive, and the documentation itself calls it the best way to learn Solr. Many problems I encountered in practice were solved with its guidance.

  1. Installation

First approach: download the official package and extract it.

Second approach: install with Homebrew on macOS.

brew install solr

The default Homebrew installation directory is:

/usr/local/Cellar/solr/
  1. Startup

Enter the bin directory of the Solr installation:

cd /usr/local/Cellar/solr/8.5.0/bin/

Start Solr:

brew services start solr

Or:

./solr start -h localhost -p 8983

The message happy searching indicates successful startup. Visit localhost:8983; the page below confirms success.

Solr administration Dashboard after startup

  1. Create a Custom Core

The distribution provides examples that do not require you to create a core. In real applications, however, we need our own custom core.

Still in the Solr bin directory, create myCore. The -c argument specifies the core to create:

./solr create_core -c myCore

After successful creation, refresh the console to see the additional core.

Solr Core Admin showing the new core's directories and status

The default storage location is shown. The myCore directory contains conf, data, and core.properties. Two important files in conf are solrconfig.xml and managed-schema.

  1. Configure the corresponding entity fields in manager-scheme:
    <field name="name" type="string" indexed="true" stored="true"/>
    <field name="age" type="plong" indexed="true" stored="true"/>
  1. Alternatively, use console -> myCore -> scheme -> add Field.

  2. You can also add fields using curl.

Tip: for field definitions and types, see the field definition section.

  1. Insert Data into Solr

  2. In the console, choose Documents -> enter the data as JSON in the Documents box -> click submit.

  3. Insert two documents using curl. Be sure to add commit=true, or Solr will not commit them.

curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/customData/update?commit=true' --data-binary '[{"userId":"234232231","name":"dijaa","age":189},{"userId":"378777","name":"oop","age":78}]'

For more examples of inserting JSON through curl, see the Uploading Data documentation.

  1. Queries

  2. Click Query in the console to retrieve the data you just inserted.

Solr Query administration page retrieving inserted documents

  1. Use curl; customData here is another custom core.
curl "http://localhost:8983/solr/customData/select?q=*:*"

For more console and command-line queries, see the searching section.

Note: Solr has a default unique key, id, of type string. Its default value is a generated sequence, such as “783723823hhh-2323b23-9”. Real applications generally use distributed IDs, usually of type Long, and not every entity’s unique key is named id. Change uniqueKey in managed-schema and add a new field. However, Solr recommends storing unique keys as strings, so you can convert the application’s Long primary key to a string for storage.

  1. Delete Data

  2. Delete through the console.

Documents -> update -> choose XML -> run the following to delete all data:

<delete><query>*:*</query></delete>
<commit/>

To delete only specific data, put the query condition between the tags. For example, delete documents whose userId is 38237239392:

<delete><query>userId:38237239392</query></delete>
<commit/>
  1. Delete with curl. Again, include commit=true. In {“delete”:“the unique key in Solr”}, the value is the unique key. For more JSON operations, see updating, inserting, and deleting JSON with curl.
curl -X -POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/customData/update?commit=true' --data-binary '{"delete":"234232231"}'
  1. Delete a Core: the argument after -c is the core name.
solr delete -c myCore
  1. Stop and Restart Solr
solr stop
solr restart

Tip:

If you are unsure which Solr commands exist or how to use one, enter the bin directory of the Solr installation.

For command help, use (./solr command -help), where command is the specific command. Omit it to see all supported commands.

For example:
Terminal example calling the Solr API with curl

  1. Possible Problems

Error Processing the request.CoreContainer is either not initialize

During development, we usually create a core by copying and modifying configuration from an existing Solr core. Typically we change in manage-schema, in solrConfig.xml, and other settings. If restarting Solr after these changes produces this error, you may have forgotten another file: core.properties. By default, it includes:

name=your_core_name

If you copy existing files and forget to change this to your new core’s name, startup reports this error.

Next: Using Solr (Part 2): Importing MySQL Data, Full Updates, and Incremental Updates


Share this post:

Continue this series

Using Solr

  1. Using Solr (Part 1): Installation, Core Management, Queries, and Fixing CoreContainer Initialization ErrorsYou are here
  2. Using Solr (Part 2): Full and Incremental Imports from MySQL
  3. Using Solr (Part 3): SolrJ in an Application
  4. Using Solr (Part 4): Spring Data Solr in Real Applications, with Practical Code Examples
  5. Using Solr (Part 5): A Complete Command-Line Workflow and HTML Tag Filtering

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.