Search code examples
elasticsearchgroovyfrequency

Elasticsearch : _score always 0 for tf()


I have this kind of groovy script:

_index[field][term].tf()

I am indexing this groovy script

POST /_scripts/groovy/getTF
{
     "script": "_index[field][term].tf()"
}

Then running the following query always returns _score to be zero (sense command)

POST /my_index/document/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "text": "algorithms"
        }
      },
      "functions": [
        {
          "script_score": {
            "script_id": "getTF",
            "lang" : "groovy",
            "params": {
              "term": "algorithms",
              "field": "text"
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  },
  "size": 10,
  "fields": ["text"]
}

What am I doing wrong here?

This is the mapping for the fields

PUT /ap_dataset/document/_mapping
{
  "document": {
    "properties": {
      "docno": {
        "type": "string",
        "store": true,
        "index": "not_analyzed"
      },
      "text": {
        "type": "string",
        "store": true,
        "index": "analyzed",
        "term_vector": "with_positions_offsets_payloads",
        "analyzer": "my_english"
      }
    }
  }
}

Solution

  • The explanation for the 0 term frequency is that the term you are looking for is not found in the index. Your script receives the term called algorithms (which is plural).

    But the english analyzer is changing plurals to singular, as a result of the english stemmer. So, even if your text is containing algorithms, the term in the index is algorithm which will not be found by _index['text']['algorithms'].