Using raw vectors for neural sparse search

If you’re using self-hosted sparse embedding models, you can ingest raw sparse vectors and use neural sparse search.

Tutorial

This tutorial consists of the following steps:

  1. Ingest sparse vectors
    1. Create an index
    2. Ingest documents into the index
  2. Search the data using raw sparse vector.

Step 1: Ingest sparse vectors

Once you have generated sparse vector embeddings, you can directly ingest them into OpenSearch.

Step 1(a): Create an index

In order to ingest documents containing raw sparse vectors, create a rank features index:

  1. PUT /my-nlp-index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "id": {
  6. "type": "text"
  7. },
  8. "passage_embedding": {
  9. "type": "rank_features"
  10. },
  11. "passage_text": {
  12. "type": "text"
  13. }
  14. }
  15. }
  16. }

copy

Step 1(b): Ingest documents into the index

To ingest documents into the index created in the previous step, send the following request:

  1. PUT /my-nlp-index/_doc/1
  2. {
  3. "passage_text": "Hello world",
  4. "id": "s1",
  5. "passage_embedding": {
  6. "hi" : 4.338913,
  7. "planets" : 2.7755864,
  8. "planet" : 5.0969057,
  9. "mars" : 1.7405145,
  10. "earth" : 2.6087382,
  11. "hello" : 3.3210192
  12. }
  13. }

copy

Step 2: Search the data using a sparse vector

To search the documents using a sparse vector, provide the sparse embeddings in the neural_sparse query:

  1. GET my-nlp-index/_search
  2. {
  3. "query": {
  4. "neural_sparse": {
  5. "passage_embedding": {
  6. "query_tokens": {
  7. "hi" : 4.338913,
  8. "planets" : 2.7755864,
  9. "planet" : 5.0969057,
  10. "mars" : 1.7405145,
  11. "earth" : 2.6087382,
  12. "hello" : 3.3210192
  13. }
  14. }
  15. }
  16. }
  17. }

copy

To learn more about improving retrieval time for neural sparse search, see Accelerating neural sparse search.