# Force brute vector query

**URL:** <https://discuss.elastic.co/t/force-brute-vector-query/336497>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [June 20, 2023, 2:58pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497 "2023-06-20T14:58:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lone\_Eagle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lone_eagle/32/62213_2.png) [@Lone\_Eagle](https://discuss.elastic.co/u/Lone_Eagle)\
**Post date:** [June 20, 2023, 2:58pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497/1 "2023-06-20T14:58:45Z")

</div>

We are currently having a normal search and want to experiment on vectors. We managed to have a normal knn search but want to create a search query using the brute force of a vector. The query receive a text to search on (converted to a vector using the trainee model).

**Information:**  
Index field with embedding: textsearch\_embedding  
Trainee model: hugging\_face\_model

**Problem:**  
There is an error of non compatible type:

> "caused\_by": {  
> "type": "class\_cast\_exception",  
> "reason": "class java.util.HashMap cannot be cast to class java.util.List (java.util.HashMap and java.util.List are in module java.base of loader 'bootstrap')"  
> }

**Query:**

```auto
{
  "query": {
    "script_score": {
      "query" : {
...
        }
      },
      "script": {
        "source": "cosineSimilarity(params.query_vector_builder, 'textsearch_embedding') + 1.0",
        "params": {
          "query_vector_builder": {
            "text_embedding": {
              "model_id": "hugging_face_model",
              "model_text": "coffee 4 cups"
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [June 20, 2023, 3:19pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497/2 "2023-06-20T15:19:28Z")

</div>

Hey @Lone_Eagle ,

`query_vector_builder` is only supported in `knn` (approximate search) for now. That is why you are receiving a parsing error.

---

<div class="post-metadata">

**Author:** ![Lone\_Eagle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lone_eagle/32/62213_2.png) [@Lone\_Eagle](https://discuss.elastic.co/u/Lone_Eagle)\
**Post date:** [June 20, 2023, 4:13pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497/3 "2023-06-20T16:13:24Z")

</div>

I tried to convert the type but still ran into issues. Is something like this possible?

```
    "source": """
      List mFloatList = new ArrayList();

      for (Map.Entry mFloat : params.query_vector_builder.entrySet())
      { 
          mFloatList.add(mFloat.getValue());
      }

      return cosineSimilarity(mFloatList, 'textsearch_embedding') + 1.0
      """,
    "params": {
      "query_vector_builder": {
        "text_embedding": {
          "model_id": "hugging_face_model",
          "model_text": "coffee 4 cups"
        }
      }
    }

```

**Got that error:**

```
"caused_by": {
  "type": "class_cast_exception",
  "reason": "class_cast_exception: class org.elasticsearch.script.field.EmptyField cannot be cast to class org.elasticsearch.script.field.vectors.DenseVectorDocValuesField (org.elasticsearch.script.field.EmptyField and org.elasticsearch.script.field.vectors.DenseVectorDocValuesField are in module org.elasticsearch.server@8.8.1 of loader 'app')"
}

```

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [June 20, 2023, 5:17pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497/4 "2023-06-20T17:17:42Z")

</div>

@Lone_Eagle as I sad, `query_vector_builder` and the `text_embedding` part are not available in brute force nearest neighbor. Only in `knn`.

For now, if you want to use brute force, you must infer against the model in a separate API call.

---

<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:** [July 18, 2023, 5:17pm UTC](https://discuss.elastic.co/t/force-brute-vector-query/336497/5 "2023-07-18T17:17:52Z")

</div>

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