hybrid serach+ filters on AzureSearch as as vector store #24191
Unanswered
aradhanachaturvedi
asked this question in
Q&A
Replies: 1 comment 18 replies
-
To store gender and country fields in AzureSearch using LangChain and apply filtering based on these fields for hybrid search, you need to define these fields in the Here is an example of how you can achieve this: Step 1: Define the gender and country fieldsfrom azure.search.documents.indexes.models import SimpleField, SearchFieldDataType
from langchain_community.vectorstores.azuresearch import AzureSearch
from langchain_openai import OpenAIEmbeddings
# Define the fields including gender and country
fields = [
SimpleField(name="id", type=SearchFieldDataType.String, key=True, filterable=True),
SimpleField(name="content", type=SearchFieldDataType.String, searchable=True),
SimpleField(name="content_vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), searchable=True),
SimpleField(name="metadata", type=SearchFieldDataType.String, searchable=True),
SimpleField(name="gender", type=SearchFieldDataType.String, filterable=True),
SimpleField(name="country", type=SearchFieldDataType.String, filterable=True),
]
# Initialize the AzureSearch instance
azure_search = AzureSearch(
azure_search_endpoint="your_endpoint",
azure_search_key="your_key",
index_name="your_index_name",
embedding_function=OpenAIEmbeddings().embed_query,
fields=fields
) Step 2: Apply filtering based on gender and country fieldsfilters = "gender eq 'male' and country eq 'ABC Country'"
results = azure_search.client.search(
search_text="your_query",
vector_queries=[
VectorizedQuery(
vector=np.array(azure_search.embed_query("your_query"), dtype=np.float32).tolist(),
k_nearest_neighbors=10,
fields="content_vector",
)
],
filter=filters,
query_type="semantic",
semantic_configuration_name=azure_search.semantic_configuration_name,
query_caption="extractive",
query_answer="extractive",
top=10,
) This code snippet demonstrates how to define the gender and country fields and apply filtering based on these fields for hybrid search in AzureSearch using LangChain [1][2][3][4]. |
Beta Was this translation helpful? Give feedback.
18 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
I am creating a Hybrid search but I have a requirement where First I want to filter the content based on user query e.g. ("only male user in ABC Country") here first step should be to filter based on gender and country and out of those filtered document, hybrid search should be applied. I have indexed my data in content field of Azure search which has semi structured data and created embedding out of it . But for better response , I need to apply filtering so how can I store gender and country as a separate field in Azure search , When I used langchain , its not allowing me to index these values, I can see only null values in gender and country with below code.
Document(
page_content=file_data,
gender=gender,
country=country,
metadata='file)
do we need to store these fields on metadata? if yes, how can we apply different filtering based on user query
System Info
langchain==0.1.20
langchain-community==0.0.38
langchain-core==0.1.52
langchain-experimental==0.0.57
langchain-openai==0.1.3
langchain-text-splitters==0.2.2
Beta Was this translation helpful? Give feedback.
All reactions