Coder Social home page Coder Social logo

No score on query about elasticsearch-php HOT 3 CLOSED

elastic avatar elastic commented on June 22, 2024
No score on query

from elasticsearch-php.

Comments (3)

polyfractal avatar polyfractal commented on June 22, 2024

The sort is your problem. You'll notice that your PHP command and Sense commands are actually different, since you are requesting fields and a sort value in PHP but not in sense. A 1:1 translation into Sense would be:

GET /inver*/search?fields=_source,name,date&sort:url_date:desc&size=100
{
   "filter":{
      "term":{
         "url":"some value here"
      }
   },
   "query":{
      "match":{
         "linktext":"some value here"
      }
   }
}

Which give you similar NULL values in score. This is because you are asking Elasticsearch to only sort by the url_date, so scores are meaningless...the results come back in the order of the date. If you want scores as well, include the _score value in the sort parameter:

In PHP:

$json='{
   "filter":{
      "term":{
         "url":"%s"
      }
   },
   "query":{
      "match":{
         "linktext":"%s"
      }
   }
}';
$qStr = sprintf($json,$url,$anchortext);

$params['index'] =get_domain_index($requesturl,"inverted");
$params['type'] ="inverted";
$params['fields'] ="_score,url,referer,linktext";
$params['body'] =$qStr;
$params['sort']="url_date:desc,_score:desc";  // Notice the addition to _score here
$params['size']=100;

try{
    $rc = $GLOBALS['e']->search($params);
}catch (Exception $error){
    echo "Not found\n";
    //echo $error." Error Getting record\n";
}

And in Sense:

GET /inver*/search?fields=_source,name,date&sort:url_date:desc,_score:desc&size=100
{
   "filter":{
      "term":{
         "url":"%s"
      }
   },
   "query":{
      "match":{
         "linktext":"%s"
      }
   }
}

That should fix your problem :)

Now, with that said, you should probably be specifying the sort value in the query body rather than the URL parameters, since it allows more configuration:

$json='{
   "filter":{
      "term":{
         "url":"%s"
      }
   },
   "query":{
      "match":{
         "linktext":"%s"
      }
   },
   "sort": [
      {
         "url_date": {
            "order": "desc"
         }
      },
      {
         "_score": {
            "order": "desc"
         }
      }
   ]
}';
$qStr = sprintf($json,$url,$anchortext);

$params['index'] =get_domain_index($requesturl,"inverted");
$params['type'] ="inverted";
$params['fields'] ="_score,url,referer,linktext";
$params['body'] =$qStr;
$params['size']=100;

try{
    $rc = $GLOBALS['e']->search($params);
}catch (Exception $error){
    echo "Not found\n";
    //echo $error." Error Getting record\n";
}

from elasticsearch-php.

tapit69 avatar tapit69 commented on June 22, 2024

WOW!!! What a great answer.. Makes perfect sense. Thank you very very much

from elasticsearch-php.

polyfractal avatar polyfractal commented on June 22, 2024

No problem, glad to help :)

Closing this ticket, feel free to reopen or make a new one if you have any other issues.

from elasticsearch-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.