# Hamming Distance on Binary Strings - Latest

**URL:** <https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292>\
**Category:** Elasticsearch\
**Created:** [April 19, 2023, 9:48am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292 "2023-04-19T09:48:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [April 19, 2023, 9:48am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/1 "2023-04-19T09:48:16Z")

</div>

Hello,

I want to do hamming distance on binary strings in elasticsearch, and I want to control the distance.  
In my case, the binary strings have a length of 256 and I want everything with a hamming distance of 32 or below.

Is this possible using fuzzy searching? Looking at the docs, the `fuzziness` parameter for fuzzy queries only accepts values; `0`, `1`, `2` or `AUTO`.

Apparently with `AUTO` I can set low and high distance values, so perhaps: `AUTO:0,32` would work?

Can someone confirm this? Is anyone else doing this? If so, how?

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [April 19, 2023, 10:38am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/2 "2023-04-19T10:38:27Z")

</div>

I've done some experiments, but aren't getting quite the results I expected.

I have several hashes, with varying hamming distances from my query hash, but results for the query:

```auto
{
  "query": {
    "fuzzy": {
      "hash.keyword": {
        "value": "01010011...etc...10010",
        "fuzziness": "AUTO:0,32",
        "max_expansions": 50,
        "prefix_length": 0,
        "transpositions": true
      }
    }
  }
}

```

only returns results where the hamming distance \<= 2.

Is that expected?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 19, 2023, 10:45am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/3 "2023-04-19T10:45:58Z")

</div>

Yes, that is expected. `AUTO` adjusts the distance based on the length of the string, but the maximum is still `2`.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [April 19, 2023, 10:47am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/4 "2023-04-19T10:47:28Z")

</div>

Thanks for the response.

Is there a reason why the limitation is 2? Is there any way to do what I want to do with elasticsearch?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 19, 2023, 10:48am UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/5 "2023-04-19T10:48:59Z")

</div>

I believe the reason for capping this is performance. I do not have any suggested way to do what you want so will leave that for others.

You may be able to do it using a script query or a runtime field, but I am not sure. this would have an impact on performance though, especially at scale.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [April 21, 2023, 2:27pm UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/6 "2023-04-21T14:27:14Z")

</div>

I tried various scoring scripts, but they're not in the least bit performant.

OpenSearch has a hamming distance plugin. So it looks like I'll be migrating to that. 😢

---

<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:** [May 19, 2023, 2:27pm UTC](https://discuss.elastic.co/t/hamming-distance-on-binary-strings-latest/330292/8 "2023-05-19T14:27:36Z")

</div>

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