-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Elasticsearch Queries
There is a lot of power in Elasticsearch that can only be leveraged by writing Elasticsearch DSL and running direct queries against an index. This plugin provides users with the ability to execute fully custom queries directly against Elasticsearch through the adapter (in addition to searching via a search query (/?s=) or ES_WP_Query) where DSL is provided instead of WP_Query-like arguments.
These queries can be executed by using the search() method. This method utilizes the adapters built-in Elasticsearch query function to execute a query with custom DSL to the Elasticsearch server. Below is an example using search() to query the Elasticsearch index for five posts that have titles similar (leveraging ES's more_like_this) to the "current" post's title.
$es_args = [
'profile' => false,
'from' => 0,
'size' => 5,
'query' => [
'function_score' => [
'query' => [
'more_like_this' => [
'like' => [
'_id' => get_the_ID(),
],
'fields' => [
'post_title',
],
'min_term_freq' => 1,
'max_query_terms' => 12,
'min_doc_freq' => 1,
],
],
],
],
'post_filter' => [
'bool' => [
'must' => [
[
'terms' => [
'post_type.raw' => [
'post',
],
],
],
[
'terms' => [
'post_status' => [
'publish',
],
],
],
],
],
],
];
$results = elasticsearch_extensions()->search( $es_args );