0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · レッスン

メタデータによるフィルタリング

メタデータを利用して類似度検索を絞り込み、クエリにコンテキスト上の制約を追加します。

「メタデータによるフィルタリング」はCoddyKit上の無料Vector Databases: Pinecone, Weaviate & pgvectorレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはVector Databases: Pinecone, Weaviate & pgvector学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Filter Your Vector Searches

Imagine searching for products, but only wanting "electronics" that are "under $50". This is where metadata filtering comes in handy!

Vector databases let you store extra information, called metadata, alongside your vectors. This lesson teaches you how to use this metadata to refine your similarity searches in Pinecone.

Understanding Metadata

Metadata is simply "data about data." In Pinecone, it's a set of key-value pairs attached to each vector.

  • Key: A string representing a property (e.g., "category", "price", "author").
  • Value: Can be a string, number, boolean, or even a list of strings/numbers.

It helps describe the item your vector represents.

Why Filter Searches?

Filtering is crucial for getting more precise and relevant search results.

  • Precision: Narrow down results to only what's relevant (e.g., "red shoes").
  • Context: Add specific conditions beyond just vector similarity (e.g., "articles published last year").
  • Efficiency: Reduce the number of vectors considered, potentially speeding up searches for large datasets.

Adding Metadata to Vectors

When you add (or "upsert") vectors into Pinecone, you can include a metadata dictionary. This makes the extra data searchable later.

You learned about upserting in a previous lesson. Here's a quick reminder of how metadata is attached:

from pinecone import Pinecone, Index
import os

# Assume Pinecone is initialized (replace with your actual setup)
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

# Example vector with metadata
vectors_to_upsert = [
    {
        "id": "item1",
        "values": [0.1, 0.2, 0.3], # Placeholder vector
        "metadata": {"genre": "fiction", "year": 2023, "price": 19.99}
    },
    {
        "id": "item2",
        "values": [0.4, 0.5, 0.6],
        "metadata": {"genre": "non-fiction", "year": 2022, "price": 25.50}
    }
]

# index.upsert(vectors=vectors_to_upsert) # Uncomment to actually upsert
print("Metadata structure for upserting shown.")

Filtering with Exact Matches

The simplest way to filter is to look for exact matches on a metadata field. You specify the field name and its desired value.

For example, to find all items with "genre": "fiction", you'd use {"genre": "fiction"} in your query's filter parameter.

from pinecone import Pinecone, Index
import os

# Assume index is already set up and has data
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

# Query for items where genre is exactly 'fiction'
query_vector = [0.1, 0.2, 0.3] # Your query embedding

# results = index.query(
#     vector=query_vector,
#     top_k=3,
#     filter={"genre": "fiction"}
# )

print("Querying with filter: {'genre': 'fiction'}")
# print(results) # Uncomment to see results

Filtering by Ranges

You can also filter by numerical ranges using special Pinecone operators. These are useful for values like prices, dates, or ratings.

  • $gt: greater than
  • $gte: greater than or equal to
  • $lt: less than
  • $lte: less than or equal to

For example, {"price": {"$lt": 20.0}} finds items cheaper than $20.

from pinecone import Pinecone, Index
import os

# Assume index with 'price' metadata
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

query_vector = [0.1, 0.2, 0.3]

# Find items published after 2022
# results = index.query(
#     vector=query_vector,
#     top_k=5,
#     filter={"year": {"$gt": 2022}}
# )

print("Querying with filter: {'year': {'$gt': 2022}}")
# print(results) # Uncomment to see results

Filtering with Lists

Sometimes you need to filter based on whether a value is present (or not present) in a list of options. Pinecone provides $in and $nin operators for this.

  • $in: value is one of the specified options.
  • $nin: value is NOT one of the specified options.

Example: {"category": {"$in": ["electronics", "clothing"]}}

from pinecone import Pinecone, Index
import os

# Assume index with 'tag' metadata
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

query_vector = [0.1, 0.2, 0.3]

# Find items belonging to 'fiction' OR 'poetry'
# results = index.query(
#     vector=query_vector,
#     top_k=5,
#     filter={"genre": {"$in": ["fiction", "poetry"]}}
# )

print("Querying with filter: {'genre': {'$in': ['fiction', 'poetry']}}")
# print(results) # Uncomment to see results

Combining Filter Conditions

You can combine multiple filter conditions to create complex queries. By default, multiple conditions at the same level are treated as an AND operation.

For explicit OR operations, you use the $or operator. For example, to find items with category="books" AND price < 20:

from pinecone import Pinecone, Index
import os

# Assume index with 'category' and 'price' metadata
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

query_vector = [0.1, 0.2, 0.3]

# Find books cheaper than $20 (AND operation)
# results = index.query(
#     vector=query_vector,
#     top_k=5,
#     filter={"category": "books", "price": {"$lt": 20.0}}
# )

print("Querying with filter: {'category': 'books', 'price': {'$lt': 20.0}}")
# print(results) # Uncomment to see results

Using $or for Flexible Filters

To perform an OR search, you use the $or operator. This operator takes a list of filter conditions, and if any of them are true, the item is included.

Example: Find items where category="electronics" OR category="home goods":

from pinecone import Pinecone, Index
import os

# Assume index with 'category' metadata
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

query_vector = [0.1, 0.2, 0.3]

# Find items where category is 'electronics' OR 'home goods'
# results = index.query(
#     vector=query_vector,
#     top_k=5,
#     filter={
#         "$or": [
#             {"category": "electronics"},
#             {"category": "home goods"}
#         ]
#     }
# )

print("Querying with $or filter...")
# print(results) # Uncomment to see results

Advanced Combined Filters

You can combine $and (implicit or explicit) and $or operators for very powerful and specific filtering. This allows you to build complex search logic.

For example, to find items that are "fiction" AND ("published after 2020" OR "price under $15"):

from pinecone import Pinecone, Index
import os

# Assume index with 'genre', 'year', 'price' metadata
# pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# index = pc.Index("my-index")

query_vector = [0.1, 0.2, 0.3]

# Complex filter: fiction AND (year > 2020 OR price < 15)
# results = index.query(
#     vector=query_vector,
#     top_k=5,
#     filter={
#         "genre": "fiction",
#         "$or": [
#             {"year": {"$gt": 2020}},
#             {"price": {"$lt": 15.0}}
#         ]
#     }
# )

print("Querying with complex AND/OR filter...")
# print(results) # Uncomment to see results

Test Your Filtering Knowledge

Which Pinecone filter would you use to find items that are either in the "books" category or have a "rating" of at least 4.5?

Recap: Mastering Metadata Filters

Great job! You've learned how to use metadata to significantly enhance your similarity searches in Pinecone.

  • Metadata: Key-value pairs stored with your vectors.
  • Filter Types: Equality, range ($gt, $lt, etc.), and list ($in, $nin).
  • Combining Filters: Use implicit AND or explicit $or for complex logic.

Next, we'll explore how to organize your data even further using Pinecone's namespaces.

よくある質問

「メタデータによるフィルタリング」レッスンは無料ですか?

はい。「メタデータによるフィルタリング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Vector Databases: Pinecone, Weaviate & pgvectorコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。

「メタデータによるフィルタリング」で何を学びますか?

メタデータを利用して類似度検索を絞り込み、クエリにコンテキスト上の制約を追加します。 ブラウザで直接実行するハンズオンコードでVector Databases: Pinecone, Weaviate & pgvectorを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Vector Databases: Pinecone, Weaviate & pgvectorを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのVector Databases: Pinecone, Weaviate & pgvectorは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「メタデータによるフィルタリング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このVector Databases: Pinecone, Weaviate & pgvectorレッスンでコードを書いて実行できますか?

はい。すべてのVector Databases: Pinecone, Weaviate & pgvectorレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. メタデータによるフィルタリング
  2. 名前空間の管理
  3. リアルタイム更新と削除
  4. 疎ベクトルと密ベクトルによるハイブリッド検索
← Vector Databases: Pinecone, Weaviate & pgvectorに戻る