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

group_by with return_properties #4912

Open
1 task done
sebawita opened this issue May 14, 2024 · 0 comments
Open
1 task done

group_by with return_properties #4912

sebawita opened this issue May 14, 2024 · 0 comments

Comments

@sebawita
Copy link
Contributor

sebawita commented May 14, 2024

Describe your feature request

Hi there,

It would be great to run an aggregate query and return a selection of properties for matched objects within each group.

For example, if we have a collection of shopping items, like:

{
  "name": "red table",
  "price": 200,
  "type": "furniture",
  "description": "a beautiful red table with rounded corners like your iPhone"
}

It should be possible to search for "red furniture" and get back something like:

groups: [
  { 
     group: "red table", 
     objects: [ { name: ..., price: ...},  { name: ..., price: ...},  { name: ..., price: ...} ]
  },
  { 
     group: "ruby table", 
     objects: [ { name: ..., price: ...},  { name: ..., price: ...},  { name: ..., price: ...} ]
  },
]

Proposal

Can we add return_properties (or we could call it include_properties) to aggregate.near_x queries, like this:

response = artworks.aggregate.near_text(
    query="red furniture",
    object_limit=200,

    group_by=GroupByAggregate(
        prop="type", limit=3
    ),
    
    # proposal to add `return_properties`
    return_properties=[
        "name",
        "price",
        "type" # yes, it should be possible to return the group_by value as well
    ]
)

Then we should be able to access each group and their underlying objects (with the selected properties) like this:

for group in response.groups:
    for item in group.objects:
        print(item.properties) # access the requested properties
        print(item.id) # access the object id, it should be there by default

The current workaround

Currently, we can use:

return_metrics=Metrics("single_prop_name").text(),

or

return_metrics=[
    Metrics("name").text(),
    Metrics("description").text()
]

However, there is no way to get the required properties together - as in, we can't get the name+description together.

Also, the code access these properties is not the most elegant, and in this case we are not interested in how many times that value occurs:

for group in response.groups:
    for name in group.properties["name"].top_occurrences
        print(name.value)    

Code of Conduct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant