AWS OpenSearch with OpenSearchVectorSearch generate "failed to create query: Field 'vector_field' is not knn_vector type." #23836
Replies: 1 comment 2 replies
-
To resolve the error "RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: Field 'vector_field' is not knn_vector type.'" and successfully query your vector database in AWS OpenSearchService using LangChain, you need to ensure that the field Here is how you can define the index mapping correctly:
Here is an example of how to create the index with the correct mapping: from langchain.vectorstores import OpenSearchVectorSearch
from langchain.embeddings import BedrockEmbeddings
from requests_aws4auth import AWS4Auth
from opensearchpy import OpenSearch, RequestsHttpConnection
import boto3
service = "aoss"
credentials = boto3.Session().get_credentials()
region = boto3.Session().region_name
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
bedrock_client = boto3.client(service_name="bedrock-runtime")
embeddings = BedrockEmbeddings(
client=bedrock_client,
model_id="amazon.titan-embed-text-v1" # Adjust according to the model you are using
)
opensearch_domain_endpoint = "your-opensearch-domain-endpoint"
opensearch_index = "your-index-name"
vector_field = "vector_field"
opensearch = OpenSearchVectorSearch(
index_name=opensearch_index,
embedding_function=embeddings,
opensearch_url=opensearch_domain_endpoint,
http_auth=awsauth,
connection_class=RequestsHttpConnection,
use_ssl=True,
verify_certs=True,
vector_field=vector_field,
dim=128, # Specify the dimensions of your vectors
)
# Define the index mapping
index_mapping = {
"settings": {"index": {"knn": True, "knn.algo_param.ef_search": 512}},
"mappings": {
"properties": {
"vector_field": {
"type": "knn_vector",
"dimension": 128,
"method": {
"name": "hnsw",
"space_type": "l2",
"engine": "nmslib",
"parameters": {"ef_construction": 512, "m": 16},
},
}
}
},
}
# Create the index if it does not exist
if not opensearch.client.indices.exists(index=opensearch_index):
opensearch.client.indices.create(index=opensearch_index, body=index_mapping)
# Now you can add documents and perform queries
query = "Your query here"
results = opensearch.similarity_search(query, k=5) Ensure that the |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I have created a knowledge base in aws bedrock. I created the knowledge base with default configuration. A vector database has been successfully created in aws OpenSearchService. I want to access that vector database with langchain. When I try I get this error
RequestError: RequestError(400, 'search_phase_execution_exception', "failed to create query: Field 'vector_field' is not knn_vector type."
System Info
langchain==0.2.5
python 3.11
Beta Was this translation helpful? Give feedback.
All reactions