【问题标题】:ElasticSearch 7 - combine filtersElasticSearch 7 - 组合过滤器
【发布时间】:2019-05-05 21:20:03
【问题描述】:

我使用 ES 7 和 Laravel 实现,我想结合范围和术语匹配,根据文档,我这样做了:

    $items = $client->search([
        'index' => $instance->getSearchIndex(),
        'type' => $instance->getSearchType(),
        'body' => [
            'size' => 50,
            'query' => [
                'bool' => [
                    'must' => [
                        'multi_match' => [
                            'fields' => config('elasticsearch.fields'),
                            'query' => $query,
                        ],
                    ],
                    'filter' => [
                        'bool' => [
                            'must' => [
                                'range' => [
                                    'note' => [
                                        'gte' => config('elasticsearch.note_minimum')
                                    ]
                                ],
                                'term' => [
                                    'type_video_id' => 5
                                ],
                            ],
                        ],
                    ],
                ]
            ],
        ],
    ]);

得到了这个错误:

"parsing_exception","re​​ason":"[range] 格式错误的查询,预期 [END_OBJECT] 但找到了 [FIELD_NAME]

我只找到了 ES 2 关于组合查询的文档和示例,有什么变化吗?

我希望我的查询匹配字段,并根据过滤器进行过滤。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这是正确的做法:

    $items = $client->search([
        'index' => $instance->getSearchIndex(),
        'type' => $instance->getSearchType(),
        'body' => [
            'size' => 50,
            'query' => [
                'bool' => [
                    'must' => [
                        'multi_match' => [
                            'fields' => config('elasticsearch.fields'),
                            'query' => $query,
                        ]
                    ],
                    'filter' => [
                        [
                            'range' => [
                                'note' => [
                                    'gte' => config('elasticsearch.note_minimum')
                                ]
                            ]
                        ],
                        [
                            'term' => [
                                'type_video_id' => 5
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]);
    

    【讨论】:

      【解决方案2】:

      我没有办法对此进行测试,但我看到了几个需要花括号的括号。另外,你不需要在那些“配置”之前需要 $ 吗?

      {
        "query" => {
          "bool" => {
            "must" => [
              {
                "multi_match" => {
                  "query" => $query,
                  "fields" => $config('elasticsearch.fields')
                }
              }
            ],
            "filter" => {
              {
                "range" => {
                  "note" => {
                    "gte" => $config('elasticsearch.note_minimum')
                  }
                }
              },
              {
                "term" => {
                  "type_video_id" => {
                    "value" => "5"
                  }
                }
              }
            }
          }
        }
      }
      

      如果这不起作用,你能粘贴你的变量渲染后字符串的样子吗?

      【讨论】:

      • PHP 中的方括号用于创建关联数组,那些花括号不起作用,恕我直言
      • 是的,对不起,我用了 PHP 的方式,但这就像用花括号替换括号
      猜你喜欢
      • 1970-01-01
      • 2015-09-17
      • 2014-02-02
      • 2020-12-02
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多