Skip to content

Commit

Permalink
Xata Go - more code snippets (#184)
Browse files Browse the repository at this point in the history
* fix indentation

* more Go code snippets

* summaries examples

* add known limitation for consistency

* Update 040-SDK/030-Go/010-overview.mdx

---------

Co-authored-by: joan-ing <[email protected]>
  • Loading branch information
philkra and joan-ing authored Dec 7, 2023
1 parent 6d1e14d commit 119e09b
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 27 deletions.
1 change: 1 addition & 0 deletions 040-SDK/030-Go/010-overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ XATA_API_KEY="<INSERT_YOUR_API_KEY>" go run example.go

- [Numeric operations](/docs/sdk/update#numeric-operations) are currently not supported using the update API. As a workaround, you can use the transaction API [example](/docs/sdk/transaction).
- Not all Xata endpoints are available at the alpha state of the SDK. You can check the coverage [here](https://github.com/xataio/xata-go/issues/1).
- Currently, there is no option to change the [consistency](/docs/concepts/data-model#consistency) and the default setting is `strong`.
10 changes: 5 additions & 5 deletions 040-SDK/101-get.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ searchClient, _ := xata.NewSearchAndFilterClient()
data, _ := searchClient.Query(context.TODO(), xata.QueryTableRequest{
TableName: "{tabel_name}",
Payload: xata.QueryTableRequestPayload{
Columns: []string{"col_1", ".."},
Columns: []string{"col_1", ".."},
Filter: &xata.FilterExpression{
// ..
},
// ..
},
Sort: xata.NewSortExpressionFromStringSortOrderMapList(
// ..
// ..
),
Page: &xata.SearchPageConfig{
// ..
},
},
}
})
```
Expand Down
87 changes: 71 additions & 16 deletions 040-SDK/111-search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ The search index is updated asynchronously after each insert/update, meaning tha

The format of a search request at the branch level (across tables) is as follows:

<TabbedCode tabs={['TypeScript', 'Python', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'JSON']}>

```ts
const results = await xata.search.all("<search phrase>", {
tables: [
{
table: "...",
target: [...],
filter: {...},
boosters: [...]
},
{ ... },
...
],
fuzziness: 1,
prefix: "phrase",
highlight: {...}
tables: [
{
table: "...",
target: [...],
filter: {...},
boosters: [...]
},
{ ... },
...
],
fuzziness: 1,
prefix: "phrase",
highlight: {...}
});
```

Expand All @@ -52,6 +52,24 @@ results = xata.data().search_branch({
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
prefix := xata.PrefixExpressionPhrase
results, _ := searchClient.SearchBranch(context.TODO(), xata.SearchBranchRequest{
Payload: xata.SearchBranchRequestPayload{
Query: *xata.String("<search phrase>"),
Tables: []*xata.SearchBranchRequestTablesItem{
// ..
},
Fuzziness: xata.Int(1),
Prefix: &prefix,
Highlight: &xata.HighlightExpression{
// ..
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/search

Expand Down Expand Up @@ -281,7 +299,7 @@ results, _ := searchClient.SearchBranch(context.TODO(), xata.SearchBranchRequest

If you want to search in a single table, it's easier to use the table-level search API. It looks like this:

<TabbedCode tabs={['TypeScript', 'Python', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'JSON']}>

```ts
const results = await xata.db.Users.search("<search phrase>", {
Expand All @@ -306,6 +324,30 @@ results = xata.data().search_table("Users", {
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
prefix := xata.PrefixExpressionPhrase
results, _ := searchClient.SearchTable(context.TODO(), xata.SearchTableRequest{
Payload: xata.SearchTableRequestPayload{
Query: *xata.String("<search phrase>"),
Filter: &xata.FilterExpression{
// ..
},
Target: []*xata.TargetExpressionItem{
// ..
},
Boosters: []*xata.BoosterExpression{
// ..
},
Fuzziness: xata.Int(1),
Prefix: &prefix,
Highlight: &xata.HighlightExpression{
// ..
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/search
{
Expand Down Expand Up @@ -494,7 +536,7 @@ results = xata.data().search_branch({

By default, Xata searches across all columns from the selected tables. You can restrict the search to specific columns by using the `target` field.

<TabbedCode tabs={['TypeScript', 'Python', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'JSON']}>

```ts
const results = await xata.search.all('new st', {
Expand All @@ -519,6 +561,19 @@ results = xata.data().search_branch({
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
results, _ := searchClient.SearchTable(context.TODO(), xata.SearchTableRequest{
Payload: xata.SearchTableRequestPayload{
Query: *xata.String("new st"),
Target: []*xata.TargetExpressionItem{
xata.NewTargetExpression("name"),
xata.NewTargetExpression("address.street"),
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/search

Expand Down
114 changes: 108 additions & 6 deletions 040-SDK/121-summarize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Let's try one more example to help clarify the concepts.

Here, as above, the column is `product`. We will also want to run a calculation on `sale_price`, however, we want to add up each sale price rather than average them, so our `summary` equation will be `sum`. Our request looks like:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
columns: ['product'],
Expand All @@ -113,6 +113,19 @@ records = xata.data().summarize("sales", {
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
TableName: "sales",
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{"product"},
Summaries: map[string]map[string]any{
"total_sales": {"sum": "sale_price"},
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand Down Expand Up @@ -152,7 +165,7 @@ Before we get started, keep in mind that we keep reference material that can be

In its most basic form `summarize` can be used as a "distinct" method to get the unique values or value combinations of the summarized columns.

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
columns: ['username'],
Expand All @@ -167,6 +180,16 @@ records = xata.data().summarize("sales", {
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
TableName: "sales",
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{"username"},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand Down Expand Up @@ -203,7 +226,7 @@ All summaries support links. If you have a link set up, you specify it using the

One can specify several summaries in the same request. As long as the names are unique, do not contain special characters, and do not conflict with columns on your table, you are free to set the names as you wish.

<TabbedCode tabs={['TypeScript', 'Python', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
summaries: {
Expand Down Expand Up @@ -260,6 +283,37 @@ records = xata.data().summarize("sales", {
})
````
```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{"product"},
Summaries: map[string]map[string]any{
// count all rows
"all_sales": {"count": "*"},

// count all rows where `store_id` is not null
"all_sales_from_store": {"count": "store_id"},

// sum the `profit` column
"total_profit": {"sum": "profit"},

// average the `shipping.time_days` column
"average_shipping_time": {"average": "shipping.time_days"},

// count the total rows where `has_arrived` is not null
"total_has_arrived": {"count": "has_arrived"},

// retrieve the smallest value in the `package_volume` column
"smallest_package": {"min": "package_volume"},

// retrieve the largest value in the `sale_price` column
"largest_sale_price": {"max": "sale_price"},
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/summarize
{
Expand Down Expand Up @@ -294,7 +348,7 @@ records = xata.data().summarize("sales", {

In the previous example, it was indicated that using `"all_sales": {"count": "*"}` yields the number of records from the chosen table. To maintain simplicity, the subsequent example demonstrates how to obtain the total count of records in the `sales` table.

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
summaries: {
Expand All @@ -316,6 +370,19 @@ records = xata.data().summarize("sales", {
print(records["summaries"][0]["count"])
````
```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
TableName: "sales",
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{"product"},
Summaries: map[string]map[string]any{
"total": {"count": "*"},
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand All @@ -341,7 +408,7 @@ The `columns` key lets you specify how you'd like to group your data. Columns wo

You may submit explicit column names, use wildcards, and reference linked columns as needed.

<TabbedCode tabs={['TypeScript', 'Python', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
columns: [
Expand All @@ -362,6 +429,20 @@ records = xata.data().summarize("sales", {
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
TableName: "sales",
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{
"settings.*", // group by all columns in the `settings` object
"username", // group by the username field
"user.hobbies.name", // group by a linked column
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/summarize
{
Expand All @@ -387,7 +468,7 @@ Keep in mind: `filter` does **not** support filtering on the result of a summary

Sort lets you decide how you'd like your results sorted. You may sort on `columns` you've explicitly referenced, as well as on `summaries` you've requested. When wanting to sort by a summary, you use the name you've chosen.

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
const records = await xata.db.sales.summarize({
columns: {"store_name"},
Expand Down Expand Up @@ -418,6 +499,27 @@ records = xata.data().summarize("sales", {
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
records, _ := searchClient.Summarize(context.TODO(), xata.SummarizeTableRequest{
TableName: "sales",
Payload: xata.SummarizeTableRequestPayload{
Columns: []string{"store_name"},
Summaries: map[string]map[string]any{
"total": {"count": "*"},
},
Sort: xata.NewSortExpressionFromStringSortOrderMapList(
[]map[string]xata.SortOrder{
// put the highest total_sales at the top
{"total_sales": xata.SortOrderDesc},
// if total_sales is equal for two rows, order them by the store_name
{"store_name": xata.SortOrderAsc},
},
),
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand Down

0 comments on commit 119e09b

Please sign in to comment.