# Jaccard similarities

**URL:** <https://discuss.elastic.co/t/jaccard-similarities/310324>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 21, 2022, 4:58pm UTC](https://discuss.elastic.co/t/jaccard-similarities/310324 "2022-07-21T16:58:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsaraph](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@vsaraph](https://discuss.elastic.co/u/vsaraph)\
**Post date:** [July 21, 2022, 4:58pm UTC](https://discuss.elastic.co/t/jaccard-similarities/310324/1 "2022-07-21T16:58:20Z")

</div>

I have an index, say `attributes`, whose documents all have a field, say `items`, which is an array of strings. I want to be able to take an array of strings, and write an elasticsearch query that gives me back all documents in `attributes` whose `items` have a high enough Jaccard similarity with array I passed, as well as the Jaccard similarity score.

I've managed to write a fairly contrived `terms_set` query to do this, I just needed to add an `items_count` field, at index time, that stores the length of `items` as well:

```auto
"query": {
    "terms_set": {
      "items": {
        "terms": ["item1", item2", "item3"],
        "minimum_should_match_script": {
          "source": "params['thresh'] * (params.num_terms + doc['items_count'].value) / (1 + params['thresh'])",
          "params": {
            "thresh": 0.3
          }
        }
      }
    }
  }

```

One can change `thresh` to vary the similarity threshold to filter by. This doesn't give me back the Jaccard similarity of each document though. Is there a way I can do this, or is there a better way of computing Jaccard similarities in general, maybe with a painless script?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 18, 2022, 4:58pm UTC](https://discuss.elastic.co/t/jaccard-similarities/310324/2 "2022-08-18T16:58:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
