Skip to content
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(search): return whole json documents #1782

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/server/search/doc_accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ JsonAccessor::JsonPathContainer* JsonAccessor::GetPath(std::string_view field) c
}

SearchDocData JsonAccessor::Serialize(const search::Schema& schema) const {
SearchDocData out{};
// TODO: use short names by checking current path
for (const auto& member : json_.object_range())
out[member.key()] = member.value().as_string();
return out;
return {{"$", json_.to_string()}};
}

SearchDocData JsonAccessor::Serialize(const search::Schema& schema,
Expand Down
34 changes: 20 additions & 14 deletions tests/dragonfly/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,31 @@ async def index_test_data(async_client: aioredis.Redis, itype: IndexType, prefix
await async_client.json().set(prefix + str(i), "$", e)


def doc_to_str(doc):
def doc_to_str(index_type, doc):
if not type(doc) is dict:
doc = doc.__dict__

if "json" in doc:
return json.dumps(json.loads(doc["json"]), sort_keys=True)

if index_type == IndexType.JSON:
return json.dumps(doc, sort_keys=True)

doc = dict(doc) # copy to remove fields
doc.pop("id", None)
doc.pop("payload", None)

return "//".join(sorted(doc))


def contains_test_data(res, td_indices):
def contains_test_data(itype, res, td_indices):
if res.total != len(td_indices):
return False

docset = {doc_to_str(doc) for doc in res.docs}
docset = {doc_to_str(itype, doc) for doc in res.docs}

for td_entry in (TEST_DATA[tdi] for tdi in td_indices):
if not doc_to_str(td_entry) in docset:
if not doc_to_str(itype, td_entry) in docset:
return False

return True
Expand Down Expand Up @@ -143,34 +149,34 @@ async def test_basic(async_client: aioredis.Redis, index_type):
)

res = await i1.search("article")
assert contains_test_data(res, [0, 1])
assert contains_test_data(index_type, res, [0, 1])

res = await i1.search("text")
assert contains_test_data(res, [1, 3])
assert contains_test_data(index_type, res, [1, 3])

res = await i1.search("brief piece")
assert contains_test_data(res, [2])
assert contains_test_data(index_type, res, [2])

res = await i1.search("@title:(article|last) @content:text")
assert contains_test_data(res, [1, 3])
assert contains_test_data(index_type, res, [1, 3])

res = await i1.search("@views:[200 300]")
assert contains_test_data(res, [1, 2])
assert contains_test_data(index_type, res, [1, 2])

res = await i1.search("@views:[0 150] | @views:[350 500]")
assert contains_test_data(res, [0, 3])
assert contains_test_data(index_type, res, [0, 3])

res = await i1.search("@topic:{world}")
assert contains_test_data(res, [0, 3])
assert contains_test_data(index_type, res, [0, 3])

res = await i1.search("@topic:{business}")
assert contains_test_data(res, [3])
assert contains_test_data(index_type, res, [3])

res = await i1.search("@topic:{world | national}")
assert contains_test_data(res, [0, 1, 3])
assert contains_test_data(index_type, res, [0, 1, 3])

res = await i1.search("@topic:{science | health}")
assert contains_test_data(res, [0, 2])
assert contains_test_data(index_type, res, [0, 2])

await i1.dropindex()

Expand Down
Loading