从2.4升级到5后,Perl弹性查询被破坏

时间:2017-10-04 20:02:58

标签: perl elasticsearch

正如标题所示,我正在升级到ES5。我原来的ES查询看起来像:

my $response = $elastic->do_request_new( 
        {
            query       => {
                filtered    => {
                    filter  => {
                        or  => [
                            { term  => { _type  => { value => "some_val1" } } },
                            { term  => { _type  => { value => "some_val2" } } },
                        ]
                    },
                    query   => {
                        query_string    => {
                            query   => $qstring,
                            rewrite => "scoring_boolean", 
                            analyze_wildcard => "true",
                        }
                    }
                }
            },

            sort    => [ qw(_score) ],
            size => 50,
        },
    ); 

我的更新看起来像:

my $response = $elastic->do_request_new(
    {
        query       => {
            bool    => {
                should  => [
                    { term  => { _type  => { value => "some_val1" } } },
                    { term  => { _type  => { value => "some_val2" } } },
                ],
                must   => {
                    query_string    => {
                        query   => $qstring,
                        rewrite => "scoring_boolean", 
                        analyze_wildcard => "true",
                    }
                }
            }
        },
        sort    => [ qw(_score) ],
        size => 50,
    },
); 

但是,在我的弹性数据库中搜索确切的字符串时,我返回零结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

关于可能发生的事情的任何想法?我的猜测是我的查询结构错了。谢谢!

1 个答案:

答案 0 :(得分:1)

更新:以下固定查询:

my $response = $elastic->do_request_new(
    {
        query   => {
                bool    => {
                    must   => [
                        {
                            query_string => {
                                query            => $qstring,
                                rewrite          => "scoring_boolean",

                                analyze_wildcard => "true",
                            },
                        }
                    ],
                    filter => {
                        bool => {
                            should => [
                                { term => { _type => { value => "some_val1" } } },
                                { term => { _type => { value => "some_val2" } } },

                            ],
                        },
                    },
                },
            },
        sort    => [ qw(_score) ],
        size => 50,
    },
);