-
Notifications
You must be signed in to change notification settings - Fork 15
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
Optimise search results #6
Comments
Hi @ray-001 , Sorry for the late answer. Thanks. |
Hi @ray-001 , Regarding the pagination, you could implement something like the following: {% extends "_layout" %}
{% set title = "Search" %}
{% set results = craft.elasticsearch.search(craft.app.request.get('q')) %}
{% set resultsDataProvider = create({'class': 'yii\\data\\ArrayDataProvider', 'allModels':results, 'pagination': {'pageSize': 10}}) %} {# adjust pageSize to the expected number of items per page #}
{% set results = resultsDataProvider.getModels() %}
{% set currentPage = resultsDataProvider.getPagination().getPage() + 1 %}
{% set pageCount = resultsDataProvider.getPagination().getPageCount() %}
{% if currentPage > 1 %}
{% set prevPage = currentPage - 1 %}
{% endif %}
{% if currentPage < pageCount %}
{% set nextPage = currentPage + 1 %}
{% endif %}
{% block content %}
<h1>{{ "Search"|t }}</h1>
<form action="{{ url('search') }}">
<input type="search" name="q" placeholder="Search" value="{{ craft.app.request.get('q') }}">
<input type="submit" value="Go">
</form>
{% if results|length %}
<h2>{{ "Results"|t }}</h2>
{% for result in results %}
<h3>{{ result.title }}</h3>
<p>
<small><a href="{{ result.url|raw }}">{{ result.url }}</a><br/>
{% if result.highlights|length %}
{% for highlight in result.highlights %}
{{ highlight|raw }}<br/>
{% endfor %}
{% endif %}
</small>
</p>
<hr>
{% endfor %}
{% if prevPage is defined %}<a href="{{ url(craft.request.url, {'q': craft.app.request.get('q'), 'page': prevPage}) }}">Previous Page</a>{% endif %}
{% if nextPage is defined %}<a href="{{ url(craft.request.url, {'q': craft.app.request.get('q'), 'page': nextPage}) }}">Next Page</a>{% endif %}
{% else %}
{% if craft.app.request.get('q') is not null %}
<p>
<em>{{ "No results"|t }}</em>
</p>
{% endif %}
{% endif %}
{% endblock %} This may not be the perfect solution, but it works pretty well. I hope the answer suits you. Regards. |
Hi @juban, Thanks for the reply and the solution. This will work for me. Kind regards, |
Hi,
I'm considering using this plugin for one of our clients but I have two questions regarding features of ElasticSearch what needs to be a key feature in our applications:
- Is it possible to make the search queries Fuzzy (not exact matching)-- https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzzinessEdit: It's possible to hook upon the before search event to trigger extra param for fuzziness
Should be great to have the option available in craft.elasticsearch.search() function though.
-- https://docs.craftcms.com/v3/dev/tags/paginate.html#app
Kind regards,
Raymond
The text was updated successfully, but these errors were encountered: