-
Notifications
You must be signed in to change notification settings - Fork 49
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 synonyms pagination #34
Open
elainenaomi
wants to merge
12
commits into
sikanhe:master
Choose a base branch
from
elainenaomi:elainenaomi/fix-synonyms-pagination
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c3589f8
Add Synonyms
CaiqueMitsuoka 99a3f7a
Add Query Rules
CaiqueMitsuoka bab02ff
Update documentation
CaiqueMitsuoka 5d34893
Fix query rule param
CaiqueMitsuoka 1214931
Remove params sanitization from Path
CaiqueMitsuoka 71c2ad9
Add query params to Algolia.set_settings
CaiqueMitsuoka dc4a9f3
Fix synonyms pagination
elainenaomi ecb1f49
Improve tests
elainenaomi 9829f2a
Fix broken test
elainenaomi 8117fe5
Remove unnecessary test
elainenaomi 7c49399
Improve test
elainenaomi 4df259f
Fix broken spec
elainenaomi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -473,9 +473,9 @@ defmodule Algolia do | |
@doc """ | ||
Set the settings of a index | ||
""" | ||
def set_settings(index, settings) do | ||
def set_settings(index, settings, opts \\ []) do | ||
body = Jason.encode!(settings) | ||
path = Paths.settings(index) | ||
path = Paths.settings(index, opts) | ||
|
||
:write | ||
|> send_request(%{method: :put, path: path, body: body}) | ||
|
@@ -491,6 +491,186 @@ defmodule Algolia do | |
|> inject_index_into_response(index) | ||
end | ||
|
||
@doc """ | ||
Get all the synonyms of a index. | ||
|
||
Only the index is required | ||
|
||
## Examples | ||
|
||
iex> Algolia.export_synonyms("index") | ||
{:ok, %{ | ||
"indexName" => "index", | ||
"nbHits" => 33, | ||
"hits" => [%{ | ||
"objectID" => "1539816124475_0", | ||
"synonyms" => ["big", "large"], | ||
"type" => "synonym" | ||
"_highlightResult" => %{...} | ||
}, ...] | ||
}} | ||
""" | ||
def export_synonyms(index, hits_per_page \\ 100) do | ||
get_all_paginated_hits(index, &search_synonyms/3, hits_per_page) | ||
end | ||
|
||
defp get_all_paginated_hits(index, search, hits_per_page \\ 100) do | ||
0 | ||
|> Stream.iterate(&(&1 + 1)) | ||
|> Stream.flat_map(fn page -> get_page_hits(index, page, hits_per_page, search) end) | ||
|> Stream.take_while(&(&1 != :stop)) | ||
end | ||
|
||
defp get_page_hits(index, page, hits_per_page, search) do | ||
case search.(index, "", page: page, hits_per_page: hits_per_page) do | ||
{:ok, %{"hits" => hits, "nbPages" => pages}} when page + 1 < pages -> | ||
page_hits(hits) | ||
|
||
{:ok, %{"hits" => hits, "nbHits" => nbHits}} when page + 1 < nbHits / hits_per_page -> | ||
page_hits(hits) | ||
|
||
{:ok, %{"hits" => hits}} -> | ||
page_hits(hits) ++ [:stop] | ||
|
||
error -> | ||
[error, :stop] | ||
end | ||
end | ||
|
||
defp page_hits(hits) do | ||
Enum.map(hits, &{:ok, Map.drop(&1, ["_highlightResult"])}) | ||
end | ||
|
||
@doc """ | ||
Search for synonyms of a index matching a query | ||
|
||
Allowed parameters: | ||
|
||
* `query` Required | ||
* `type` Defaults to `""`(all). Allowed the types: `"synonym,oneWaySynonym,altCorrection1,altCorrection2,placeholder"` | ||
* `page` | ||
* `hitsPerPage` | ||
|
||
## Examples | ||
|
||
iex> Algolia.search_synonyms("index", "query", hits_per_page: 20) | ||
{:ok, %{ | ||
"indexName" => "index", | ||
"nbHits" => 33, | ||
"hits" => [%{ | ||
"objectID" => "1539816124475_0", | ||
"synonyms" => ["big", "large"], | ||
"type" => "synonym" | ||
"_highlightResult" => %{...} | ||
}, ...] | ||
}} | ||
""" | ||
def search_synonyms(index, query, opts \\ []) do | ||
body = | ||
opts | ||
|> Enum.into(%{}) | ||
|> Map.put("query", query) | ||
|> Map.put("page", opts[:page] || 0) | ||
|> Map.put("hitsPerPage", opts[:hits_per_page] || 20) | ||
|> Map.drop([:page, :hits_per_page]) | ||
|> Jason.encode!() | ||
|
||
:write | ||
|> send_request(%{method: :post, path: Paths.search_synonyms(index), body: body}) | ||
|> inject_index_into_response(index) | ||
end | ||
|
||
@doc """ | ||
Create or update an list of synonyms. | ||
|
||
* `synonyms` Required: With [synonyms objects](https://www.algolia.com/doc/api-reference/api-methods/save-synonym/#method-param-synonym-object). | ||
* - `objectID` Required: If the Id do not exist it will be created. | ||
* - `type` Required: Allowed the types: `"synonym,oneWaySynonym,altCorrection1,altCorrection2,placeholder"`. | ||
* - `synonyms` Required if type=synonym or type=oneWaySynonym: List of strings. | ||
* - `input` Required if type=oneWaySynonym. | ||
* - `word` Required if type=altCorrection1 or type=altCorrection2. | ||
* - `corrections` Required if type=altCorrection1 or type=altCorrection2. | ||
* - `placeholder` Required if type=placeholder. | ||
* - `replacements` Required if type=placeholder. | ||
|
||
Allowed params: | ||
* `forwardToReplicas` | ||
* `replaceExistingSynonyms` | ||
""" | ||
def batch_synonyms(index, batch, opts \\ []) do | ||
body = Jason.encode!(batch) | ||
|
||
:write | ||
|> send_request(%{method: :post, path: Paths.batch_synonyms(index, opts), body: body}) | ||
|> inject_index_into_response(index) | ||
end | ||
|
||
@doc """ | ||
Search for query rules of a index matching a query | ||
|
||
Allowed parameters: | ||
|
||
* `query` Required | ||
* `page` | ||
* `hitsPerPage` | ||
""" | ||
def search_rules(index, query, opts \\ []) do | ||
body = | ||
opts | ||
|> Enum.into(%{}) | ||
|> Map.put("query", query) | ||
|> Map.put("page", opts[:page] || 0) | ||
|> Map.put("hitsPerPage", opts[:hits_per_page] || 20) | ||
|> Map.drop([:page, :hits_per_page]) | ||
|> Jason.encode!() | ||
|
||
:write | ||
|> send_request(%{method: :post, path: Paths.search_rules(index), body: body}) | ||
|> inject_index_into_response(index) | ||
end | ||
|
||
@doc """ | ||
Get all the query rules of a index. | ||
|
||
Only the index is required | ||
""" | ||
def export_rules(index) do | ||
get_all_paginated_hits(index, &search_rules/3) | ||
end | ||
|
||
@doc """ | ||
Create or update an list of Query Rules. | ||
|
||
* `batch` Required: list with [Query Rules](https://www.algolia.com/doc/api-reference/api-methods/save-rule/#method-param-rule) | ||
* - `objectID` Required: If the Id do not exist it will be created. | ||
* - `description` To ease searching for rules and presenting them to human readers. | ||
* - `enabled` Whether the rule is enabled. Disabled rules remain in the index, but are not applied at query time. | ||
* - `validity` By default, rules are permanently valid. When validity periods are specified, the rule applies only during those periods. | ||
* - `condition` Required: [condition](https://www.algolia.com/doc/api-reference/api-methods/save-rule/?language=javascript#method-param-condition-2) | ||
* -- `pattern` Required: Query patterns are expressed as a string with a specific syntax. | ||
* -- `anchoring` Required: Enum `["is", "startsWith", "endsWith", "contains"]`. | ||
* -- `context`: Rule context. When specified, the rule is contextual and applies only when the same context is specified at query time. | ||
* - `consequence` Required at least 1 [consequence](https://www.algolia.com/doc/api-reference/api-methods/save-rule/?language=javascript#method-param-consequence-2) | ||
* -- `params`: Additional search parameters. Any valid search parameter is allowed. | ||
* -- `promote`: List with objects to promote as hits. | ||
* --- `objectID` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty cool. How does it look like in |
||
* --- `position` | ||
* -- `hide`: List with objects to hide from hits. | ||
* --- `objectID` | ||
* -- `userData`: Custom JSON object that will be appended to the userData array in the response. | ||
|
||
Allowed params: | ||
* `forwardToReplicas` When true, the change is forwarded to all replicas of this index. | ||
* `clearExistingRules` When true, existing rules are cleared before adding this batch. | ||
""" | ||
def batch_rules(index, batch, opts \\ []) do | ||
body = Jason.encode!(batch) | ||
|
||
:write | ||
|> send_request(%{method: :post, path: Paths.batch_rules(index, opts), body: body}) | ||
|> inject_index_into_response(index) | ||
end | ||
|
||
@doc """ | ||
Moves an index to new one | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an edge case when Algolia doesn't return
nbPages
in their response JSON?