tpg73
Forum Replies Created
-
Forum: Plugins
In reply to: [WP Search with Algolia] Add search history to autocomplete templatesThank you for your answer.
Yes. I referred to that page. The autocomplete.js plugin is 0.38.x so I think it probably can’t be used, so I’m trying to add my own search history function. After further investigation, I came up with the following source:
var sources = [];
var recentSearch = {
source: function( query, callback ) {
var history = [
{query: 'clock'},
{query: 'mobile'},
]
callback(history);
},
templates: {
header:function () {
return wp.template( 'autocomplete-header' )( {
label: 'recent-search'
} );
},
suggestion: function (suggestion) {
return wp.template( 'autocomplete-recent-search' )( {
query: suggestion.query
} );
}
}
};
sources.push( recentSearch );I haven’t been able to implement the crucial search history yet, but I was able to add any section to the autocomplete, so I’ll consider that the issue is resolved.
Thank you as always.
My previous method using wp_query had performance issues. I had 7 price range links and needed to make 7 requests to Algolia. So I changed it to a different method. I hope this helps someone.
We have pre-indexed ‘attributesForFaceting’ for seven price ranges.
$_algolia = Algolia_Plugin_Factory::create();
$_algolia_index = $_algolia->get_index( 'searchable_posts' )->get_index();
$search_parameters = [
'query' => get_query_var( 'keyword' ),
'filters' => 'taxonomies.category:clock',
];
$faset_hits = $_algolia_index->searchForFacetValues( 'cat_price_range', '*', $search_parameters );Since
searchForFacetValues
??existed in the plugin source, I decided to call it from the theme side. This reduced the number of Algolia calls from 7 to 1, making performance more tolerable.It seems that the data can also be obtained by passing
'facets'
to thealgolia_search_params
filter, which would be the most desirable option, but since this is executed within thepre_get_posts
filter and it seemed impossible to obtain the data without directly modifying the plugin code, I gave up on that idea.Although it’s tentative, I was able to achieve the requirement with the following process.
public function create_price_count() {
// '1000-1500' → '1000 TO 1500'
$this->price_algolia = str_replace( '-', ' TO ', $price );
$args = [
's' => get_query_var( 'keyword' ),
];
add_filter( 'algolia_should_filter_query', array( $this, 'change_should_filter_query' ), 10, 2 );
add_filter( 'algolia_search_params', array( $this, 'algolia_filter_backend_search_params' ) );
$wp_query = new WP_Query( $args );
remove_filter( 'algolia_search_params', array( $this, 'algolia_filter_backend_search_params' ) );
remove_filter( 'algolia_should_filter_query', array( $this, 'change_should_filter_query' ) );
return $wp_query->found_posts;
}
public function change_should_filter_query( $should_filter, $query ) {
return true;
}
public function algolia_filter_backend_search_params( $params ) {
$params['filters'] = 'price:' . $this->price_algolia;
return $params;
}I’ll mark this as resolved, but I’d appreciate any further advice you may have.
Thank you.
Sorry, the process hasn’t been finalized yet, but it looks like it can be achieved.
By using the
algolia_should_filter_query
andalgolia_search_params
filters and controlling the subquery ofwp_query
, I was able to obtain found_posts for each price range.Once the process is finalized, I’d like to mark it as resolved.
Thank you for your answer. It’s very helpful.
Is it possible to issue a query directly to Algolia from the backend? For example, suppose a user searches for “refrigerator” and there are 300 results. If I could issue a query directly to Algolia based on “refrigerator” and each price range, I would be able to find out the number of results for each price range.
If I perform a similar search using WordPress’s standard wp_query, I cannot reproduce the Algolia search due to ambiguous notation and differences in search target attributes.
Thank you for your detailed advice. I’m relieved to hear that my approach is correct. Yes. I want to avoid hard coding, so I would like to generate the filters value on the server side and pass it in an inline script.
I also managed to implement multiple autocompletes. Thank you for your advice on setting facets. This time, the taxonomies were already set, so I didn’t do anything in particular, but when I change the facets, I would like to manage them with code.
Thank you for your quick response. I look forward to working with you again.
Sorry, I forgot to mention something.
My site has a site-wide search box in the header, which implements an Algolia search for all products.My question this time is about setting filters for a new search box that I’ve added to the archive page for electrical appliances, for example, in addition to the site-wide search box.
I understood from your previous comment how to use “filters,” but I don’t know how to branch based on the search box.
I was able to solve the issue to some extent.
I modified autocomplete.php as follows.source: algoliaHitsSource( client.initIndex( config[ 'index_name' ] ), {
hitsPerPage: config[ 'max_suggestions' ],
attributesToSnippet: [
'content:10'
],
highlightPreTag: '__ais-highlight__',
highlightPostTag: '__/ais-highlight__',
filters: '(taxonomies.category:"TVs" OR taxonomies.category:"refrigerators")'
} ),I added filters.
Also, it seemed necessary to change the settings for the posts_post index in the Algolia dashboard. I opened “Facets” in the “Filtering and Faceting” section and added an Attribute, and the autocomplete filter was reflected.
Is my method correct? Please let me know if you have any further information. I used the following sites as reference.
https://discourse.algolia.com/t/shopify-add-a-filter-to-the-autocomplete-and-or-the-search-page/3975
https://www.algolia.com/doc/api-reference/api-parameters/filters/#filters
Thank you for checking. The problem has been resolved.
When I checked
wp-content/db.php
, it was found to be a corrupted file. I deleted it with the rm command and the problem was resolved.Also, after that, when I reinstalled and enabled the plugin,
wp-content/db.php
was created, and when I disabled the plugin,wp-content/db.php
was deleted.It seems there was a problem with my environment. This was very helpful. Now I can use the plugin with peace of mind. Thank you.
Forum: Plugins
In reply to: [WP Search with Algolia] How to set extra headerThank you for your response. I have also checked the issue. We will review the change log in a future update.
Thank you very much for your help. Mark as resolved.
Forum: Plugins
In reply to: [WP Search with Algolia] How to set extra headerThanks for letting us know.
Thanks to you, I was able to achieve what I wanted to do.I have implemented the following filter hooks
function wds_algolia_custom_searchclient_config( $config ) { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR']; $headers = [ 'X-Forwarded-For' => $ip_address, ]; $config['headers'] = $headers; return $config; } add_filter( 'algolia_custom_search_config', 'wds_algolia_custom_searchclient_config' );
And I added the following code to line 85 of class-algolia-search-client-factory.php
if ( is_array($custom_config['headers'] ) && ! empty( $custom_config['headers'] ) ) { $config->setDefaultHeaders( $custom_config['headers'] ); }
However, the result is editing the plugin source file directly, and future version upgrades may require re-editing the source file. Is this unavoidable in the current situation?