【问题标题】:Search box value is replaced with page number selected搜索框值替换为选定的页码
【发布时间】:2020-04-01 05:38:35
【问题描述】:

我正在尝试使用 elasticsearch 和 PHP 制作搜索引擎。我正在使用 AJAX 进行实时数据搜索。在我应用分页之前它工作正常。当我应用分页时,我的搜索查询被替换为页码。即搜索框中的输入被替换为页码并根据检索到的文档。 这是我在 index.php 文件中的代码:

<div class="container">
   <br />
   <h2 align="center">Search for any course</h2><br />
   <div class="form-group">
    <div class="input-group">
     <span class="input-group-addon">Search</span>
     <input type="text" name="search_text" id="search_text" placeholder="Search here..." class="form-control" />

     <br>
    </div>
   </div>
   <br />
   <div id="result"></div>
  </div>

<script>
$(document).ready(function(){

 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"fetch_nptel.php",
   method:"POST",
   data:{query:query},
   success:function(data)
   {
    $('#result').html(data);
   }
  });
 }

 $(document).on('click', '.pagination_link', function(){  
    var page = $(this).attr("id"); 
    load_data(page);  
  });

 $('#search_text').keyup(function(){
  var search = $(this).val();
  if(search != "")
  {
   load_data(search);
  }
  else
  {
   load_data();
  }
 });
});
</script>

这里是 fetch.php 中的代码:

<?php
session_start();
require_once '../vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();

$record_per_page = 10;
$page='';
$output = '';
if(isset($_POST["page"]))  
{  
    $page = $_POST["page"];  
}  
else  
{  
    $page = 1;  
}  
$start_from = ($page - 1)*$record_per_page;  

if(isset($_POST["query"]))
{
 $link='';
 $search = $_POST["query"];
 $query = $client->search([
    'body' => [
      'query' => [
        'bool' => [
          'must' => [
            'multi_match' => [
              'fields' => ['title','University','Professor','description'],
              'query' => $search
            ]
          ]
        ]
      ],

      'from' => 0,
      'size' => 350
    ]
  ]);

 $n = count($query['hits']['hits']);

 $query = $client->search([
    'body' => [
      'query' => [
        'bool' => [
          'must' => [
            'multi_match' => [
              'fields' => ['title','University','Professor','description'],
              'query' => $search
            ]
          ]
        ]
      ],

      'from' => $start_from,
      'size' => $record_per_page
    ]
  ]);

  if($query['hits']['total']>=1){
    $results = $query['hits']['hits'];
  }

}

if(isset($results))
{
 $output .= '
  <div style="margin-bottom: 10px; margin-top: -10px;">Search results for "<b>'.$search.'</b>"</div>
  <div style="margin-bottom: 10px; margin-top: -10px;">Found '.$n.' results</div>
 ';

 /*This foreach loop is just printing output variable, you may ignore this loop*/

 foreach($results as $r)
 {

  if(isset($r["_source"]["link"])){
  $link = $r["_source"]["link"];
  $output .='
    <div class="card card-1">
    <div id="sticky_header" class="title_header">
      <div class="title"><a class="title_link" href="' . $link . '">'.$r["_source"]["title"].'</a></div>
      <div class="link"><a href="' . $link . '">'.$r["_source"]["link"].'</a></div>
      <hr style="margin-top: 10px;">
    </div>
    <div class="description" style="padding-bottom: 10px;">
    <div style="padding-bottom: 5px;"><b>Description:</b></div>
    <div>'.$r["_source"]["description"].'</div>
    </div>
    <hr style="margin-top: -5px;">
    <div class="div_university">
      <div class="professor" style="font-size: 15px; margin-top: -10px;  margin-bottom: 5px;"><b>Guided '.$r["_source"]["Professor"].'</b></div>
      <div class="university" style="font-size: 15px; margin-top: -5px; margin-bottom: 10px;"><b>Offered By: </b><em style="font-size: 15px;">'.$r["_source"]["University"].'</em></div>
    </div>
    </div>
  ';
  }

 }

 $total_pages = ceil($n/$record_per_page);  
 for($i=1; $i<=$total_pages; $i++)  
 {  
      $output .= '<span class="pagination_link" style="cursor:pointer; padding:6px; border:1px solid #ccc;" id="'.$i.'">'.$i.'</span>';  
 }  
$output .= '</div><br /><br />';
 echo $output;
}
else
{
 echo 'No data found';
}
?>

我可以看到在我的 index.php 中,我的 load_data() 函数的查询值被替换为页码,这就是它以页码作为输入值的原因。 那么我在哪里犯错了? 我非常感谢任何帮助。

【问题讨论】:

    标签: javascript php jquery ajax elasticsearch


    【解决方案1】:

    您正在使用 Javascript 中的页码调用 load_data:load_data(page) 您需要向您的 load_data 添加一个我认为的额外参数。

    function load_data(query, page = 0)
     {
      $.ajax({
       url:"fetch_nptel.php",
       method:"POST",
       data:{query:query, page: page},
       success:function(data)
       {
        $('#result').html(data);
       }
      });
    

    在点击处理程序中,传递两个参数

    $(document).on('click', '.pagination_link', function(){  
        var page = $(this).attr("id"); 
        var search = $('#search_text')
        load_data(search, page);  
      });
    

    最后,您还应该更新您的 PHP 代码。

    【讨论】:

    • 谢谢@Gillespie59...帮助。但在那之后又发现了另一个错误:
    • 致命错误:未捕获的 Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","re​​ason":"numHits must be > 0 ; 如果您只需要总命中数,请使用 TotalHitCountCollector"}],"type":"search_phase_execution_exception","re​​ason":"all shards failed","phase":"query","grouped":true,"failed_shards ":[{"shard":0,"index":"kr_v1","node":"ejVIcMUWSImolzvQXFurpA","re​​ason":{"type":"illegal_argument_exception","re​​ason":"numHits 必须 > 0;如果您只需要总命中数,请使用 TotalHitCountCollector"}}]},"status":400}
    • 这是由于我正在执行的查询。
    • 找到解决方案了吗?
    • 是的,非常感谢@Gillespie59。正如你所说,我必须将页码和我的查询传递给 load_data 文件,并稍微更改了 php 代码并且它起作用了。抱歉,我的笔记本电脑出现了一些问题,我不得不将它送去维修,所以无法回复。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多