以编程方式使用文档

Couchbase 是一个分布式 NoSQL 数据库,用于跨云、移动和边缘部署的运营工作负载。它支持需要相似性搜索以及键值和 JSON 文档访问的应用程序的向量搜索。

Couchbase 为 LangChain 提供了两种不同的向量存储实现:

向量存储索引类型最低版本适用场景
CouchbaseQueryVectorStore超大规模向量索引 or 组合向量索引Couchbase Server 8.0+大规模纯向量搜索或结合向量相似性与标量过滤器的搜索
CouchbaseSearchVectorStore搜索向量索引Couchbase Server 7.6+结合向量相似性与全文搜索 (FTS) 和地理空间搜索的混合搜索

本教程说明如何在 Couchbase 中使用向量搜索。您可以使用 Couchbase Capella 或自托管的 Couchbase Server。

设置

要访问 Couchbase 向量存储,您首先需要安装 langchain-couchbase 合作伙伴包:

pip install langchain-couchbase langchain-openai

凭据

访问 Couchbase 网站 并创建新连接,确保保存您的数据库用户名和密码。

您还需要 OpenAI API 密钥用于嵌入。请从 OpenAI.

COUCHBASE_CONNECTION_STRING = getpass.getpass(
    "Enter the connection string for the Couchbase cluster: "
)
DB_USERNAME = getpass.getpass("Enter the username for the Couchbase cluster: ")
DB_PASSWORD = getpass.getpass("Enter the password for the Couchbase cluster: ")
OPENAI_API_KEY = getpass.getpass("Enter your OpenAI API key: ")

os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
Enter the connection string for the Couchbase cluster:  ········
Enter the username for the Couchbase cluster:  ········
Enter the password for the Couchbase cluster:  ········
Enter your OpenAI API key:  ········

如果您想获得最佳级别的模型调用自动追踪,您还可以设置您的 LangSmith API 密钥,请取消下方注释:

os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

创建 Couchbase 连接对象

我们首先创建到 Couchbase 集群的连接,然后将集群对象传递给向量存储。

在这里,我们使用上述用户名和密码进行连接。您也可以使用其他任何支持的方式连接到您的集群。

有关连接到 Couchbase 集群的更多信息,请参阅 文档.

from datetime import timedelta

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions

auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD)
options = ClusterOptions(auth)
options.apply_profile("wan_development")
cluster = Cluster(COUCHBASE_CONNECTION_STRING, options)

# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))

我们现在将设置要在向量搜索中使用的 Couchbase 集群中的 bucket、scope 和 collection 名称。

对于此示例,我们使用默认的 scope 和 collections。

BUCKET_NAME = "langchain_bucket"
SCOPE_NAME = "_default"
COLLECTION_NAME = "_default"

CouchbaseQueryVectorStore

CouchbaseQueryVectorStore 支持使用查询和索引服务将 Couchbase 用于向量搜索。它支持两种不同类型的向量索引:

  • 超大规模向量索引 - 针对大规模数据集(数十亿文档)的纯向量搜索进行了优化。最适合内容发现、推荐以及需要高准确性且内存占用低的应用程序。超大规模向量索引同时比较向量和标量值。
  • 组合向量索引 - 将全局二级索引 (GSI) 与向量列相结合。非常适合结合向量相似性与标量过滤器的搜索,其中标量会过滤掉大部分数据集。组合向量索引首先应用标量过滤器,然后对过滤后的结果执行向量搜索。

有关选择正确索引类型的指导,请参阅 选择正确的向量索引.

Requirements: Couchbase Server 8.0 及以上版本。

有关索引的更多信息,请参阅:

初始化

下面,我们使用集群信息和距离度量创建向量存储对象。

首先,设置嵌入(如果尚未完成):

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large")

然后创建向量存储:

from langchain_couchbase import CouchbaseQueryVectorStore
from langchain_couchbase.vectorstores import DistanceStrategy

vector_store = CouchbaseQueryVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=embeddings,
    distance_metric=DistanceStrategy.DOT,
)

距离策略

CouchbaseQueryVectorStore 通过以下方式支持以下距离策略 DistanceStrategy enum:

策略描述
DistanceStrategy.DOT点积相似度
DistanceStrategy.COSINE余弦相似度
DistanceStrategy.EUCLIDEAN欧氏距离(等同于 L2)
DistanceStrategy.EUCLIDEAN_SQUARED平方欧氏距离(等同于 L2_SQUARED)

指定文本和嵌入字段

您可以使用以下方式选择性地指定文档的文本和嵌入字段 text_keyembedding_key fields.

vector_store_specific = CouchbaseQueryVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=embeddings,
    distance_metric=DistanceStrategy.COSINE,
    text_key="text",
    embedding_key="embedding",
)

管理向量存储

创建向量存储后,我们可以通过添加和删除不同项目来与其交互。

向向量存储添加项目

我们可以使用以下方式向向量存储添加项目 add_documents function.

from uuid import uuid4

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="thud", metadata={"bar": "baz"})
document_3 = Document(page_content="i will be deleted :(")

documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)

创建向量索引

Important: 必须创建向量索引 **之后** 将文档添加到向量存储后使用 create_index() 方法可在添加文档后实现高效的向量搜索。

from langchain_couchbase.vectorstores import IndexType

# Create a Hyperscale Vector Index
vector_store.create_index(
    index_type=IndexType.HYPERSCALE,
    index_description="IVF,SQ8",
)

或创建复合向量索引:

# Create a Composite Vector Index
vector_store.create_index(
    index_type=IndexType.COMPOSITE,
    index_description="IVF,SQ8",
)

从向量存储中删除项目

vector_store.delete(ids=["3"])

查询向量存储

相似性搜索

执行简单相似性搜索可以按以下方式完成:

results = vector_store.similarity_search(query="thud", k=1)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
* thud [{'bar': 'baz'}]

带过滤器的相似性搜索

您可以使用 SQL++ WHERE 子句进行结果过滤 where_str parameter:

results = vector_store.similarity_search(
    query="thud", k=1, where_str="metadata.bar = 'baz'"
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
* thud [{'bar': 'baz'}]

带评分的相似性搜索

您可以通过调用获取结果的距 similarity_search_with_score 方法。较低的距离值表示更相似的文档。

results = vector_store.similarity_search_with_score(query="qux", k=1)
for doc, score in results:
    print(f"* [DIST={score:3f}] {doc.page_content} [{doc.metadata}]")
* [DIST=-0.500724] foo [{'baz': 'bar'}]

异步操作

CouchbaseQueryVectorStore 支持异步操作:

# add documents
await vector_store.aadd_documents(documents=documents, ids=ids)

# delete documents
await vector_store.adelete(ids=["3"])

# search
results = await vector_store.asimilarity_search(query="thud", k=1)

# search with score
results = await vector_store.asimilarity_search_with_score(query="qux", k=1)
for doc, score in results:
    print(f"* [DIST={score:3f}] {doc.page_content} [{doc.metadata}]")
* [DIST=-0.500724] foo [{'baz': 'bar'}]

用作检索器

您可以将向量存储转换为检索器:

retriever = vector_store.as_retriever(
    search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
[Document(id='2', metadata={'bar': 'baz'}, page_content='thud')]

从文本创建

您可以直接从 CouchbaseQueryVectorStore 文本列表创建:

texts = ["hello", "world"]

vectorstore = CouchbaseQueryVectorStore.from_texts(
    texts,
    embedding=embeddings,
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    distance_metric=DistanceStrategy.COSINE,
)

CouchbaseSearchVectorStore

CouchbaseSearchVectorStore 支持使用 搜索向量索引Search Vector Indexes 将 Couchbase Search 索引与向量列结合,允许将向量搜索与全文搜索 (FTS) 和地理空间搜索相结合的混合搜索。

Requirements: Couchbase Server 7.6 及以上版本。

有关如何创建支持 Vector 字段的 Search 索引的详细信息,请参阅文档:

本教程的 Search 索引字段映射

要跟随本文档中的示例,您的 Search 索引应包含以下字段的映射:

字段类型描述
texttext文档文本内容
embeddingvector向量嵌入字段(维度:3072 用于 text-embedding-3-large)
metadataobject(子映射)包含子字段的元数据对象,如 source, author, rating, date

Notes:

  • - 向量字段维度必须与您的嵌入模型相匹配(3072 用于 text-embedding-3-large 本教程中使用)
  • - 元数据子字段(source, author, rating, date)是混合查询示例所需的
  • - 您可以使用 text_keyembedding_key 参数在初始化向量存储时自定义字段名称

初始化

下面,我们使用集群信息和搜索索引名称创建向量存储对象。

首先,设置嵌入:

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large")

然后创建向量存储:

from langchain_couchbase import CouchbaseSearchVectorStore

SEARCH_INDEX_NAME = "langchain-test-index"

vector_store = CouchbaseSearchVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=embeddings,
    index_name=SEARCH_INDEX_NAME,
)

指定文本和嵌入字段

您可以使用以下方式选择指定文档的文本和嵌入字段 text_keyembedding_key fields.

vector_store_specific = CouchbaseSearchVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=embeddings,
    index_name=SEARCH_INDEX_NAME,
    text_key="text",
    embedding_key="embedding",
)

管理向量存储

创建向量存储后,我们可以通过添加和删除不同的项目来与其交互。

向向量存储添加项目

我们可以使用 add_documents function.

from uuid import uuid4

from langchain_core.documents import Document

document_1 = Document(
    page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
    metadata={"source": "tweet"},
)

document_2 = Document(
    page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
    metadata={"source": "news"},
)

document_3 = Document(
    page_content="Building an exciting new project with LangChain - come check it out!",
    metadata={"source": "tweet"},
)

document_4 = Document(
    page_content="Robbers broke into the city bank and stole $1 million in cash.",
    metadata={"source": "news"},
)

document_5 = Document(
    page_content="Wow! That was an amazing movie. I can't wait to see it again.",
    metadata={"source": "tweet"},
)

document_6 = Document(
    page_content="Is the new iPhone worth the price? Read this review to find out.",
    metadata={"source": "website"},
)

document_7 = Document(
    page_content="The top 10 soccer players in the world right now.",
    metadata={"source": "website"},
)

document_8 = Document(
    page_content="LangGraph is the best framework for building stateful, agentic applications!",
    metadata={"source": "tweet"},
)

document_9 = Document(
    page_content="The stock market is down 500 points today due to fears of a recession.",
    metadata={"source": "news"},
)

document_10 = Document(
    page_content="I have a bad feeling I am going to get deleted :(",
    metadata={"source": "tweet"},
)

documents = [
    document_1,
    document_2,
    document_3,
    document_4,
    document_5,
    document_6,
    document_7,
    document_8,
    document_9,
    document_10,
]
uuids = [str(uuid4()) for _ in range(len(documents))]

vector_store.add_documents(documents=documents, ids=uuids)
['f125b836-f555-4449-98dc-cbda4e77ae3f',
 'a28fccde-fd32-4775-9ca8-6cdb22ca7031',
 'b1037c4b-947f-497f-84db-63a4def5080b',
 'c7082b74-b385-4c4b-bbe5-0740909c01db',
 'a7e31f62-13a5-4109-b881-8631aff7d46c',
 '9fcc2894-fdb1-41bd-9a93-8547747650f4',
 'a5b0632d-abaf-4802-99b3-df6b6c99be29',
 '0475592e-4b7f-425d-91fd-ac2459d48a36',
 '94c6db4e-ba07-43ff-aa96-3a5d577db43a',
 'd21c7feb-ad47-4e7d-84c5-785afb189160']

从向量存储删除项目

vector_store.delete(ids=[uuids[-1]])
True

查询向量存储

创建向量存储并添加相关文档后,您很可能希望在运行链或代理时对其进行查询。

相似性搜索

执行简单的相似性搜索可以按如下方式完成:

results = vector_store.similarity_search(
    "LangChain provides abstractions to make working with LLMs easy",
    k=2,
)
for res in results:
    print(f"* {res.page_content} [{res.metadata}]")
* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]
* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]

带评分的相似性搜索

您还可以通过调用以下方式获取结果的评分 similarity_search_with_score method.

results = vector_store.similarity_search_with_score("Will it be hot tomorrow?", k=1)
for res, score in results:
    print(f"* [SIM={score:3f}] {res.page_content} [{res.metadata}]")
* [SIM=0.553213] The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees. [{'source': 'news'}]

过滤结果

您可以通过指定 Couchbase Search 服务支持的文档中文本或元数据上的任何过滤器来过滤搜索结果。

filter 可以是任何有效的 SearchQuery Couchbase Python SDK 支持。在执行向量搜索之前应用这些过滤器。

如果要过滤元数据中的某个字段,需要使用 .

例如,要获取 source 元数据中的字段,需要指定 metadata.source.

请注意,筛选器需要被搜索索引支持。

from couchbase import search

query = "Are there any concerning financial news?"
filter_on_source = search.MatchQuery("news", field="metadata.source")
results = vector_store.similarity_search_with_score(
    query, fields=["metadata.source"], filter=filter_on_source, k=5
)
for res, score in results:
    print(f"* {res.page_content} [{res.metadata}] {score}")
* The stock market is down 500 points today due to fears of a recession. [{'source': 'news'}] 0.38733142614364624
* Robbers broke into the city bank and stole $1 million in cash. [{'source': 'news'}] 0.20637883245944977
* The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees. [{'source': 'news'}] 0.10403035581111908

指定要返回的字段

您可以使用以下方式指定要从文档中返回的字段 fields 参数在搜索中。这些字段作为以下内容的一部分返回 metadata 返回文档中的对象。您可以获取搜索索引中存储的任何字段。文档的 text_key 作为文档的一部分返回 page_content.

如果您未指定要获取的任何字段,则返回索引中存储的所有字段。

如果您想获取元数据中的某个字段,需要使用以下方式指定 .

例如,要获取 source 元数据中的字段,需要指定 metadata.source.

query = "What did I eat for breakfast today?"
results = vector_store.similarity_search(query, fields=["metadata.source"])
print(results[0])
page_content='I had chocolate chip pancakes and scrambled eggs for breakfast this morning.' metadata={'source': 'tweet'}

通过转换为检索器进行查询

您还可以将向量存储转换为检索器,以便在链中更方便地使用。

以下是将向量存储转换为检索器,然后使用简单查询和筛选器调用检索器的方法。

retriever = vector_store.as_retriever(
    search_type="similarity",
    search_kwargs={"k": 1, "score_threshold": 0.5},
)
filter_on_source = search.MatchQuery("news", field="metadata.source")
retriever.invoke("Stealing from the bank is a crime", filter=filter_on_source)
[Document(id='b480c9c6-b7df-4a22-ac2e-19287af7562d', metadata={'source': 'news'}, page_content='Robbers broke into the city bank and stole $1 million in cash.')]

混合查询

Couchbase 允许您通过将向量搜索结果与文档的非向量字段(如 metadata object.

上的搜索相结合来进行混合搜索。结果将基于向量搜索和搜索服务支持的搜索的组合结果。每个分量搜索的分数相加得到结果的总分。

要执行混合搜索,有一个可选参数 search_options 可以传递给所有相似性搜索。 You can find the different search/query possibilities for the search_optionsCouchbase 搜索请求参数文档中.

为混合搜索创建多样化元数据

为了演示混合搜索,让我们创建具有多样化元数据的文档。我们向元数据添加三个字段: date 介于 2010 年和 2020 年之间, rating 介于 1 到 5 之间,以及 author 设置为 John Doe 或 Jane Doe。

from langchain_core.documents import Document

# Create documents with diverse metadata for hybrid search examples
hybrid_docs = [
    Document(
        page_content="The new AI model shows impressive performance on benchmark tests.",
        metadata={"source": "tech", "date": "2019-01-01", "rating": 5, "author": "John Doe"},
    ),
    Document(
        page_content="Stock markets showed mixed results today with tech sector leading gains.",
        metadata={"source": "finance", "date": "2017-01-01", "rating": 3, "author": "Jane Doe"},
    ),
    Document(
        page_content="The annual developer conference announced new framework updates.",
        metadata={"source": "tech", "date": "2018-01-01", "rating": 4, "author": "John Doe"},
    ),
    Document(
        page_content="Weather patterns indicate a mild winter ahead for the region.",
        metadata={"source": "weather", "date": "2016-01-01", "rating": 2, "author": "Jane Doe"},
    ),
    Document(
        page_content="The new smartphone release features advanced camera technology.",
        metadata={"source": "tech", "date": "2020-01-01", "rating": 4, "author": "John Doe"},
    ),
    Document(
        page_content="Economic indicators suggest steady growth in the coming quarter.",
        metadata={"source": "finance", "date": "2017-01-01", "rating": 3, "author": "Jane Doe"},
    ),
]

vector_store.add_documents(hybrid_docs)

query = "Tell me about technology news"
results = vector_store.similarity_search(query)
print(results[0].metadata)
{'author': 'John Doe', 'date': '2020-01-01', 'rating': 4, 'source': 'tech'}

按精确值查询

我们可以对 metadata object.

query = "What are the latest technology updates?"
results = vector_store.similarity_search(
    query,
    search_options={"query": {"field": "metadata.author", "match": "John Doe"}},
    fields=["metadata.author"],
)
print(results[0])
page_content='The new smartphone release features advanced camera technology.' metadata={'author': 'John Doe'}

中的文本字段(如作者)进行精确匹配搜索

我们可以指定模糊度来搜索部分匹配项。当您想搜索查询的轻微变体或拼写错误时,这很有用。

在这里,"Jae" 与 "Jane" 接近(模糊度为 1)。

query = "What are the financial market updates?"
results = vector_store.similarity_search(
    query,
    search_options={
        "query": {"field": "metadata.author", "match": "Jae", "fuzziness": 1}
    },
    fields=["metadata.author"],
)
print(results[0])
page_content='Stock markets showed mixed results today with tech sector leading gains.' metadata={'author': 'Jane Doe'}

按日期范围查询

我们可以搜索日期字段在日期范围内的文档,如 metadata.date.

query = "What happened in the markets?"
results = vector_store.similarity_search(
    query,
    search_options={
        "query": {
            "start": "2016-12-31",
            "end": "2018-01-02",
            "inclusive_start": True,
            "inclusive_end": False,
            "field": "metadata.date",
        }
    },
)
print(results[0])
page_content='Stock markets showed mixed results today with tech sector leading gains.' metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': 'finance'}

按数字范围查询

我们可以搜索数字字段在一定范围内的文档,如 metadata.rating.

query = "What are the economic indicators for the coming quarter?"
results = vector_store.similarity_search_with_score(
    query,
    search_options={
        "query": {
            "min": 4,
            "max": 5,
            "inclusive_min": True,
            "inclusive_max": True,
            "field": "metadata.rating",
        }
    },
)
print(results[0])
(Document(id='6aeb8413bce340bc893f175cefbb64b3', metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': 'finance'}, page_content='Economic indicators suggest steady growth in the coming quarter.'), 0.7944117188453674)

组合多个搜索查询

可以使用 AND(合取)或 OR(析取)运算符组合不同的搜索查询。

在此示例中,我们检查评分在 3 到 4 之间且日期为 2017 年的文档。

query = "Tell me about finance"
results = vector_store.similarity_search_with_score(
    query,
    search_options={
        "query": {
            "conjuncts": [
                {"min": 3, "max": 4, "inclusive_max": True, "field": "metadata.rating"},
                {"start": "2016-12-31", "end": "2018-01-01", "field": "metadata.date"},
            ]
        }
    },
)
print(results[0])
(Document(id='0c9af73370c1483caddf9941440edb50', metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': 'finance'}, page_content='Stock markets showed mixed results today with tech sector leading gains.'), 0.7275013146103568)

注意

混合搜索结果可能包含不满足所有搜索参数的文档。这是因为 分数的计算方式. 分数是向量搜索分数和混合搜索查询分数的总和。如果向量搜索分数较高,则组合分数将高于匹配混合搜索中所有查询的结果。 为避免此类结果,请使用 filter 参数代替混合搜索。

将混合搜索查询与过滤器结合使用

混合搜索可以与过滤器结合使用,以充分发挥混合搜索和过滤器的优势,获得符合要求的结果。

在这个示例中,我们正在检查评分在3到5之间且文本字段中包含字符串"market"的文档。

filter_text = search.MatchQuery("market", field="text")

query = "Tell me about market updates"
results = vector_store.similarity_search_with_score(
    query,
    search_options={
        "query": {
            "min": 3,
            "max": 5,
            "inclusive_min": True,
            "inclusive_max": True,
            "field": "metadata.rating",
        }
    },
    filter=filter_text,
)

print(results[0])
(Document(id='0c9af73370c1483caddf9941440edb50', metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': 'finance'}, page_content='Stock markets showed mixed results today with tech sector leading gains.'), 0.4503188681265006)

其他查询

同样,您可以在 search_options 参数中使用任何支持的查询方法,如地理距离、多边形搜索、通配符、正则表达式等。请参阅文档以了解更多关于可用查询方法及其语法的详细信息。

用于检索增强生成

有关如何使用这些向量存储进行检索增强生成(RAG)的指南,请参阅以下章节:

常见问题

问题:我应该在创建 CouchbaseSearchVectorStore 对象之前创建搜索索引吗?

是的,您需要在创建 CouchbaseSearchVectorStore object.

问题:我应该在添加文档之前还是之后创建 CouchbaseQueryVectorStore 的索引?

对于 CouchbaseQueryVectorStore,您应该在 **之后** 创建索引 create_index() 方法添加文档。 CouchbaseSearchVectorStore.

问题:CouchbaseSearchVectorStore 和 CouchbaseQueryVectorStore 之间有什么区别?

功能CouchbaseSearchVectorStoreCouchbaseQueryVectorStore
最低版本Couchbase Server 7.6+Couchbase Server 8.0+
索引类型搜索向量索引超规模或复合向量索引
索引创建在向量存储创建之前在添加文档之后
过滤SearchQuery 对象SQL++ WHERE 子句(where_str)
适用场景混合搜索(向量 + FTS + 地理)大规模纯向量搜索或向量 + 标量过滤器

问题:我没有看到搜索结果中我指定的所有字段

在 Couchbase 中,我们只能返回存储在搜索索引中的字段。请确保您在搜索结果中尝试访问的字段是搜索索引的一部分。

一种处理方法是动态地在索引中索引和存储文档的字段。

  • - 在 Capella 中,您需要进入"高级模式",然后在"常规设置"标题下可以勾选"[X] 存储动态字段"或"[X] 索引动态字段"
  • - 在 Couchbase Server 中,在索引编辑器(非快速编辑器)中,在"高级"标题下可以勾选"[X] 存储动态字段"或"[X] 索引动态字段"

请注意,这些选项会增加索引的大小。

有关动态映射的更多详细信息,请参阅 文档.

问题:我无法在搜索结果中看到元数据对象

这很可能是由于 metadata field in the document not being indexed and/or stored by the Couchbase Search index. In order to index the metadata 文档中的字段,您需要将其作为子映射添加到索引中。

如果您选择映射映射中的所有字段,您将能够搜索所有元数据字段。或者,为了优化索引,您可以选择内部特定字段 metadata 对象被索引。您可以参考 文档 来了解更多关于索引子映射的信息。

创建子映射

问题:过滤器和搜索之间有什么区别_options / hybrid queries?

过滤器是 pre-filters 用于限制在搜索索引中搜索的文档。它在 Couchbase Server 7.6.4 及更高版本中可用。

混合查询是额外的搜索查询,可用于调整从搜索索引返回的结果。

过滤器和混合搜索查询具有相同的功能,但语法略有不同。过滤器是 SearchQuery 对象,而混合搜索查询是 字典.

API 参考

有关所有功能和配置的详细文档: