0

im new to Elastic press and got some problems on Wordpress multi-network site and got this exception when trying to execute WP-query:

Caused by: SearchParseException[failed to parse search source [{"from":0,"size":3,"sort":[{"_score":{"order":"desc"}}],"query":{"bool":{"should":[{"multi_match":{"query":"test","type":"phrase","fields":{"0":"post_title","1":"post_content","2":"post_excerpt","3":"author_name","taxonomies":["post_tag","category"]},"boost":4,"fuzziness":0,"operator":"and"}},{"multi_match":{"query":"test","fields":{"0":"post_title","1":"post_content","2":"post_excerpt","3":"author_name","taxonomies":["post_tag","category"]},"boost":2,"fuzziness":0}},{"multi_match":{"fields":{"0":"post_title","1":"post_content","2":"post_excerpt","3":"author_name","taxonomies":["post_tag","category"]},"query":"test","fuzziness":0,"operator":"and"}}]}},"post_filter":{"bool":{"must":[{"terms":{"post_type.raw":["post","page","attachment","events"]}},{"terms":{"post_status":["publish","acf-disabled"]}}]}},"aggs":{"terms":{"filter":{"bool":{"must":[{"terms":{"post_type.raw":["post","page","attachment","events"]}},{"terms":{"post_status":["publish","acf-disabled"]}}]}},"aggs":{"category":{"terms":{"size":10000,"field":"terms.category.slug"}},"post_tag":{"terms":{"size":10000,"field":"terms.post_tag.slug"}},"post_format":{"terms":{"size":10000,"field":"terms.post_format.slug"}},"key_themes":{"terms":{"size":10000,"field":"terms.key_themes.slug"}},"themes":{"terms":{"size":10000,"field":"terms.themes.slug"}},"story_labels":{"terms":{"size":10000,"field":"terms.story_labels.slug"}},"asset-class":{"terms":{"size":10000,"field":"terms.asset-class.slug"}},"region":{"terms":{"size":10000,"field":"terms.region.slug"}},"size":{"terms":{"size":10000,"field":"terms.size.slug"}},"places":{"terms":{"size":10000,"field":"terms.places.slug"}},"organizations":{"terms":{"size":10000,"field":"terms.organizations.slug"}},"people":{"terms":{"size":10000,"field":"terms.people.slug"}}}}}}]]; nested: QueryParsingException[[multi_match] query does not support [fields]];

example of the elastic press code that cause this issue:

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ElasticpressFilters
 *
 * @author serafim_inplayer
 */
class ElasticpressFilters
{

public function __construct()
{
    /*
     *  Hook into ep_formatted_args to add wighting arguments.
     */
    add_filter('ep_formatted_args', [$this, 'set_es_search_args'], 300, 2);
}

function set_es_search_args($formatted_args, $args)
{
    $search_fields = [
        'post_title',
        'post_excerpt',
        'post_content'
    ];

    if (isset($args['search_fields'])) {
        $search_fields = $args['search_fields'];
    }

    if (isset($args['exact']) && $args['exact'] == true) {
        $query = [
            'multi_match' => [
                'query' => '',
                'type' => 'phrase',
                'fields' => $search_fields,
                'boost' => apply_filters('ep_match_phrase_boost', 4, $search_fields, $args),
                'operator' => 'and',
                'fuzziness' => 0,
            ]
        ];

        if (!empty($args['s'])) {
            unset($formatted_args['query']);
            $query['multi_match']['query'] = $args['s'];
            $formatted_args['query'] = $query;
        }
    } else {

        $query = [
            'bool' => [
                'should' => [
                    [
                        'multi_match' => [
                            'query' => '',
                            'type' => 'phrase',
                            'fields' => $search_fields,
                            'boost' => apply_filters('ep_match_phrase_boost', 4, $search_fields, $args),
                            'fuzziness' => 0,
                            'operator' => 'and',
                        ]
                    ],
                    [
                        'multi_match' => [
                            'query' => '',
                            'fields' => $search_fields,
                            'boost' => apply_filters('ep_match_boost', 2, $search_fields, $args),
                            'fuzziness' => 0,
                        ]
                    ],
                    [
                        'multi_match' => [
                            'fields' => $search_fields,
                            'query' => '',
                            'fuzziness' => 0,
                            'operator' => 'and',
                        ],
                    ]
                ],
            ],
        ];

        /**
         * We are using ep_integrate instead of ep_match_all. ep_match_all will be
         * supported for legacy code but may be deprecated and removed eventually.
         *
         * @since 1.3
         */
        if (!empty($args['s'])) {
            $query['bool']['should'][2]['multi_match']['query'] = $args['s'];
            $query['bool']['should'][1]['multi_match']['query'] = $args['s'];
            $query['bool']['should'][0]['multi_match']['query'] = $args['s'];
            $formatted_args['query'] = apply_filters('ep_formatted_args_query', $query, $args);
        } else if (!empty($args['ep_match_all']) || !empty($args['ep_integrate'])) {
            $formatted_args['query']['match_all'] = ['boost' => 1];
        }
    }

    /*
     * Exclude posts with defined categories by slugs in array
     */

    if (!empty($args['ep_integrate']) && !empty($args['ep_exclude_categories'])) {
        $formatted_args['post_filter']['bool']['must'][0]
            ['bool']['should'][0]['bool']['must_not'][]
            ['terms']['terms.category.slug'] = $args['ep_exclude_categories'];
    }

    /*
     * If ep_exists_key_themes is true will display all posts with key themes
     */
    if (!empty($args['ep_integrate']) && !empty($args['ep_exists_key_themes'])) {
        $formatted_args['post_filter']['bool']['must'][0]
            ['bool']['should'][1]['bool']['must'][]
            ['exists']['field'] = 'terms.key_themes.term_id';
    }

    return $formatted_args;
}

The problem that cause this is when $search_fields got this values (array inside array):

$search_fields = array(
            'post_title',
            'post_content',
            'post_excerpt',
            'author_name',
            'taxonomies' => array(
                'post_tag',
                'category',
            )
        );

Thanks for Help.

1 Answer 1

0

You need to flatten your fields :

 $search_fields = array(
                'post_title',
                'post_content',
                'post_excerpt',
                'author_name',
                'taxonomies.post_tag',
                'taxonomies.category'
   );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, will try with that and see what will happen. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.