I'm trying to build auto-suggestive search results which make use of wildcard in the search term within the post_title and post_content; however it seems my code only works when I either do it for a post_title or post_content, but not for both together
function set_es_search_args( $ep_formatted_args, $args ) {
if( isset( $args['autocomplete'] ) && $args['autocomplete'] ) {
$ep_formatted_args['query'] = array("wildcard"=>array("post_title" => $args['s'], "post_content" => $args['s']));
}
return $ep_formatted_args;
}
add_filter( 'ep_formatted_args', 'set_es_search_args', 300, 2 );
Hey @diwakhat1603,
I am not sure 100% that it will work, but can you try to use query_string with multiple fields instead? Something like this:
$ep_formatted_args['query'] = array(
"query_string" => array(
"query" => $args['s'],
"fields" => array( "post_title", "post_content" ),
),
);
Please, let me know if it helps.
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
Thanks, @eugene-manuilov for the help!
Now we're trying to do the same for taxonomies e.g. post_tag, category etc. as well
This is our code:
$ep_formatted_args['query'] = array(
"query_string" => array(
"query" => $args['s'],
"fields" => array("post_title", "post_content"),
'taxonomies' => array( 'post_tag' ),
),
);
The taxonomy is present in the indexes but it's not fetching the results, we'll really appreciate your help on this
Thanks,
Rahul
Thanks, @eugene-manuilov for the help!
Now I'm trying to pull results for custom post_types, but isn't working out right (though we successfully tweaked the plugin's code to index custom post types and they are getting index now)
This is our code:
$es_query = new WP_Query( array(
'posts_per_page' => -1,
'ep_integrate' => true,
'autocomplete' => true,
'sites' => 'current',
's' => 'elast*',
'search_fields' => array(
'post_title',
'post_content',
//'post_excerpt',
//'taxonomies' => array('category'),
),
'post_status' => array('publish'),
'post_type' => array('post','photo-story','videos','collection'),
//'post_type' => 'any',
) );
We'll really appreciate your help on this and sorry for putting in more and more queries here :-)
Thanks,
Diwakar
Most helpful comment
Hey @diwakhat1603,
I am not sure 100% that it will work, but can you try to use
query_stringwith multiplefieldsinstead? Something like this:Please, let me know if it helps.
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html