Skip to content

Commit

Permalink
Add additional examples for grouping and facets
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh1nku committed Dec 10, 2024
1 parent 4db2eb7 commit d062e2d
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions wrappers/python/solrstice/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,34 @@ class SolrGroupResult:
"""
Gets the field results form a group query
:return: List of group field results
>>> from solrstice import GroupingComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
>>> client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
>>> async def field_result() -> None:
... group_builder = GroupingComponent(fields=["age"], limit=10)
... select_builder = SelectQuery(fq=["age:[* TO *]"], grouping=group_builder)
... groups = (await client.select(select_builder, "example_collection")).get_groups()
... age_group = groups["age"]
... docs = age_group.get_field_result()
"""

def get_query_result(self) -> Optional["SolrDocsResponse"]:
"""
Gets the query result from a group query
:return: Query result
>>> from solrstice import GroupingComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
>>> client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
>>> async def group_query_result() -> None:
... group_builder = GroupingComponent(queries=["age:[0 TO 59]", "age:[60 TO *]"], limit=10)
... select_builder = SelectQuery(fq=["age:[* TO *]"], grouping=group_builder)
... groups = (await client.select(select_builder, "example_collection")).get_groups()
... age_group = groups["age:[0 TO 59]"]
... group = age_group.get_query_result()
... assert group is not None
... docs = group.get_docs()
"""

def get_simple_result(self) -> Optional["SolrDocsResponse"]:
Expand Down Expand Up @@ -166,18 +188,62 @@ class SolrJsonFacetResponse:
"""
Get the buckets for this facet
:return: A list of buckets
>>> from solrstice import AsyncSolrCloudClient, SolrServerContext, SelectQuery, JsonFacetComponent, JsonTermsFacet
>>> client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
>>> async def json_facet_buckets() -> None:
... select_builder = SelectQuery(
... json_facet=JsonFacetComponent(facets={"age": JsonTermsFacet("age")})
... )
... response = await client.select(select_builder, "example_collection")
... facets = response.get_json_facets()
... assert facets is not None
... age_buckets = facets.get_nested_facets()["age"].get_buckets()
... assert len(age_buckets) == 3
"""

def get_flat_facets(self) -> Dict[str, Any]:
"""
Get stat counts for this facet
:return: A dictionary of stat counts
>>> from solrstice import JsonFacetComponent, JsonStatFacet, SelectQuery, SolrServerContext, AsyncSolrCloudClient
>>> client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
>>> async def json_flat_facets() -> None:
... select_builder = SelectQuery(
... json_facet=JsonFacetComponent(
... facets={"total_people": JsonStatFacet("sum(count)")}
... )
... )
... response = await client.select(select_builder, "example_collection")
... facets = response.get_json_facets()
... assert facets is not None
... total_people = facets.get_flat_facets()["total_people"]
... assert total_people == 1000
"""

def get_nested_facets(self) -> Dict[str, "SolrJsonFacetResponse"]:
"""
Get the nested facets for this facet
:return: A dictionary of nested facets
>>> from solrstice import JsonFacetComponent, JsonQueryFacet, SelectQuery, SolrServerContext, AsyncSolrCloudClient
>>> async def main() -> None:
... client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
... select_builder = SelectQuery(
... json_facet=JsonFacetComponent(
... facets={"below_60": JsonQueryFacet("age:[0 TO 59]")}
... )
... )
... response = await client.select(select_builder, "example_collection")
... facets = response.get_json_facets()
... assert facets is not None
... below_60 = facets.get_nested_facets().get("below_60")
... assert below_60 is not None
... assert below_60.get_count() == 4
"""

def get_count(self) -> Optional[int]:
Expand Down

0 comments on commit d062e2d

Please sign in to comment.