Skip to content

Commit

Permalink
feat: search_field should be optional in hybrid text search (docarray…
Browse files Browse the repository at this point in the history
…#1516)

Signed-off-by: AnneY <evangeline-lun@foxmail.com>
  • Loading branch information
AnneYang720 authored May 10, 2023
1 parent bc7f725 commit 096f644
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
15 changes: 11 additions & 4 deletions docarray/index/backends/weaviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,18 +891,23 @@ def filter_batched(self, filters) -> Any:

return self

def text_search(self, query, search_field) -> Any:
def text_search(self, query: str, search_field: Optional[str] = None) -> Any:

"""Find documents in the index based on a text search query
:param query: The text to search for
:param search_field: name of the field to search on
:return: self
"""
bm25 = {"query": query, "properties": [search_field]}
bm25: Dict[str, Any] = {"query": query}
if search_field:
bm25["properties"] = [search_field]
self._queries[0] = self._queries[0].with_bm25(**bm25)
return self

def text_search_batched(self, queries, search_field) -> Any:
def text_search_batched(
self, queries: Sequence[str], search_field: Optional[str] = None
) -> Any:
"""Find documents in the index based on a text search query
:param queries: The texts to search for
Expand All @@ -915,7 +920,9 @@ def text_search_batched(self, queries, search_field) -> Any:
new_queries = []

for query, clause in zip(adj_queries, adj_clauses):
bm25 = {"query": clause, "properties": [search_field]}
bm25 = {"query": clause}
if search_field:
bm25["properties"] = [search_field]
new_queries.append(query.with_bm25(**bm25))

self._queries = new_queries
Expand Down
11 changes: 2 additions & 9 deletions docs/user_guide/storing/index_weaviate.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ from docarray import BaseDoc
from docarray.typing import NdArray
from docarray.index.backends.weaviate import WeaviateDocumentIndex


# Define a document schema
class Document(BaseDoc):
text: str
Expand Down Expand Up @@ -376,15 +377,7 @@ This will perform a hybrid search for the word "hello" and the vector [1, 2] and
**Note**: Hybrid search searches through the object vector and all fields. Accordingly, the `search_field` keyword it will have no effect.

```python
q = (
store.build_query()
.text_search(
"world", search_field=None # Set as None as it is required but has no effect
)
.find([1, 2])
.limit(2)
.build()
)
q = store.build_query().text_search("world").find([1, 2]).limit(2).build()

docs = store.execute_query(q)
docs
Expand Down
4 changes: 2 additions & 2 deletions tests/index/weaviate/test_index_get_del_weaviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_hybrid_query(test_index):
q = (
test_index.build_query()
.find(query=query_embedding)
.text_search(query=query_text, search_field="text")
.text_search(query=query_text)
.filter(where_filter)
.build()
)
Expand All @@ -373,7 +373,7 @@ def test_hybrid_query_batched(test_index):
.find_batched(
queries=query_embeddings, score_name="certainty", score_threshold=0.99
)
.text_search_batched(queries=query_texts, search_field="text")
.text_search_batched(queries=query_texts)
.build()
)

Expand Down

0 comments on commit 096f644

Please sign in to comment.