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

Posted

技术标签:

【中文标题】如何使用 Jquery 从 SOLR 获取搜索结果【英文标题】:How to get search results from SOLR using Jquery 【发布时间】:2019-06-30 14:08:30 【问题描述】:

我在 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 的新手。我也想在搜索框上实现自动完成。你能帮我解决一下吗?

提前致谢!

【问题讨论】:

您的查询中没有字段,例如q=author:"Rick*" 那么您是否在 schema.xml 中定义了默认搜索字段,您是否将作者索引到该字段中? 查看***.com/questions/5449727/… 示例 实际上我不想在查询中包含硬编码字段,因为我想使用多个字段进行搜索。有时按作者,有时按名称或流派。我查看了您分享的示例。试试看,谢谢! 我尝试了您建议的示例,不幸的是我没有得到搜索结果 :( 我在托管模式中添加了一个名为“text”的默认字段,并且我的所有字段都有 copyField 条目。并且我将“文本”传递到 SOLR Admin 中的“df”字段并运行 URL:“localhost:8983/solr/techs/select?df=text&q=*Rick*&start=0&rows=10&indent=on&wt=json&callback=?&json.wrf=on_data”.. 我做错什么了吗? 【参考方案1】:

我终于找到了我的问题的答案!不需要在查询中指定字段名称,例如“例如 q=author:“Rick*””。 SOLR 可以对我们在 solrconfig.xml 中的 DisMax 参数“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>

现在,如果我使用 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" 进行搜索,我得到以下回复:


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

【讨论】:

以上是关于如何使用 Jquery 从 SOLR 获取搜索结果的主要内容,如果未能解决你的问题,请参考以下文章

如何使用SOLRJ java客户端从SOLR获取列名(字段)?

检索Solr查询中的特定字段?

检索 Solr 查询中的特定字段?

Solr - 在不返回搜索结果的情况下获取构面计数

如何使用 Solr 进行实时搜索

如何在 Solr 中获取最后一个索引记录?