Search code examples
pythonelasticsearch

elasticsearch.BadRequestError: BadRequestError(400, 'search_phase_execution_exception', 'runtime error')


My code is crashing when I execute the following code, I must say I have already documents indexed in ES

db = ElasticVectorSearch(
    elasticsearch_url=url,  # it is fine, I didnt change it
    index_name=os.environ["ELASTICSEARCH_INDEX_NAME"],
    embedding=OpenAIEmbeddings(openai_api_key=os.environ["OPENAI_API_KEY"],
                         deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT"]),
    ssl_verify={"ca_certs": os.environ["APP_LANGCHAIN_CERT_PATH"]})

docs = db.similarity_search(query="my query", 
                            k=10)

Thank you

It was working before, I removed the indexes and I created them with

client.options(ignore_status=400).indices.create(index=os.environ["ELASTICSEARCH_INDEX"])

Solution

  • You need to create an index whose mapping contains a vector field of type dense_vector as you can see in the default mapping created by langchain. It was probably the case in your previous index.

    You can mimick what the langchain code does and do it like this (make sure that dims match the number of dimensions created by OpenAI, usually 1536):

    mapping = {
        "properties": {
            "text": {"type": "text"},
            "vector": {"type": "dense_vector", "dims": 1536},
        }
    }  
    client.options(ignore_status=400).indices.create(index = os.environ["ELASTICSEARCH_INDEX_NAME"], mapping=mapping)
    

    Note that judging by the source code, I'm not sure why langchain doesn't create the index if it doesn't exist.