Xplod
Xplod

Reputation: 43

How to get search results from SOLR using Jquery

I am having data in SOLR as below:

{
  "responseHeader":{
    "status":0,
    "QTime":11,
    "params":{
      "q":"*:*",
      "_":"1549456677891"}},
  "response":{"numFound":4,"start":0,"maxScore":1.0,"docs":[
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Lightning Thief"],
        "pages":[384],
        "price":[12.5],
        "id":"776afcaf-0d4d-4fa8-a953-002b138ffc47",
        "author_str":["Rick Riordan"],
        "_version_":1624642549903785984,
        "name_str":["The Lightning Thief"],
        "score":1.0},
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Sea of Monsters"],
        "pages":[304],
        "price":[6.49],
        "id":"d4ad9071-5738-4534-a55f-12411da2125c",
        "author_str":["Rick Riordan"],
        "_version_":1624642549984526336,
        "name_str":["The Sea of Monsters"],
        "score":1.0},
      {
        "author":["Jostein Gaarder"],
        "genre_s":["fantasy"],
        "name":["Sophie's World : The Greek Philosophers"],
        "pages":[64],
        "price":[3.07],
        "id":"22faa20e-dd96-4694-8da0-b4ba300a6718",
        "author_str":["Jostein Gaarder"],
        "_version_":1624642549986623488,
        "name_str":["Sophie's World : The Greek Philosophers"],
        "score":1.0},
      {
        "author":["Michael McCandless"],
        "genre_s":["IT"],
        "name":["Lucene in Action, Second Edition"],
        "pages":[475],
        "price":[30.5],
        "id":"ad0b793d-d660-447f-99d5-b3312c3c14b7",
        "author_str":["Michael McCandless"],
        "_version_":1624642549987672064,
        "name_str":["Lucene in Action, Second Edition"],
        "score":1.0}]
  },
  "highlighting":{
    "776afcaf-0d4d-4fa8-a953-002b138ffc47":{},
    "d4ad9071-5738-4534-a55f-12411da2125c":{},
    "22faa20e-dd96-4694-8da0-b4ba300a6718":{},
    "ad0b793d-d660-447f-99d5-b3312c3c14b7":{}}}

I am performing a search on this data using Jquery as below:

function on_data(data) {
    $('#results').empty();
    var docs = data.response.docs;
    $.each(docs, function(i, item) {
        $('#results').prepend($('<div>' + item.name + '</div>'));
    });

    var total = 'Found ' + docs.length + ' results';
    $('#results').prepend('<div>' + total + '</div>');
}

function on_search() {
    var query = $('#query').val();
    var url = 'http://localhost:8983/solr/techs/select?q=' + query + '&start=0&rows=10&indent=on&wt=json&callback=?&json.wrf=on_data';
    $.getJSON(url);
}

function on_ready() {
    $('#search').click(on_search);
    /* Hook enter to search */
    $('body').keypress(function(e) {
        if (e.keyCode == '13') {
            on_search();
        }
    });
}

$(document).ready(on_ready);

But I do not get any response because the URL "http://localhost:8983/solr/techs/select?q=Rick&start=0&rows=4&indent=on&wt=json&callback=?&json.wrf=on_data" returns the below:

on_data({
  "responseHeader":{
    "status":0,
    "QTime":68,
    "params":{
      "q":"Rick",
      "json.wrf":"on_data",
      "indent":"on",
      "start":"0",
      "callback":"?",
      "rows":"4",
      "wt":"json"}},
  "response":{"numFound":0,"start":0,"maxScore":0.0,"docs":[]
  },
  "highlighting":{}})

But if i search using this URL : "http://localhost:8983/solr/techs/select?q=&start=0&rows=4&indent=on&wt=json&callback=?&json.wrf=on_data" I get the correct response as below:

on_data({
  "responseHeader":{
    "status":0,
    "QTime":3,
    "params":{
      "q":"",
      "json.wrf":"on_data",
      "indent":"on",
      "start":"0",
      "callback":"?",
      "rows":"4",
      "wt":"json"}},
  "response":{"numFound":4,"start":0,"maxScore":1.0,"docs":[
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Lightning Thief"],
        "pages":[384],
        "price":[12.5],
        "id":"776afcaf-0d4d-4fa8-a953-002b138ffc47",
        "author_str":["Rick Riordan"],
        "_version_":1624642549903785984,
        "name_str":["The Lightning Thief"],
        "score":1.0},
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Sea of Monsters"],
        "pages":[304],
        "price":[6.49],
        "id":"d4ad9071-5738-4534-a55f-12411da2125c",
        "author_str":["Rick Riordan"],
        "_version_":1624642549984526336,
        "name_str":["The Sea of Monsters"],
        "score":1.0},
      {
        "author":["Jostein Gaarder"],
        "genre_s":["fantasy"],
        "name":["Sophie's World : The Greek Philosophers"],
        "pages":[64],
        "price":[3.07],
        "id":"22faa20e-dd96-4694-8da0-b4ba300a6718",
        "author_str":["Jostein Gaarder"],
        "_version_":1624642549986623488,
        "name_str":["Sophie's World : The Greek Philosophers"],
        "score":1.0},
      {
        "author":["Michael McCandless"],
        "genre_s":["IT"],
        "name":["Lucene in Action, Second Edition"],
        "pages":[475],
        "price":[30.5],
        "id":"ad0b793d-d660-447f-99d5-b3312c3c14b7",
        "author_str":["Michael McCandless"],
        "_version_":1624642549987672064,
        "name_str":["Lucene in Action, Second Edition"],
        "score":1.0}]
  },
  "highlighting":{
    "776afcaf-0d4d-4fa8-a953-002b138ffc47":{},
    "d4ad9071-5738-4534-a55f-12411da2125c":{},
    "22faa20e-dd96-4694-8da0-b4ba300a6718":{},
    "ad0b793d-d660-447f-99d5-b3312c3c14b7":{}}})

I believe that it is because of spaces in the value i.e "Rick Riordan". In that case could someone please help me in correcting my Jquery code/SOLR config. I am new to SOLR. Also I would like to implement Autocomplete on a search box. Could you please also help me with that?

Thanks in advance!

Upvotes: 0

Views: 285

Answers (1)

Xplod
Xplod

Reputation: 43

I finally found an answer to my question!! It is not required to specify the field name in the query like "e.g. q=author:"Rick*"". SOLR can perform a search on the values of the fields which we specify under DisMax Parameters "qf" in solrconfig.xml. Something like below:

<requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
    <lst name="defaults">
      <str name="echoParams">explicit</str>
      <int name="rows">10</int>

      <!-- Query settings -->
   <str name="defType">edismax</str>
   <str name="qf">
   author name pages price genre_s
   </str>
    <str name="df">*_str</str>
    <str name="mm">100%</str>
       <str name="q.alt">*:*</str>
       <str name="rows">10</str>
       <str name="fl">*,score</str> 
    </lst>
  </requestHandler>

Now if i search using the URL "http://localhost:8983/solr/techs/select?q=rick" OR "http://localhost:8983/solr/techs/select?q='Rick'&start=0&rows=4&indent=on&wt=json&callback=?&json.wrf=on_data" , I get the below response:

{
  "responseHeader":{
    "status":0,
    "QTime":8,
    "params":{
      "q":"rick"}},
  "response":{"numFound":2,"start":0,"maxScore":0.6931472,"docs":[
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Lightning Thief"],
        "pages":[384],
        "price":[12.5],
        "id":"776afcaf-0d4d-4fa8-a953-002b138ffc47",
        "author_str":["Rick Riordan"],
        "_version_":1624642549903785984,
        "name_str":["The Lightning Thief"],
        "score":0.6931472},
      {
        "author":["Rick Riordan"],
        "genre_s":["fantasy"],
        "name":["The Sea of Monsters"],
        "pages":[304],
        "price":[6.49],
        "id":"d4ad9071-5738-4534-a55f-12411da2125c",
        "author_str":["Rick Riordan"],
        "_version_":1624642549984526336,
        "name_str":["The Sea of Monsters"],
        "score":0.6931472}]
  }
 }

Upvotes: 1

Related Questions