Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No score on query #48

Closed
tapit69 opened this issue Mar 3, 2014 · 3 comments
Closed

No score on query #48

tapit69 opened this issue Mar 3, 2014 · 3 comments

Comments

@tapit69
Copy link

tapit69 commented Mar 3, 2014

I executing a query this is the query
$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";
$params['size']=100;

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

each record comes back with a NULL score
[98]=>
array(6) {
["_index"]=>
string(10) "inverted-p"
["_type"]=>
string(8) "inverted"
["_id"]=>
string(32) "255718a4b0d3a938360d4d2f92070e40"
["_score"]=>
NULL
["fields"]=>
array(3) {
["linktext"]=>
array(1) {
[0]=>
string(124) "
However when i exectue the same request with Sense I get the score
POST /inver*/_search
{
"filter": {"term": {
"url": "porscheofranchomirage.com"
}},
"query": {"match": {
"linktext": "porsche dealer"
}}
}
"_index": "inverted-p",
"_type": "inverted",
"_id": "be30dd48eea377b389623318186e3141",
"_score": 2.1395235,
"_source": {
"tld": "porscheofranchomirage.com",
"url": "porscheofranchomirage.com",
"referer": "indigoautogroupsocal.com",
"linktext": "Porsche",
"url_date": "2014-03-03"
}
},
{
"_index": "inverted-p",
"_type": "inverted",
"_id": "7582fb1eb94eec4a8fd3f7e0662b3e9d",
"_score": 1.0697618,
"_source": {
"tld": "porscheofranchomirage.com",
"url": "porscheofranchomirage.com",
"referer": "riversidepca.org",
"linktext": "Porsche of Rancho Mirage",
"url_date": "2014-03-03"

Can you please tell how to get the score in my code.

Thanks

@polyfractal
Copy link
Contributor

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";
}

@tapit69
Copy link
Author

tapit69 commented Mar 3, 2014

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

@polyfractal
Copy link
Contributor

No problem, glad to help :)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants