简体   繁体   English

ElasticPress - 搜索元和高级自定义字段不起作用

[英]ElasticPress - searching meta and Advanced Custom Fields not working

We are using Wordpress 4.9.5 with ElasticPress 2.5 and Searchly.我们将 Wordpress 4.9.5 与 ElasticPress 2.5 和 Searchly 一起使用。

The default search works fine.默认搜索工作正常。 However, we are indexing hundreds of thousands of part numbers, each of which are a post in wordpress.但是,我们正在索引数十万个零件编号,每个零件编号都是 wordpress 中的一个帖子。 Like many part numbers, they have titles like ABC-12-B23-QQ.像许多零件号一样,它们的名称类似于 ABC-12-B23-QQ。

We are using ElasticPress right out of the box.我们开箱即用地使用 ElasticPress。 It's advertised as working great right out of the box, but ...它被宣传为开箱即用,但......

It appears that it's only searching the post titles.看来它只是在搜索帖子标题。 It is not searching our Advanced Custom Fields at all.它根本不搜索我们的高级自定义字段。 We are using ACF for all of the part details, like length, color, and finish, so it is very important that we be able to search these fields.我们将 ACF 用于所有零件细节,例如长度、颜色和表面处理,因此能够搜索这些字段非常重要。

How do I get Elasticpress to search my custom fields?如何让 Elasticpress 搜索我的自定义字段? Do I need to use custom mapping?我需要使用自定义映射吗?

https://github.com/10up/ElasticPress https://github.com/10up/ElasticPress

NOTE: I am posting this so it is included on stackoverflow for the benefit of others who search later.注意:我发布此内容是为了将其包含在 stackoverflow 中,以供以后搜索的其他人使用。 It would have saved me hours if I would have found this on Stackoverflow at the beginning of my search.如果我在搜索开始时在 Stackoverflow 上找到它,那将节省我几个小时。 :) :)

It turns out that yes, ElasticPress does map all the meta fields automatically, but by default, it does not search them .事实证明,是的,ElasticPress 会自动映射所有元字段,但默认情况下,它不会搜索它们

According to the documentation ( https://github.com/10up/ElasticPress ),根据文档( https://github.com/10up/ElasticPress ),

"The following are special parameters that are only supported by ElasticPress. “以下是仅 ElasticPress 支持的特殊参数。

search_fields (array)
If not specified, defaults to array( 'post_title', 'post_excerpt', 'post_content' ).

This was my problem.这是我的问题。 To solve, you must tell ElasticPress what fields to search.要解决这个问题,你必须告诉 ElasticPress 要搜索哪些字段。

The ElasticPress documentation includes this example using WP_Query: ElasticPress 文档包括这个使用 WP_Query 的例子:

new WP_Query( array(
    's'             => 'meta search phrase',
    'search_fields' => array(
        'post_title',
        'post_content',
        'post_excerpt',
        'meta' => array( 'meta_key_1', 'meta_key_2' ),
    ),
) );

I was really needing to hook into the existing search query, so here is an example of what I did using the pre_get_posts action:我真的需要连接到现有的搜索查询,所以这里是我使用 pre_get_posts 操作所做的一个例子:

function my_search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search() ) {
        $query->set('search_fields', array(
        'post_title',
        'meta' => array( 'bin_part_number',
                        'shelf_part_number',
                        )));
    }
  }
}
add_action('pre_get_posts','my_search_filter',1);

At this point in time, the only method that truly works is to use the ep_search_fields hook.此时,唯一真正有效的方法是使用ep_search_fields钩子。 Note: You must make sure that the field is indexed, but all ACF Fields are added by default.注意:您必须确保该字段已编入索引,但默认情况下会添加所有 ACF 字段。 You can use the following code.您可以使用以下代码。 The key, after long struggles, is that it must be provides as meta .yourfield.经过长期斗争,关键是它必须作为.yourfield 提供。 value价值

add_filter( 'ep_search_fields', 'update_search_fields', 11, 2 );
/**
 * Updates fields to search against.
 *
 * @param array $fields List of searchable fields.
 * @param array $args List of arguments provided.
 *
 * @return array
 */
function update_search_fields( array $fields, array $args ): array {
    $fields[] = 'meta.mfr_part.value';

    return $fields;
}

and a more complex example that allows you to supply an array of fields, since this array can be generated by a helper function and used elsewhere.还有一个更复杂的例子,它允许你提供一个字段数组,因为这个数组可以由一个辅助函数生成并在其他地方使用。

/**
  * ElasticSearch: Modify available search fields
  */
  add_filter( 'ep_search_fields', function($search_fields, $args ) {
    $fields = ['my_field'];
    foreach($acf_fields as $key => $label) {
    $fields[] = 'meta.'.$key.'.value';
  }
  return array_merge( $search_fields, $fields );
 }, 10,2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM