# BadRequestError(400, 'search\_phase\_execution\_exception', 'runtime error') when trying to do l2 similarity search

**URL:** <https://discuss.elastic.co/t/badrequesterror-400-search-phase-execution-exception-runtime-error-when-trying-to-do-l2-similarity-search/342649>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields, vector-search\
**Created:** [September 9, 2023, 8:25am UTC](https://discuss.elastic.co/t/badrequesterror-400-search-phase-execution-exception-runtime-error-when-trying-to-do-l2-similarity-search/342649 "2023-09-09T08:25:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![longlegs](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@longlegs](https://discuss.elastic.co/u/longlegs)\
**Post date:** [September 9, 2023, 8:25am UTC](https://discuss.elastic.co/t/badrequesterror-400-search-phase-execution-exception-runtime-error-when-trying-to-do-l2-similarity-search/342649/1 "2023-09-09T08:25:15Z")

</div>

Hello, Im trying to implement a vector similarity search for face recognition using Elasticsearch in python.

This is my mapping for creating an index:

```auto
mapping = {
    "mappings": {
        "properties": {
            "embedding_aaaa":{
                "type": "dense_vector",
                "dims": 128
            },
            "title_name": {"type": "keyword"}
        }
    }
}

```

once this index is created, i insert:

```auto
    video_details = {
        "filename": (unique_filename + ".mp4"),
        "video_title": video_title,
        "tags": tags.split(",") if tags else [],
        "created_at": str(datetime.datetime.now()),
        "created_by": createdBy,
        "uploded_file_size_in_mb": round(os.path.getsize(file_path) / (1024 * 1024), 2)
    }

```

This much data. once that is complete, i use a face recognition model to generate an embedding for each frame i extract from this video and append the frame's timestamp along with the embedding before finally inserting that data onto the same document by using update method.

```auto
embedding_per_frame = []
 ds = {
                "timestamp": timestamp,
                "embedding": embedding
            }
        embedding_per_frame.append(ds.copy())

        update_body = {
        "script": {
            "source": "ctx._source." + "title_vector_aaaa" + " = params." + "title_vector_aaaa",
            "lang": "painless",
            "params": {
                "title_vector_aaa": embedding_per_frame,
            }
     }
    }
        resp = es.update(index=index_name, id=document_id, body=update_body)

```

Now comes the search part where i use l2 distance to find similar vectors:

```auto
        for embed in embeddings:
            query = {
                    "size": 5,
                    "query": {
                    "script_score": {
                        "query": {
                            "match_all": {}
                        },
                        
                        "script": {
                            "source": "1 / (1 + l2norm(params.queryVector, 'embedding_aaaa'))",
                            "params": {
                                "queryVector": list(embed)
                            }
                        }
                    }
                }}

            res = es.search(index="videos, body=query)

```

This is where the error happens.  
if i replace the source statement with this:

```auto
source": "doc['embedding_aaaa'].size() == 0 ? 0 :1 / (1 + l2norm(params.queryVector, 'embedding_aaaa'))", #euclidean distance

```

the error goes away but all the scores are 0.0 which i assume is from the first half of the source statement.

where did i do wrong?. any help is very much appreciated

---

<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:** [October 7, 2023, 8:26am UTC](https://discuss.elastic.co/t/badrequesterror-400-search-phase-execution-exception-runtime-error-when-trying-to-do-l2-similarity-search/342649/2 "2023-10-07T08:26:08Z")

</div>

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