du2229 2010-03-04 19:06
浏览 77
已采纳

为什么在Solr工作中不会出现这种情况?

I need to sort on a date-field type, which name is "mod_date".

It works like this in the browser adress-bar:

   http://localhost:8983/solr/select/?&q=bmw&sort=mod_date+desc

But I am using a phpSolr client which sends an URL to Solr, and the url sent is this:

 fq=+category%3A%22Bilar%22+%2B+car_action%3AS%C3%A4ljes&version=1.2&wt=json&json.nl=map&q=%2A%3A%2A&start=0&rows=5&sort=mod_date+desc

 // This wont work and is echoed after this in php:

 $queryString = http_build_query($params, null, $this->_queryStringDelimiter);
 $queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString);

This wont work, I dont know why!

Everything else works fine, all right fields are returned. But the sort doesn't work.

Any ideas?

Thanks

BTW: The field "mod_date" contains something like:

    2010-03-04T19:37:22.5Z

EDIT:

First I use PHP to send this to a SolrPhpClient which is another php-file called service.php:

    require_once('../SolrPhpClient/Apache/Solr/Service.php');
    $solr = new Apache_Solr_Service('localhost', 8983, '/solr/');
    $results = $solr->search($querystring, $p, $limit, $solr_params);

$solr_params is an array which contains the solr-parameters (q, fq, etc).

Now, in service.php:

            $params['version'] = self::SOLR_VERSION;

    // common parameters in this interface
    $params['wt'] = self::SOLR_WRITER;
    $params['json.nl'] = $this->_namedListTreatment;

    $params['q'] = $query;
    $params['sort'] = 'mod_date desc'; // HERE IS THE SORT I HAVE PROBLEM WITH

    $params['start'] = $offset;
    $params['rows'] = $limit;
            $queryString = http_build_query($params, null, $this->_queryStringDelimiter);
            $queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString);

     if ($method == self::METHOD_GET)
    {
 return $this->_sendRawGet($this->_searchUrl . $this->_queryDelimiter . $queryString);
    }
else if ($method == self::METHOD_POST)
    {
    return $this->_sendRawPost($this->_searchUrl, $queryString, FALSE, 'application/x-www-form-urlencoded');
    }

The $results contain the results from Solr... So this is the way I need to get to work (via php).

This code below (also on top of this Q) works but thats because I paste it into the adress bar manually, not via the PHPclient. But thats just for debugging, I need to get it to work via the PHPclient:

  http://localhost:8983/solr/select/?&q=bmw&sort=mod_date+des // Not via phpclient, but works

UPDATE (2010-03-08): I have tried Donovans codes (the urls) and they work fine. Now, I have noticed that it is one of the parameters causing the 'SORT' not to work. This parameter is the "wt" parameter. If we take the url from top of this Q, (fq=+category%3A%22Bilar%22+%2B+car_action%3AS%C3%A4ljes&version=1.2&wt=json&json.nl=map&q=%2A%3A%2A&start=0&rows=5&sort=mod_date+desc), and just simply remove the "wt" parameter, then the sort works. BUT the results appear differently, thus making my php code not able to recognize the results I believe. Donovan would know this I think. I am guessing in order for the PHPClient to work, the results must be in a specific structure, which gets messed up as soon as I remove the wt parameter.

Donovan, help me please...

Here is some background what I use your SolrPhpClient for: I have a classifieds website, which uses MySql. But for the searching I am using Solr to search some indexed fields. Then Solr returns an array of ID:numbers (for all matches of the search criteria). Then I use those ID:numbers to find everything in a MySql db and fetch all other information (example is not searchable information).

So simplified: Search -> Solr returns all matches in an array of ID:nrs -> Id:numbers from Solr are the same as the Id numbers in the MySql db, so I can just make a simple match agains every record with the ID matching the ID from the Solr results array.

I don't use Faceting, no boosting, no relevancy or other fancy stuff. I only sort by the latest classified put, and give the option to users to also sort on the cheapest price. Nothing more.

Then I use the "fq" parameter to do queries on different fields in Solr depending on category chosen by users (example "cars" in this case which in my language is "Bilar").

I am really stuck with this problem here... Thanks for all help

  • 写回答

4条回答 默认 最新

  • dua6992 2010-03-05 17:29
    关注

    As pointed out in the stack overflow comments, your browser query is different than your php client based query - to remove that from the equation you should test with this corrected. To get the same results as the browser based query you're php code should have looked something like this:

    $solr = new Apache_Solr_Client(...);
    
    $searchOptions = array(
      'sort' => 'mod_date desc'
    );
    
    $results = $solr->search("bmw", 0, 10, $searchOptions);
    

    Instead, I imagine it looks more like:

    $searchOptions = array(
        'fq' => 'category:"Bilar" + car_action:Sälje',
        'sort' => 'mod_date desc'
    )
    
    $solr->search("\*:*", 0, 10, $searchOptions);
    

    What I expect you to see is that php client results will be the same as the browser based results, and I imagine the same would happen if you did it the opposite way - take your current parameters from the php client and applied them correctly to the browser based query.

    Now onto your problem, you don't see documents sorted properly.

    I would try this query, which is the equivalent of the php client based code:

    http://localhost:8983/solr/select/?&q=%2A%3A%2A&fq=+category%3A%22Bilar%22+%2B+car_action%3AS%C3%A4ljes&sort=mod_date+desc
    

    versus this query, which moves the filter query into the main query:

    http://localhost:8983/solr/select/?&q=+category%3A%22Bilar%22+%2B+car_action%3AS%C3%A4ljes&sort=mod_date+desc
    

    and see if there is a difference. If there is, then it might be a bug in how results from cached filtered queries are used and sorted by solr - which wouldn't be a problem with the client, but the solr service itself.

    Hope this gets you closer to an anser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化