在我的Elasticsearch查询中放置逗号的位置

时间:2015-02-25 15:50:09

标签: java php mysql laravel elasticsearch

我在我的Laravel应用程序中使用这个Elasticquent ES包。

但我不知道应该在查询字符串中放置逗号。据我所知,如果内部数组中有多个元素,我必须使用逗号。但是所有数组都需要逗号。两个查询似乎都很好,这就是为什么我想知道。我想做最好的练习,这就是我要问的原因。

'filtered' => [
                'query' => [
                    'match' => ['title' => Input::get('query')]
                ],
                'filter'=> [
                    'bool' => [
                        'must' => [
                            ['term' => [ 'type' =>  1] ],
                            ['term' => [ 'state' =>  22] ],
                            ['term' => [ 'city' => ] ],
                            [ 'range' => [
                                    'price' => [
                                        'gte' => ,
                                        'lte' => ,
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ],

还有更多的逗号(,)

'filtered' => [
                'query' => [
                    'match' => ['title' => Input::get('query')]
                ],
                'filter'=> [
                    'bool' => [
                        'must' => [
                            ['term' => [ 'type' =>  1] ],
                            ['term' => [ 'state' =>  22] ],
                            ['term' => [ 'city' => ] ],
                            [ 'range' => [
                                    'price' => [
                                        'gte' => ,
                                        'lte' => ,
                                    ]
                                ]
                            ]
                        ],
                    ],
                ],
            ],

他们似乎都工作。以下是使用此软件包的完整查询示例:

$posts = Post::searchByQuery([
  'filtered' => [
    'filter' => [
      'not' => [
        'terms' => ['title' => ['impedit', 'voluptatem']]
      ]
    ],
    'query' => [
      "bool" => [
        'must' => [
          'multi_match' => [
            'query' => Input::get('query', ''),
            'fields' => [ "title^2", "content"]
          ],
        ],
        "should" => [
          'match' => [
            'tags' => [
              "query" => Input::get('query', ''),
              "type" => "phrase"
            ]
          ]
        ]
      ]
    ],
  ],
]);

此外,是否可以像在查询#1和#2中一样使用 3过滤器,或者只能在中添加多个过滤器{}

1 个答案:

答案 0 :(得分:0)

在查询的第二个版本中,使用尾随逗号通常用于指示其他参数。由于您只有一组参数,因此不需要其他逗号。例如,在filter =>块之后放置一个逗号表示在过滤后会有其他语句。另外,为什么要为解析器留下额外的字符?

 'filtered' => [
                    'query' => [
                        'match' => ['title' => Input::get('query')]
                    ],
                    'filter'=> [
                        'bool' => [
                            'must' => [
                                ['term' => [ 'type' =>  1] ],
                                ['term' => [ 'state' =>  22] ],
                                ['term' => [ 'city' => ] ],
                                [ 'range' => [
                                        'price' => [
                                            'gte' => ,
                                            'lte' => ,
                                        ]
                                    ]
                                ]
                            ], // <=== Other operators to filter on?
                        ],  // <=== Other operations other than query and filter?
                    ],
                ],

对于您的第二个问题,根据docs,可以将多个过滤器包装在bool过滤器中来应用:

filtered' => [
                'query' => [
                    'match' => ['title' => Input::get('query')]
                ],
                'filter'=> [
                    'bool' => [
                        'must' => [
                            ['term' => [ 'type' =>  1] ],
                            ['term' => [ 'state' =>  22] ],
                            ['term' => [ 'city' => ] ],
                            [ 'range' => [
                                    'price' => [
                                        'gte' => ,
                                        'lte' => ,
                                    ]
                                ]
                            ]
                        ],
                        'must_not' => [
                          ['term' => ['state' => 23] ]  // <=== Additional must clause
                        ]
                    ]
                ],
            ],