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

Term stats script score POC #108063

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

afoucret
Copy link
Contributor

@afoucret afoucret commented Apr 30, 2024

A proof of concept of term stats query.

Add documentFrequencyStatistics and termFrequencyStatistics helpers to the painless script_score query so it can be used to build term stats based query.

To be continued to add additional stats (total term frequency, term positions).

##Example:##

  1. Add few docs:
PUT /movies/_doc/1
{ "title": "star wars"}

PUT /movies/_doc/2
{
  "title": "star trek"
}

PUT /movies/_doc/3
{ "title": "rambo"}
  1. Test the documentFrequencyStatistics helper:
GET /_search
{
  "query": {
    "script_score": {
      "query": { "match_all": {} },
      "script": {
        "source": "return documentFrequencyStatistics(\"title\", \"star wars story\").getMin()"        
      }
    }
  }
}

Output:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 2,
    "hits": [
      {
        "_index": "movies",
        "_id": "2",
        "_score": 2,
        "_source": {
          "title": "star trek"
        }
      },
      {
        "_index": "movies",
        "_id": "1",
        "_score": 2,
        "_source": {
          "title": "star wars"
        }
      },
      {
        "_index": "movies",
        "_id": "3",
        "_score": 2,
        "_source": {
          "title": "rambo"
        }
      }
    ]
  }
}
  1. Test the termFrequencyStatistics helper:
GET /_search
{
  "query": {
    "script_score": {
      "query": { "match_all": {} },
      "script": {
        "source": "return termFrequencyStatistics(\"title\", \"star wars\").getSum()"        
      }
    }
  }
}
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 2,
    "hits": [
      {
        "_index": "movies",
        "_id": "1",
        "_score": 2,
        "_source": {
          "title": "star wars"
        }
      },
      {
        "_index": "movies",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "star trek"
        }
      },
      {
        "_index": "movies",
        "_id": "3",
        "_score": 0,
        "_source": {
          "title": "rambo"
        }
      }
    ]
  }
}

@afoucret afoucret requested a review from a team as a code owner April 30, 2024 10:09
@afoucret afoucret marked this pull request as draft April 30, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants