-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Issue with search dialect 3 and JSON (resolves #140) #151
Open
bsbodden
wants to merge
1
commit into
main
Choose a base branch
from
bsb/issue-140
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import json | ||
|
||
import pytest | ||
from redis import Redis | ||
from redis.commands.search.query import Query | ||
|
||
from redisvl.index import SearchIndex | ||
from redisvl.query import FilterQuery, VectorQuery | ||
from redisvl.query.filter import Tag | ||
from redisvl.schema.schema import IndexSchema | ||
|
||
|
||
@pytest.fixture | ||
def sample_data(): | ||
return [ | ||
{ | ||
"name": "Noise-cancelling Bluetooth headphones", | ||
"description": "Wireless Bluetooth headphones with noise-cancelling technology", | ||
"connection": {"wireless": True, "type": "Bluetooth"}, | ||
"price": 99.98, | ||
"stock": 25, | ||
"colors": ["black", "silver"], | ||
"embedding": [0.87, -0.15, 0.55, 0.03], | ||
"embeddings": [[0.56, -0.34, 0.69, 0.02], [0.94, -0.23, 0.45, 0.19]], | ||
}, | ||
{ | ||
"name": "Wireless earbuds", | ||
"description": "Wireless Bluetooth in-ear headphones", | ||
"connection": {"wireless": True, "type": "Bluetooth"}, | ||
"price": 64.99, | ||
"stock": 17, | ||
"colors": ["red", "black", "white"], | ||
"embedding": [-0.7, -0.51, 0.88, 0.14], | ||
"embeddings": [[0.54, -0.14, 0.79, 0.92], [0.94, -0.93, 0.45, 0.16]], | ||
}, | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def schema_dict(): | ||
return { | ||
"index": {"name": "products", "prefix": "product", "storage_type": "json"}, | ||
"fields": [ | ||
{"name": "name", "type": "text"}, | ||
{"name": "description", "type": "text"}, | ||
{"name": "connection_type", "path": "$.connection.type", "type": "tag"}, | ||
{"name": "price", "type": "numeric"}, | ||
{"name": "stock", "type": "numeric"}, | ||
{"name": "color", "path": "$.colors.*", "type": "tag"}, | ||
{ | ||
"name": "embedding", | ||
"type": "vector", | ||
"attrs": {"dims": 4, "algorithm": "flat", "distance_metric": "cosine"}, | ||
}, | ||
{ | ||
"name": "embeddings", | ||
"path": "$.embeddings[*]", | ||
"type": "vector", | ||
"attrs": {"dims": 4, "algorithm": "hnsw", "distance_metric": "l2"}, | ||
}, | ||
], | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def index(sample_data, redis_url, schema_dict): | ||
index_schema = IndexSchema.from_dict(schema_dict) | ||
redis_client = Redis.from_url(redis_url) | ||
index = SearchIndex(index_schema, redis_client) | ||
index.create(overwrite=True, drop=True) | ||
index.load(sample_data) | ||
yield index | ||
index.delete(drop=True) | ||
|
||
|
||
def test_dialect_3_json(index, sample_data): | ||
# Create a VectorQuery with dialect 3 | ||
vector_query = VectorQuery( | ||
vector=[0.23, 0.12, -0.03, 0.98], | ||
vector_field_name="embedding", | ||
return_fields=["name", "description", "price"], | ||
dialect=3, | ||
) | ||
|
||
# Execute the query | ||
results = index.query(vector_query) | ||
|
||
# Print the results | ||
print("VectorQuery Results:") | ||
print(results) | ||
|
||
# Assert the expected format of the results | ||
assert len(results) > 0 | ||
for result in results: | ||
assert not isinstance(result["name"], list) | ||
assert not isinstance(result["description"], list) | ||
assert not isinstance(result["price"], (list, str)) | ||
|
||
# Create a FilterQuery with dialect 3 | ||
filter_query = FilterQuery( | ||
filter_expression=Tag("color") == "black", | ||
return_fields=["name", "description", "price"], | ||
dialect=3, | ||
) | ||
|
||
# Execute the query | ||
results = index.query(filter_query) | ||
|
||
# Print the results | ||
print("FilterQuery Results:") | ||
print(results) | ||
|
||
# Assert the expected format of the results | ||
assert len(results) > 0 | ||
for result in results: | ||
assert not isinstance(result["name"], list) | ||
assert not isinstance(result["description"], list) | ||
assert not isinstance(result["price"], (list, str)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this is the desired behavior from @tylerhutcherson but I feel like I'd rather have a list than a comma-separated string in this situation. I may be missing some deeper context though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tylerhutcherson what do you think?