# Is indexing with HNSW required for kNN to work?

**URL:** <https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [December 18, 2023, 9:36pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598 "2023-12-18T21:36:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmaccou](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mmaccou](https://discuss.elastic.co/u/mmaccou)\
**Post date:** [December 18, 2023, 9:36pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598/1 "2023-12-18T21:36:13Z")

</div>

While setting up some search templates for kNN, I discovered HNSW and the parameters for index options.

```auto
"index_options": {
          "type": "hnsw",
          "m": 32,
          "ef_construction": 100
        }

```

When indexing all my data, I didn't set the type to "hnsw". Does this mean kNN will not work at all? Or lower accuracy? I'm trying to understand the impact before I spend the time reindexing.

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [December 19, 2023, 2:57pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598/2 "2023-12-19T14:57:50Z")

</div>

Hi @mmaccou !

`"type": "hnsw"` is the default (and the only value supported) for `index_options` on `dense_vector` field types. You don't have to explicitly set that on your index for knn to work.

There are two ways of performing [kNN search](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html):

- [Approximate knn](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#approximate-knn), which does require your field to be indexed.
- [Exact knn](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#exact-knn), which does not require indexing.

The parameter that controls the indexing of the field (and thus, approximate knn search) is `index` (see [dense\_vector field type parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html#dense-vector-params)).

Depending of the Elasticsearch version you used to create the index, it will be indexed by default ([8.11](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/dense-vector.html#dense-vector-params) or higher) or not ([8.10](https://www.elastic.co/guide/en/elasticsearch/reference/8.10/dense-vector.html#dense-vector-params) and previous).

If you are using approximate kNN:

- If you created your index before 8.11 and didn't have `"index": true` as a parameter for your `dense_vector` field type, you will need to create a new index and ensure your `dense_vector` field is indexed. You can use [reindex](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) for transferring your data to the new index.
- If you created your index on version 8.11 or above, you don't need to do anything for approximate knn to work.

If you are using exact kNN, you need no index changes as you don't need your field to be indexed.

I hope that helped!

---

<div class="post-metadata">

**Author:** ![mmaccou](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mmaccou](https://discuss.elastic.co/u/mmaccou)\
**Post date:** [December 19, 2023, 3:16pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598/3 "2023-12-19T15:16:45Z")

</div>

Very helpful thank you! I confirmed I have 8.11, so just to confirm, both of the index mappings below are essentially identical given the default settings.

```auto
"properties": {
      "ml": {
        "properties": {
          "inference": {
            "properties": {
              "chunkedContent": {
                "properties": {
                  "predicted_value": {
                    "type": "dense_vector",
                    "dims": 768,
                    "index": true,
                    "similarity": "cosine",
                    "index_options": {
                      "type": "hnsw",
                      "m": 32,
                      "ef_construction": 100

```

```auto
"properties": {
      "ml": {
        "properties": {
          "inference": {
            "properties": {
              "chunkedContent": {
                "properties": {
                  "predicted_value": {
                    "type": "dense_vector",
                    "dims": 768,
                    "index": true,
                    "similarity": "cosine"

```

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [December 19, 2023, 3:36pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598/4 "2023-12-19T15:36:26Z")

</div>

From the knn point of view, they are equivalent as both are indexed explicitly.

The only change is `m` option in index\_options that change from the default (16). It affects the internal HNSW structure, but doesn't affect the usage from the knn point of view.

---

<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:** [January 16, 2024, 3:36pm UTC](https://discuss.elastic.co/t/is-indexing-with-hnsw-required-for-knn-to-work/349598/5 "2024-01-16T15:36:43Z")

</div>

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