mikedean
Forum Replies Created
-
Forum: Plugins
In reply to: [WPSOLR - Elasticsearch and Solr search] “Do not search” has no effectI installed a brand new WordPress site with only the WPSOLR plugin. This little checkbox does not seem to have any effect on a basic post. Can you advise why this could be occurring? Is there some set-up or configuration needed for this functionality?
- This reply was modified 7 years, 9 months ago by mikedean.
Finally got this sorted. I’m documenting it here for future users. Here’s how it works.
1. First things first. Let’s get SOLR to index our custom field. Go to the WPSOLR plugin configuration and mark your custom field to be indexed. It’s tab 2.2. WPSOLR makes use of the dynamic fields defined in SOLR. By using a dynamic field definition, we won’t have to define the field ourselves in schema.xml. The dynamic fields available in WPSOLR are text, numbers, and dates.
See https://cwiki.apache.org/confluence/display/solr/Dynamic+Fields
yoursite.com/wp-admin/admin.php?page=solr_settings&tab=solr_option&subtab=index_opt
2. We need to hook into the SOLR query process. See the code that was posted above. Hooks are provided here:
https://www.wpsolr.com/guide/actions-and-filters/3. Limit results. In the function that is called from the hook, set the filter query to limit the result. If there is no value for the field you’re trying to limit results by, the result will still be shown.
$filterQuery->setKey( ‘some_key_value’ )->setQuery( ‘your_custom_field_machine_name:the_value_to_limit_it_by’);
The piece to note here is that ‘your_custom_field_machine_name’ should really be ‘your_custom_field_machine_name_fieldtype’. When I’m writing _fieldtype, what I mean is the extension used to define a field type with a wildcard for dynamic fields. As an example, a date uses the _dt extension (see the dynamic fields link above). So a custom field called ‘your_custom_field_machine_name’ should be ‘your_custom_field_machine_name_dt’ in the setQuery function if you set it’s type as a date in WPSOLR’s tab 2.2.
4. At this point you probably will have to reindex all your data.
I hope this helps someone as I imagine limiting results by custom metadata or dates is probably pretty common.
I have still not been able to resolve this issue, even with the steps recommended. We purchased WPSOLR Pro specifically to resolve this issue.
Here’s the WPSOLR configuration on tab 2.2. I’ve covered the real prefix with orange and replaced it in the code below with ‘myprefix’.
The field is created in a metabox like this:
sprintf(' <input class="date-picker" id="%s" type="date" name="%s" value="%s" %s data-rule-dateISO="true" />', 'myprefix_date_parution', 'myprefix_date_parution', $stored_value, $required );
Then, we try to limit the search results using a date range using the following code:
add_action( 'plugins_loaded', 'myprefix_add_wpsolr_hooks' ); function myprefix_add_wpsolr_hooks() { //Add publication date as a custom filter add_action( WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY, 'myprefix_set_publication_date_as_custom_query', 10, 1 ); } function myprefix_set_publication_date_as_custom_query( $parameters ) { $query = $parameters[ WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_QUERY ]; // Add filter query $filterQuery = $query->createFilterQuery(); $filterQuery->setKey( 'myprefix_date_parution_key_dt' )->setQuery( 'myprefix_date_parution:[1900-01-01 TO 2017-01-18]'); $query->addFilterQuery( $filterQuery ); }
Is there some part of the configuration that could have been missed? Thanks for your feedback.
- This reply was modified 8 years ago by mikedean.
Great thanks. Thought I had done that, but nonetheless it’s back. Cheers for the top support ??
Since upgrading it appears these options have all disappeared from section 2.2
Thanks this is all becoming much clearer.
Just to clarify, ‘If your field is a date, you’ll need to declare it’s type in tab 2.2’. This means after I check the box beside the field under ‘Custom Fields to be indexed’, there should be a menu that appears. In the menu, I can select the type as date (in the premium version).
Thanks very much for your support. Really appreciate your help and work.
I’ve been pouring over the Solarium Read the Docs to try to better understand the example in the WPSOLR guide.
In order to filter using custom fields, do they need to be indexed? I’m paying for Gotosolr to host the Solr installation, but I noticed in the WPSOLR admin that I can’t index the custom fields without a premium license key. When I try to use setQuery with one of my custom fields, I receive something like:
"error":{"msg":"undefined field my_custom_field","code":400}
Do custom fields have to be indexed in order to use to filter queries? Explicitly, is a premium licence required to filter results by custom fields?
For reference here is the code in the WPSOLR guide:
public function set_custom_query( $parameters ) { $query = $parameters[ WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_QUERY ]; // Add filter query $filterQuery = $query->createFilterQuery(); $filterQuery->setKey( 'some_filter_key' )->setQuery( 'my_custom_field:value_to_filter_by'); $query->addFilterQuery( $filterQuery ); }
The custom field I want to filter with is actually any date that is today or earlier. Judging by the examples in the Solarium Docs, I suppose I’ll also have to figure out how to format this.
In the Solarium docs:
setQuery('price:[1 TO 300]')
So for dates, I’m guessing:
setQuery('my_custom_field:[1900-01-01 TO 2017-01-18]')
Thank you for your reply. I read the documentation but it’s not quite evident. Could you provide a short example as to how to use WordPress custom fields to filter the results?
Thanks again for your help.
Forum: Plugins
In reply to: [WPSOLR - Elasticsearch and Solr search] Change label for facets ?Thanks. We used this as follows (object oriented context).
First, a function to check if wpsolr plugin is active:
/** * Checks if the required plugin is active in network or single site. * * @source https://queryloop.com/how-to-detect-if-a-wordpress-plugin-is-active/ * * @param $plugin * * @return bool */ function plugin_is_active( $plugin ) { $network_active = false; if ( is_multisite() ) { $plugins = get_site_option( 'active_sitewide_plugins' ); if ( isset( $plugins[$plugin] ) ) { $network_active = true; } } return in_array( $plugin, get_option( 'active_plugins' ) ) || $network_active; }
Then in the constructor of the object used as a ‘facet’:
//if the 'Apache Solr search by WPSOLR plugin' is active, hook into a filter that // allows control over the text displayed for WPSolR 'facets' if( $this->plugin_is_active( 'wpsolr-search-engine/wpsolr_search_engine.php' ) ) { //wait until the plugins are loaded to calculate the text to make sure the filter // is available add_action( 'plugins_loaded', array( $this, 'update_wpsolr_facet_labels' ) ); }
And the functions:
/** * A filter function for WPSolR, a WordPress SolR plugin, that ensures the * taxonomy's label is shown when displaying a 'facet', rather than the $machine_name. * * @param String $machine_name The original name used for the facet. * Expected as the name saved in the database to * refer to the taxonomy. * @return String $label The text to display for the facet on the search page. */ public function get_label_for_wpsolr( $machine_name ) { //set the original facet name as the default value $label = $machine_name; //if the facet is this taxonomy object, return the actual label of the taxonomy if ( $this->machine_name === $machine_name ) { $label = $this->label; } //pass the result to WPSolR to display on the search page return $label; } /** * Filter the text used to display the names of taxonomies when they are * used as 'facets' by the Apache Solr search by WPSOLR plugin. * * @return void */ public function update_wpsolr_facet_labels() { //hook into WPSolR, an additional plugin add_filter( WpSolrFilters::WPSOLR_FILTER_SEARCH_PAGE_FACET_NAME, array( $this, 'get_label_for_wpsolr' ), 10, 1 ); }