How to get search results from SOLR using Jquery

8
February 07, 2019, at 05:00 AM

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!

READ ALSO
frontend side (JS or Angular) : Create and save file with large mass of indexed data for ultrieur print

frontend side (JS or Angular) : Create and save file with large mass of indexed data for ultrieur print

I'm working on a monitoring application using Angular and SpringI'm retrieving a large mass of data from the server and I'm printing them in a graphs format

38
Mongoose - Find where array size is equal to field

Mongoose - Find where array size is equal to field

I'm trying to find all documents where an array's size is equal to a field's valueFor example:

47
Job processing with a traditional worker or using a Node server?

Job processing with a traditional worker or using a Node server?

I want to be able to process background jobs that will have multiple tasks associated with itTasks may consist of launching API requests (blocking operations) and manipulating and persisting responses

16
Vue / Vuetify: get dimensions of v-flex

Vue / Vuetify: get dimensions of v-flex

Vuetify has the v-resize directive, and the example shows how to use it with window size

35