• Resolved mikedean

    (@mikedean)


    I use a custom field with my media files. When I do a search, I would like to exclude a result based on the content of the custom field.

    Is this possible?

    Ideally:
    1) Get the results returned by SOLR
    2) For each result, check the value of the custom field
    3) If the custom field = some_value, then exclude from search results.

    Thanks for your time!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author WPSolr free

    (@wpsolr)

    To filter the Solr query with your custom field value:
    https://www.wpsolr.com/guide/actions-and-filters/search-query-query/

    Thread Starter mikedean

    (@mikedean)

    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.

    Thread Starter mikedean

    (@mikedean)

    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]')

    • This reply was modified 8 years, 1 month ago by mikedean.
    • This reply was modified 8 years, 1 month ago by mikedean.
    • This reply was modified 8 years, 1 month ago by mikedean.
    • This reply was modified 8 years, 1 month ago by mikedean.
    • This reply was modified 8 years, 1 month ago by mikedean.
    Plugin Author WPSolr free

    (@wpsolr)

    If your field is a date, you’ll need to declare it’s type in tab 2.2, and index it accordingly. But as you mentioned, this is a WPSOLR PRO feature.

    Then your code will work with ‘my_custom_field_dt’
    (_dt is the dynamic type for dates declared in WPSOLR schema.xml).

    Thread Starter mikedean

    (@mikedean)

    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.

    Plugin Author WPSolr free

    (@wpsolr)

    Exact. You can choose among number, date, geolocation, string, array of string.

    Thread Starter mikedean

    (@mikedean)

    Since upgrading it appears these options have all disappeared from section 2.2

    wp-solr screenshot

    Plugin Author WPSolr free

    (@wpsolr)

    Thread Starter mikedean

    (@mikedean)

    Great thanks. Thought I had done that, but nonetheless it’s back. Cheers for the top support ??

    Thread Starter mikedean

    (@mikedean)

    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’.
    null

    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.
    Thread Starter mikedean

    (@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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Removing results based on custom fields’ is closed to new replies.