2

I'm currently trying to query ES through Python, using elasticsearch-py but with no success... I'm testing the request on the "elasticsearch-head" plugin and it works fine (results come scored)

However, when I do it in Python something seems to be wrong and although I get the same results, they are not scored properly...

here is the code:

custom_query={
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "lang": "groovy", 
            "script": "_score+20",
            "params":{"myMap":{}}
          }
        }
      ]
    }
  }
}

This is my custom query, very simple, just to test the scoring mechanism.

src = es.search(index=['test'],scroll='60s',search_type='scan',size=10,body=custom_query)

now I test it out:

while(1):
    src = es.scroll(scroll_id=sid, scroll='60s')
    sid = src['_scroll_id']
    kws.extend(src['hits']['hits'])

All this gives is a set of results that have been improperly scored... I've even enabled "_explanation" to understand if the script is being utilized... This meaning: - "_score" is always 0 - "_explanation" shows something like:

{
    'description': 'function score',
    product of:'',
        'value': 21.0,
        'details': [
            {
                'description': 'ConstantScore(*:*)',
                product of:'',
                    'value': 1.0,
                    'details': [
                        {'description': 'boost', 'value': 1.0}, {'description': 'queryNorm', 'value': 1.0}
                    ]
            },
            {
                'description': 'Math.min of',
                'value': 21.0,
                'details': [
                    {
                        'description': 'script score function',
                        computed with script:"return _score+30;" and parameters: \n{myMap={}}', 'value': 21.0
                    },
                    {
                        'description': 'maxBoost',
                        'value': 3.4028235e+38
                    }
                ]
            },
            {
                'description': 'queryBoost',
                'value': 1.0
            }
        ]
}
Garry Welding
  • 3,599
  • 1
  • 29
  • 46
besnico
  • 253
  • 2
  • 4
  • 13
  • what errors do you get? your question seems like it cuts off before you meant it to. – Garry Welding Apr 17 '15 at 10:47
  • I don't get any errors. Sorry, I kind of missed the wrapping up part, updating the question now :D – besnico Apr 17 '15 at 11:11
  • It seems to be the fact that you're doing a match_all query and not actually passing anything through to search against, so obviously ES doesn't have anything to score for. You're asking ES to return all documents without and kind of search term, so it can't score a document based off nothing... You need to do something like a boolean query instead of a match_all. – Garry Welding Apr 17 '15 at 12:17
  • Oh, I see, but I still want it to score all the results of the index I'm searching through. I'll try to just do a boolean query for something I know will return all items on the index. However, the weird thing is that it returns the same results and the right score when I perform exactly the same query through the "AnyRequest" tab on elasticsearch-head... – besnico Apr 17 '15 at 12:44
  • That's fine, but to generate a score you need to have something to score against. How can you generate a score without a query string to score documents against? What would you be scoring against without a query? How would you score documents against each other? Your best bet is to set the minimum score to 0 and set the limit on the results to match the number of stored documents. That way you'll get all documents returned in score order, with an actual score against them. – Garry Welding Apr 17 '15 at 12:45

1 Answers1

0

It seems the problem is with ElasticSearch itself... "search-type" : "scan" does not allow for scoring multiple results.

A more thorough explanation of the issue is provided in here

besnico
  • 253
  • 2
  • 4
  • 13