嵌套:QueryParsingException [[multi_match]查询不支持[fields]];

时间:2019-03-13 09:34:05

标签: php wordpress elasticsearch

Elastic press的新手,在Wordpress多网络站点上遇到了一些问题,并在尝试执行WP-query时出现此异常:

  

原因:SearchParseException [无法解析搜索源[{“ from”:0,“ size”:3,“ sort”:[{“ _ score”:{“ order”:“ desc”}}]]“,”查询”:{“布尔”:{“应该”:[{“ multi_match”:{“查询”:“测试”,“类型”:“短语”,“字段”:{“ 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”,“分类法”:[“ post_tag”,“类别”]}},“查询“:” test“,” fuzziness“:0,” operator“:” and“}}]}},” post_filter“:{” bool“:{” must“:[{” terms“:{” post_type.raw “:[” post“,” page“,”附件“,”事件“]}}},{” terms“:{” post_status“:[” publish“,” acf-disabled“]}}]}}}},”,“ aggs“:{” terms“:{” filter“:{” bool“:{” must“:[{” terms“:{” post_type.raw“:[” post“,” page“,” attachment“,”事件“]}},{”条款“:{” p ost_status“:[” publish“,” acf-disabled“]}}}}},” ags“:{” 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“}}},”地点”:{“条款”:{“大小”:10000,“字段”:“ terms.places.slug”}},“组织”:{“条款”:{“大小”:10000,“字段”:” terms.organizations.slug“}},” people“:{” terms“:{” size“:10000,” field“:” terms.people.slug“}}}}}}]]]]]]]]]]];嵌套:QueryParsingException [[multi_match]查询不支持[fields]];

导致此问题的弹性新闻代码示例:

<?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;
}

导致此问题的原因是$ search_fields获得此值(数组内的数组):

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

感谢帮助。

1 个答案:

答案 0 :(得分:0)

您需要拼合您的字段:

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