如何使用Jquery从SOLR获取搜索结果

时间:2019-02-06 15:09:41

标签: jquery json solr autocomplete

我在SOLR中的数据如下:

{
  "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":{}}}

我正在使用Jquery对数据进行搜索,如下所示:

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);

但是我没有收到任何回复,因为URL“ http://localhost:8983/solr/techs/select?q=Rick&start=0&rows=4&indent=on&wt=json&callback=?&json.wrf=on_data”返回了以下内容:

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":{}})

但是,如果我使用以下URL搜索:“ http://localhost:8983/solr/techs/select?q=&start=0&rows=4&indent=on&wt=json&callback=?&json.wrf=on_data”,则会得到如下正确的响应:

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":{}}})

我认为这是因为值中的空格,即“ Rick Riordan”。在这种情况下,有人可以帮助我更正我的Jquery代码/ SOLR配置。我是SOLR的新手。我也想在搜索框中实现自动完成功能。你能帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我终于找到了我问题的答案!!不需要在查询中指定字段名称,例如“ q = author:“ Rick *”“。 SOLR可以搜索我们在solrconfig.xml中的DisMax Parameters“ qf”下指定的字段的值。如下所示:

<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>

现在,如果我使用网址“ http://localhost:8983/solr/techs/select?q=rick”或“ http://localhost:8983/solr/techs/select?q=”搜索 Rick '&start = 0&rows = 4&indent = on&wt = json&callback =?&json.wrf = on_data ,我得到以下回应:

{
  "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}]
  }
 }