# Lucene InBuilt Scalar Quantization

**URL:** <https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750>\
**Category:** k-NN\
**Created:** [August 9, 2024, 7:05am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750 "2024-08-09T07:05:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 9, 2024, 7:05am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/1 "2024-08-09T07:05:52Z")

</div>

OpenSearch 2.16.0 introduced InBuilt Scalar Quantization which stores and retrieve quantized vectors automatically. However, I spotted the below mentioned in the documentation. I was expecting this feature to shrink disk space at the cost of small decrease of recall, but according to the documentation it would increase the disk utilization! If that is the case, then what is the use of this feature? And is there a way to keep only the quantized vector and ignore the original?

[k-NN vector quantization - OpenSearch Documentation](https://opensearch.org/docs/latest/search-plugins/knn/knn-vector-quantization/)  
“Quantization can decrease the memory footprint by a factor of 4 in exchange for some loss in recall. Additionally, quantization slightly increases disk usage because it requires storing both the raw input vectors and the quantized vectors.”

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [August 9, 2024, 3:55pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/2 "2024-08-09T15:55:38Z")

</div>

@asfoorial Thanks for your question. We need to store the raw vectors along with quantized vectors because during forcemerge or if the distribution of the input data to be ingested changes over the period of time, the quantiles will change and might need to recompute the quantiles and requantize the data (using raw vectors).

The motive of this feature is to reduce memory utilization and quantize the data dynamically, unlike the Lucene byte vector feature where users needs to quantize the data into byte range before ingesting into the index which has become a bottleneck if the min max values or distribution of their input float data changes over the period of time.

Also, memory is expensive compared to disk. But, if you are concerned about disk usage then this feature comes with a tradeoff.

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [August 12, 2024, 11:41am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/3 "2024-08-12T11:41:52Z")

</div>

Is your goal just doing Semantic Search, not Hybrid Search which is a combination of Semantic and Lexical(aka keyword) search?

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 12, 2024, 12:28pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/4 "2024-08-12T12:28:28Z")

</div>

My search can combine both. But let us assume that I am only using semantic search only, is there a way to avoid storing the original vector and keep only the quantized version, just like it is done in faiss scalar quantization? I noticed around a 25% shrink in storage size there.

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [August 12, 2024, 4:14pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/5 "2024-08-12T16:14:29Z")

</div>

Then you should quantize the vectors into byte sized vectors before ingesting into the index and use the lucene byte vector feature. This helps to reduce both memory and disk usage besides reducing latencies at a cost of drop in recall.

> **[Byte-quantized vectors in OpenSearch](https://opensearch.org/blog/byte-quantized-vectors-in-opensearch/)**
>
> In OpenSearch k-NN plugin when we index and query vectors of type float, each vector element occupies 4 bytes. This is getting expensive in terms of memory and storage, especially for large-scale use cases. Starting with the OpenSearch 2.9 release,...

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 12, 2024, 4:54pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/6 "2024-08-12T16:54:22Z")

</div>

The problem with external quantization is that I won’t be able to use neural-search query.

Also, is there a way to perform quantization through ml-common?

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [August 12, 2024, 5:27pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/7 "2024-08-12T17:27:07Z")

</div>

Yes, there is an option through ml-commons using cohere-v3 model. Please take a look at this tutorial  
[ml-commons/docs/tutorials/semantic\_search/semantic\_search\_with\_byte\_quantized\_vector.md at main · opensearch-project/ml-commons · GitHub](https://github.com/opensearch-project/ml-commons/blob/main/docs/tutorials/semantic_search/semantic_search_with_byte_quantized_vector.md)?

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 12, 2024, 6:59pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/8 "2024-08-12T18:59:22Z")

</div>

Unfortunately that requires internet connectivity. I need to do that offline. But thanks for sharing this interesting tutorial, worth exploring.

Could you please confirm if neural-search can work with any byte vector option?

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [August 12, 2024, 10:31pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/9 "2024-08-12T22:31:40Z")

</div>

Lucene byte vector will work with neural-search. Can you pls explain what are your limitations to use it with neural-search ?

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [August 13, 2024, 1:06am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/10 "2024-08-13T01:06:07Z")

</div>

I think @asfoorial can reduce disk usage via Lucene’s byte\_vector, but since cohere api needs external internet connection to `https://api.cohere.ai/v1/embed`, the offline environment cannot deal with this method.

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [August 16, 2024, 6:12am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/11 "2024-08-16T06:12:47Z")

</div>

@navtat  
From the version 2.16,  
the k-NN plugin supports built-in scalar quantization for the Lucene engine. Unlike the [Lucene byte vector](https://opensearch.org/docs/latest/field-types/supported-field-types/knn-vector#lucene-byte-vector), which requires you to quantize vectors before ingesting the documents, the Lucene scalar quantizer quantizes input vectors in OpenSearch during ingestion.

> **[k-NN vector quantization](https://opensearch.org/docs/latest/search-plugins/knn/knn-vector-quantization/#lucene-scalar-quantization)**
>
> k-NN vector quantization

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [August 16, 2024, 6:20am UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/12 "2024-08-16T06:20:02Z")

</div>

> **[Performance tuning](https://opensearch.org/docs/latest/search-plugins/knn/performance-tuning/#expert-level-disable-vector-field-storage-in-the-source-field)**
>
> Performance tuning

When defining an index for vector search, you can omit \_source of vector field using “ **excludes** ” and “ **recovery\_source\_excludes** ” settings. Since Lucene-based engines(OpenSearch, Elasticsearch) store original data as ‘\_source’ field, we can easily reindex existed indices.

If it’s okay to give up this convenience and reduce disk space, you can use the settings of index mapping.

( **WARN** : Disabling the `_recovery_source` may lead to failures during peer-to-peer recovery)

```auto
  PUT /<index_name>/_mappings
  {
      "_source": {
        "excludes": ["location"],
        "recovery_source_excludes": ["location"]
      },
      "properties": {
          "location": {
              "type": "knn_vector",
              "dimension": 2,
              "method": {
                  "name": "hnsw",
                  "space_type": "l2",
                  "engine": "faiss"
              }
          }
      }
  }

```

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 16, 2024, 3:21pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/13 "2024-08-16T15:21:56Z")

</div>

Thanks for the hint. It works even better this way and reduces almost 50% of the storage size as shown in the experiment below! I also noticed that having scalar quantization will result in little additional storage overhead (~6%).

However, is any implication and risks to this? Is this any functionality to fail other than calling \_reindex API?

 ![size_experiment](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/5/5d69a71c1d44d967d7d4148f6f7d8224bb3ddb52.jpeg)

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [August 16, 2024, 3:42pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/14 "2024-08-16T15:42:38Z")

</div>

I don’t think so. It would be okay to index data without \_source if data have no change.

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [August 16, 2024, 4:46pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/15 "2024-08-16T16:46:58Z")

</div>

Yes, as @yeonghyeonKo suggested you can either disable \_source or exclude source fields to reduce disk usage. But, you can’t reindex the data. However, once this new feature is added you can reindex the data even after disabling the source. Feel free to +1 if you are interested in it. Thanks!

> <https://github.com/opensearch-project/k-NN/pull/1571>
>
> \### Description
> in some scenarios, we want to \`reduce the disk usage\` and \`io t…hroughput\` for the source field. so, we would excludes knn fields in mapping which do not store the source like( this would make knn field can not be retrieve and rebuild)
> 
> \`\`\`
> "mappings": { 
> "\_source": { 
> "excludes": \[
> "target\_field1",
> "target\_field2",
> \]
> }
> }
> \`\`\`
> 
> so I propose to use doc\_values field for the vector fields. like:
> 
> \`\`\`
> POST some\_index/\_search
> {
> "docvalue\_fields": \[
> "vector\_field1",
> "vector\_field2",
> \],
> "\_source": false
> }'
> \`\`\`
> 
> \### Proposal
> 
> 1. Rewrite \`KNNVectorDVLeafFieldData\` get data from docvalues
> 
> i rewrite \`KNNVectorDVLeafFieldData\` and make \`KNN80BinaryDocValues\` can return the specific knn \`docvalue\_fields\` like: (\`vector\_field1\` is knn field type)
> 
> \`\`\`
> "hits":\[{"\_index":"test","\_id":"1","\_score":1.0,"fields":{"vector\_field1":\["1.5","2.5"\]}},{"\_index":"test","\_id":"2","\_score":1.0,"fields":{"vector\_field1":\["2.5","1.5"\]}}\]
> \`\`\`
> 
> \*\*optimize result:\*\* 
> 1m SIFT dataset, 1 shard, 
> \*\*with source store: 1389MB\*\*
> \*\*without source store: 1055MB(-24%)\*\*
> 
> for the continues dive in to \`knndocvalues\` fields, I think when use faiss engine, we can use \`reconstruct\_n\` interface to retrieve the specific doc values and save the disk usage for \`BinaryDocValuesFormat\`. or like this issue comments for redesign a \`KnnVectorsFormat\`
> 
> 2. composite vector field to \_source
> 
> I added \`KNNFetchSubPhase\` and add a processor like \`FetchSourcePhase#FetchSubPhaseProcessor\` to combine the \`docvalue\_fields\` into \`\_source\` something like \`synthetic\` logic
> 
> \### Issues Resolved
> 
> \#1087
> \#1572 
> 
> \- 1st I made \`KNNVectorDVLeafFieldData\` can return the vectorDocValue fields like script do. 
> \- 2nd I write a KNNFetchSubPhase class which add a process in fetch phase, and it can fulfill the \`\_source\` with 1st step docValues fields response. and this way something like \`synthetic source\` but need explicit add value from search body like \`docvalue\_fields\` 
>  
> \### Check List
> \- \[\] New functionality includes testing.
> - \[\] All tests pass
> \- \[\] New functionality has been documented.
> - \[\] New functionality has javadoc added
> \- \[\] Commits are signed as per the DCO using --signoff 
> 
> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
> For more information on following Developer Certificate of Origin and signing off your commits, please check \[here\](https://github.com/opensearch-project/k-NN/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [August 16, 2024, 5:08pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/16 "2024-08-16T17:08:53Z")

</div>

Thanks, I would certainly like such feature.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [October 15, 2024, 5:09pm UTC](https://forum.opensearch.org/t/lucene-inbuilt-scalar-quantization/20750/17 "2024-10-15T17:09:40Z")

</div>

Closed on October 15, 2024, 5:09pm UTC

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