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

Posted

技术标签:

【中文标题】嵌套:QueryParsingException[[multi_match] 查询不支持 [fields]];【英文标题】:nested: QueryParsingException[[multi_match] query does not support [fields]]; 【发布时间】:2019-08-03 22:16:30 【问题描述】:

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

原因:SearchParseException[无法解析搜索源["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","分类法":["post_tag","category"],"查询":"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","事件"],"条款":"p ost_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":"大小":10000,"字段":"terms.region.slug","大小":"terms":"size":10000,"field":"terms.size.slug","地方":"terms":"size":10000,"field":"terms.places.slug","organizations":"terms":"size":10000,"field":" 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】:

你需要展平你的领域:

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

【讨论】:

感谢您的回复,将尝试并看看会发生什么。干杯

以上是关于嵌套:QueryParsingException[[multi_match] 查询不支持 [fields]];的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView和ScrollView嵌套使用

关于几个标签嵌套问题

vb怎么窗体嵌套

标准SQL嵌套查询语句

求助:获取嵌套块选择集lisp

Sass嵌套