docs: Add FAISS Filter with Advanced Query Operators Documentation & Demonstration (#28938)

## Description
This pull request updates the documentation for FAISS regarding filter
construction, following the changes made in commit `df5008f`.

## Issue
None. This is a follow-up PR for documentation of
[#28207](https://github.com/langchain-ai/langchain/pull/28207)

## Dependencies:
None.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
RuofanChen03 2025-01-02 16:08:25 -05:00 committed by GitHub
parent ba9dfd9252
commit 5c32307a7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 89 additions and 0 deletions

View File

@ -286,6 +286,52 @@
" print(f\"* {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "39cb1496",
"metadata": {},
"source": [
"Some [MongoDB query and projection operators](https://www.mongodb.com/docs/manual/reference/operator/query/) are supported for more advanced metadata filtering. The current list of supported operators are as follows:\n",
"- `$eq` (equals)\n",
"- `$neq` (not equals)\n",
"- `$gt` (greater than)\n",
"- `$lt` (less than)\n",
"- `$gte` (greater than or equal)\n",
"- `$lte` (less than or equal)\n",
"- `$in` (membership in list)\n",
"- `$nin` (not in list)\n",
"- `$and` (all conditions must match)\n",
"- `$or` (any condition must match)\n",
"- `$not` (negation of condition)\n",
"\n",
"Performing the same above similarity search with advanced metadata filtering can be done as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b3dd99d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]\n",
"* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search(\n",
" \"LangChain provides abstractions to make working with LLMs easy\",\n",
" k=2,\n",
" filter={\"source\": {\"$eq\": \"tweet\"}},\n",
")\n",
"for res in results:\n",
" print(f\"* {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "5ae35069",

View File

@ -397,6 +397,49 @@
" print(f\"Content: {doc.page_content}, Metadata: {doc.metadata}\")"
]
},
{
"cell_type": "markdown",
"id": "8dead085",
"metadata": {},
"source": [
"Some [MongoDB query and projection operators](https://www.mongodb.com/docs/manual/reference/operator/query/) are supported for more advanced metadata filtering. The current list of supported operators are as follows:\n",
"- `$eq` (equals)\n",
"- `$neq` (not equals)\n",
"- `$gt` (greater than)\n",
"- `$lt` (less than)\n",
"- `$gte` (greater than or equal)\n",
"- `$lte` (less than or equal)\n",
"- `$in` (membership in list)\n",
"- `$nin` (not in list)\n",
"- `$and` (all conditions must match)\n",
"- `$or` (any condition must match)\n",
"- `$not` (negation of condition)\n",
"\n",
"Performing the same above similarity search with advanced metadata filtering can be done as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af47c6f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Content: foo, Metadata: {'page': 1}\n"
]
}
],
"source": [
"results = await db.asimilarity_search(\n",
" \"foo\", filter={\"page\": {\"$eq\": 1}}, k=1, fetch_k=4\n",
")\n",
"for doc in results:\n",
" print(f\"Content: {doc.page_content}, Metadata: {doc.metadata}\")"
]
},
{
"cell_type": "markdown",
"id": "1becca53",