在我的 Elasticsearch 查询中将逗号放在哪里

Where to put commas in my Elasticsearch query

我正在为我的 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 中那样在 MUST 中使用 3 个过滤器,或者您能否只在 中放置多个过滤器应该{}

在您的第二个查询版本中,放置尾随逗号通常用于指示其他参数。由于您只有一组参数,因此不需要额外的逗号。例如,在 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:

'

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
                        ]
                    ]
                ],
            ],