Author: Mikael Mattsson (mikael@wallmanderco.se)
Inheritance: extends Client, use trait Wallmander\ElasticsearchIndexer\Model\Query\BuilderTrait
/**
 * @param null|WP_Query $wpQuery
 *
 * @return \Wallmander\ElasticsearchIndexer\Model\Query
 */
function es_query($wpQuery = null)
{
    $esq = new Query();
    if ($wpQuery instanceof WP_Query) {
        $esq->applyWpQuery($wpQuery);
    }
    return $esq;
}
 /**
  * Search custom fields as well as content.
  * Replaces WC_Admin_Post_Types::Replaces shop_order_search_custom_fields.
  *
  * @param \Wallmander\ElasticsearchIndexer\Model\Query $query
  */
 public static function actionOrderSearch(Query $query)
 {
     global $pagenow;
     $wpQuery = $query->wp_query;
     if ('edit.php' != $pagenow || !$wpQuery->get('s') || $wpQuery->get('post_type') != 'shop_order') {
         return;
     }
     $search = str_replace('Order #', '', $wpQuery->get('s'));
     $searchFields = apply_filters('woocommerce_shop_order_search_fields', ['_billing_first_name', '_billing_last_name', '_shipping_first_name', '_shipping_last_name', '_billing_company', '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_postcode', '_billing_country', '_billing_state', '_billing_email', '_billing_phone', '_shipping_address_1', '_shipping_address_2', '_shipping_city', '_shipping_postcode', '_shipping_country', '_shipping_state']);
     foreach ($searchFields as $key => $value) {
         $searchFields[$key] = 'post_meta.' . $value;
     }
     $searchFields[] = 'order_item_names';
     $searchFields[] = '_id';
     $query->setQuery(['bool' => ['should' => [['multi_match' => ['fields' => $searchFields, 'type' => 'phrase_prefix', 'analyzer' => 'esi_simple_analyzer', 'query' => $search]], ['multi_match' => ['fields' => $searchFields, 'type' => 'cross_fields', 'operator' => 'and', 'analyzer' => 'esi_simple_analyzer', 'query' => $search]]]]]);
     if (!$wpQuery->get('orderby')) {
         $query->setSort('post_date', 'desc');
     }
 }
 /**
  * Save the elasticsearch query.
  *
  * @param Query    $query
  * @param WP_Query $wpQuery
  */
 public static function actionAfterFormatArgs(Query $query, WP_Query $wpQuery)
 {
     ProfilerModel::addElasticsearchQueryArgs($query->getArgs(), $wpQuery->query_vars);
 }
 /**
  * @param array     $posts
  * @param \WP_Query &$query
  *
  * @return array
  */
 public static function filterThePosts($posts, WP_Query $query)
 {
     if (apply_filters('esi_skip_query_integration', false, $query)) {
         return $posts;
     }
     if (empty($query->is_elasticsearch_compatible)) {
         return $posts;
     }
     return Query::fromWpQuery($query)->getPosts();
 }
 public static function argDateQuery(Query $query, $value, &$q)
 {
     $query->bool(function (Query $query) use($value, $q) {
         foreach ($value as $dq) {
             $column = !empty($dq['column']) ? $dq['column'] : 'post_date';
             $inclusive = !empty($dq['inclusive']);
             foreach ($dq as $key => $value) {
                 switch ($key) {
                     case 'before':
                         $date = static::buildDatetime($value, $inclusive);
                         $comparator = 'lt';
                         if ($inclusive) {
                             $comparator .= 'e';
                         }
                         $query->where($column, $comparator, $date);
                         break;
                     case 'after':
                         $date = static::buildDatetime($value, !$inclusive);
                         $comparator = 'gt';
                         if ($inclusive) {
                             $comparator .= 'e';
                         }
                         $query->where($column, $comparator, $date);
                         break;
                     case 'week':
                     case 'w':
                         $query->where($column . '_object.week', $value);
                         break;
                     case 'year':
                     case 'month':
                     case 'dayofyear':
                     case 'day':
                     case 'dayofweek':
                     case 'dayofweek_iso':
                     case 'hour':
                     case 'minute':
                     case 'second':
                         $query->where($column . '_object.' . $key, $value);
                         break;
                 }
             }
         }
     }, !empty($value['relation']) ? $value['relation'] : 'and');
 }