Apache Solr - Running Search Query

By now, we have indexed sample data of books.csv that comes with Solr. Lets see how to run Search Query in books data.

Running Queries on Solr
Open Solr Admin UI in a browser using http://localhost:8983 and choose the core1 under "Core Selector" dropdown list on left nav bar. You can see the number of total documents index by that core in right section of the page, as below:



To run search query on core1, choose the query link at left menu, you would a query builder UI, as below.

Figure 2: Setting Search Query


Admin UI, see Figure 2, provides user friendly way to build search query. Solr Admin passes all specified parameters to Solr API and shows the results return by the API on right section of the page. Solr Admin Query component is good tool to test search queries before we integrate them in our applications. If you leave all options to their default values and click "Execute Query" button, Solr would return the below JSON. You see, its same data that our books.csv files contain.

{
  "responseHeader":{
    "status":0,
    "QTime":1,
    "params":{
      "q":"*:*",
      "indent":"on",
      "wt":"json"}},
  "response":{"numFound":10,"start":0,"docs":[
      {
        "id":"0553573403",
        "cat":"book",
        "name":"A Game of Thrones",
        "price":7.99,
        "inStock":true,
        "author":"George R.R. Martin",
        "series_t":["A Song of Ice and Fire"],
        "sequence_i":1,
        "genre_s":"fantasy",
        "_version_":1562647546884521984},
      {
        "id":"0553579908",
        "cat":"book",
        "name":"A Clash of Kings",
        "price":7.99,
        "inStock":true,
        "author":"George R.R. Martin",
        "series_t":["A Song of Ice and Fire"],
        "sequence_i":2,
        "genre_s":"fantasy",
        "_version_":1562647546950582272},
      {
        "id":"055357342X",
        "cat":"book",
        "name":"A Storm of Swords",
        "price":7.99,
        "inStock":true,
        "author":"George R.R. Martin",
        "series_t":["A Song of Ice and Fire"],
        "sequence_i":3,
        "genre_s":"fantasy",
        "_version_":1562647546950582273},
      {
        "id":"0553293354",
        "cat":"book",
        "name":"Foundation",
        "price":7.99,
        "inStock":true,
        "author":"Isaac Asimov",
        "series_t":["Foundation Novels"],
        "sequence_i":1,
        "genre_s":"scifi",
        "_version_":1562647546950582274},
      {
        "id":"0812521390",
        "cat":"book",
        "name":"The Black Company",
        "price":6.99,
        "inStock":false,
        "author":"Glen Cook",
        "series_t":["The Chronicles of The Black Company"],
        "sequence_i":1,
        "genre_s":"fantasy",
        "_version_":1562647546950582275},
      {
        "id":"0812550706",
        "cat":"book",
        "name":"Ender's Game",
        "price":6.99,
        "inStock":true,
        "author":"Orson Scott Card",
        "series_t":["Ender"],
        "sequence_i":1,
        "genre_s":"scifi",
        "_version_":1562647546950582276},
      {
        "id":"0441385532",
        "cat":"book",
        "name":"Jhereg",
        "price":7.95,
        "inStock":false,
        "author":"Steven Brust",
        "series_t":["Vlad Taltos"],
        "sequence_i":1,
        "genre_s":"fantasy",
        "_version_":1562647546950582277},
      {
        "id":"0380014300",
        "cat":"book",
        "name":"Nine Princes In Amber",
        "price":6.99,
        "inStock":true,
        "author":"Roger Zelazny",
        "series_t":["the Chronicles of Amber"],
        "sequence_i":1,
        "genre_s":"fantasy",
        "_version_":1562647546950582278},
      {
        "id":"0805080481",
        "cat":"book",
        "name":"The Book of Three",
        "price":5.99,
        "inStock":true,
        "author":"Lloyd Alexander",
        "series_t":["The Chronicles of Prydain"],
        "sequence_i":1,
        "genre_s":"fantasy",
        "_version_":1562647546950582279},
      {
        "id":"080508049X",
        "cat":"book",
        "name":"The Black Cauldron",
        "price":5.99,
        "inStock":true,
        "author":"Lloyd Alexander",
        "series_t":["The Chronicles of Prydain"],
        "sequence_i":2,
        "genre_s":"fantasy",
        "_version_":1562647546950582280}]
  }}

On top of above results page, Admin UI also display Solr API URL with parameters that was passed to server. Learning how different parameters are passed to Solr instance is first step to understanding how we can integrate queries into our applications.

On Figure 2, after the Request Handler i.e. /select, there are common options to use for drafting the search query. These options include: q, fq, sort, start-rows, fl, df. Here is what each search parameter represent:

q: Query
f1: Filter Query
sort: Sort field or function with ascending or descending order
start, rows: How many documens to skip i.e. offset, how many next documents to fetch
fl: Fields List that Solr should return, by default, it would returl all fields
df: Default Search Field

Lets look at each field with example one by one: (We would see the other checkboxes and fields of Figure 2 in some other post.)

Query: q
q parameter is used to define condition that must be fulfilled to include a document into query result. For example if we enter price:5.99  in q field. It would return books where price field is equal to 5.99. In same way, if we enter name:"A Clash of Kings", the books with name "A Clash of Kings" would be return only. See result below, with the query specified.


Filter Query Parameter or fl


Continue.....

Comments