# Semantic search with search correlation between fields

**URL:** https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564
**Category:** Elasticsearch
**Tags:** elastic-stack-machine-learning, vector-search
**Created:** [August 24, 2023, 10:17am UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564 "2023-08-24T10:17:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sivagurlinka](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sivagurlinka](https://discuss.elastic.co/u/sivagurlinka)
#### Post date: [August 24, 2023, 10:17am UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/1 "2023-08-24T10:17:30Z")

</div>

Can semantic search with correlation between fields can be implemented with Elasticsearch ?  
I have ecommerce data indexed to Elasticsearch with below fields and description is vector text embedded.

Name : product name(text)  
Price : product price (Float)  
Description : product description (text)

I would like to have to have semantic search functionality implemented for search if I search something like "products below 100$". expectation is Elasticsearch to return corresponding products less than 100$. Can this functionality achieved with Elasticsearch ? if yes, pls guide with relevant details and documentation for such implementation

**Index Mapping**

```auto
PUT collection-with-embeddings
{
  "mappings": {
    "properties": {
    "text_embedding.predicted_value": {
        "type": "dense_vector",
        "dims": 768,
        "index": true,
        "similarity": "dot_product"
      },
      "price": {
        "type": "float" 
      },
      "name": {
        "type": "text" 
      },
      "description": {
        "type": "text" 
      }
    }
  }
}

```

**KNN Search**

```auto
GET collection-with-embeddings/_search
{
  "knn": {
    "field": "text_embedding.predicted_value",
    "query_vector_builder": {
	"text_embedding": {
          "model_id": "sentence-transformers__msmarco-minilm-l-12-v3",
          "model_text": "products below 100$"
       }
    },
    "k": 5,
    "num_candidates": 10
  }
}

```

---

<div class="post-metadata">

### Author: ![sivagurlinka](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sivagurlinka](https://discuss.elastic.co/u/sivagurlinka)
#### Post date: [August 25, 2023, 6:42am UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/2 "2023-08-25T06:42:18Z")

</div>

@Francois_Protopapa /@Peter_Steenbergen , could you please guide us with the relevant details or documentation for this use case.

---

<div class="post-metadata">

### Author: ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)
#### Post date: [August 25, 2023, 6:27pm UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/3 "2023-08-25T18:27:22Z")

</div>

Hi,

Sounds like you are looking for the filter within the knn search found here:

> **[k-nearest neighbor (kNN) search | Elasticsearch Guide \[8.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-search-filter-example)**

Instead of term in the example you could make use of range here.

---

<div class="post-metadata">

### Author: ![sivagurlinka](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sivagurlinka](https://discuss.elastic.co/u/sivagurlinka)
#### Post date: [August 28, 2023, 2:57pm UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/4 "2023-08-28T14:57:06Z")

</div>

Hi @Peter_Steenbergen , Thank you for your response.  
We are not looking to add filters inside KNN. As we are trying to achieve this with semantic search, we are expecting Elasticsearch to understand intent of user search query.  
Is there anyway to achieve this this either with ELSER or with some other Model can be deployed on Elasticsearch ?

---

<div class="post-metadata">

### Author: ![joshdevins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshdevins/32/62865_2.png) [@joshdevins](https://discuss.elastic.co/u/joshdevins)
#### Post date: [August 29, 2023, 2:49pm UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/5 "2023-08-29T14:49:14Z")

</div>

Hey @sivagurlinka . At the moment the best way to do this in a general purpose way is to use an LLM first to "self query" the index metadata and decide which parts of the query are filters versus keywords. This can be accomplished using the LangChain module of the same name: [Self-querying | 🦜️🔗 Langchain](https://python.langchain.com/docs/modules/data_connection/retrievers/self_query/)

You would need to be cautious with the latency and cost ($) of using an LLM to reflect in this way though.

For a more scalable and cost effective approach, there are several methods to choose from but it's typically best to start with some hand-crafted rules as there is nothing out-of-the-box in Elasticsearch (yet) to do this automatically.

If you go down that road, have a look at libraries like [Duckling](https://github.com/facebook/duckling) which can help.

---

<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: [September 26, 2023, 2:49pm UTC](https://discuss.elastic.co/t/semantic-search-with-search-correlation-between-fields/341564/6 "2023-09-26T14:49:54Z")

</div>

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