【问题标题】:How to use match_phrase with constant_score, terms如何将 match_phrase 与 constant_score、术语一起使用
【发布时间】:2017-06-09 09:40:42
【问题描述】:

这是我在 symfony2 中的查询。我想在此处添加“match_phrase”,但无论我在哪里添加,都会出错。

$params = [
    'index' => 'articles_v2',
    'type' => 'article',
    'body' => [
        "sort"  => [
            [ "date"  =>
                ["order" => "desc"]
            ],
        ],
        "from" => $fromId,
        "size"  => $newsPerPage,
        "query" => [
            "constant_score" => [
                "filter" => [
                    "bool" => [
                        "must" => [
                            ["terms" => [ "article.topics" => $topics ] ],
                            ["match_phrase" => ["article.bodytext" => [$search_phrase] ]]
                        ]
                    ]
                ]
            ]

        ]
    ]
];
$response = $client->search($params);

当我尝试运行此程序时,出现错误: 嵌套:QueryParsingException[[articles_v2] 没有为 [match_phrase]] 注册过滤器; }]","状态":400

那么这个 match_phrase 应该放在哪里呢? (我想得到类似 SQL LIKE '%xxxx%' 的结果)


我已更改查询。这次没有错误,但无论如何,没有过滤。

$params = [
    'index' => 'articles_v2',
    'type' => 'article',
    'body' => [
        "sort"  => [
            [ "date"  =>
                ["order" => "desc"]
            ],
        ],
        "from" => $fromId,
        "size"  => $newsPerPage,
        "query" => [
            "constant_score" => [
                "filter" => [
                    "bool" => [
                        "must" => [
                            ["terms" => [ "article.topics" => $topics ] ]
                        ]
                    ]
                ],
                "query" => [
                    "multi_match" => [
                        "query" =>    $search_phrase, 
                        "fields" => [ "title", "bodytext" ]
                    ]
                ]
            ]
        ]
    ]
];
$response = $client->search($params);

【问题讨论】:

    标签: symfony elasticsearch match-phrase


    【解决方案1】:

    解决方法是不要使用匹配的分数。

    您必须使用 query/bool/must 并且所有这些 ma​​tch 和内部的其他条件 must

    这里是代码。

    $params = [
        'index' => 'articles_v2',
        'type' => 'article',
        'size' => 50,
        'body' => [
            "sort"  => [
                [ "date"  =>
                    ["order" => "desc"]
                ],
            ],
            "from" => $fromId,
            "size"  => $newsPerPage,
            "query" => [
                "bool" => [
                    "must" => [
                        [
                            "match_phrase_prefix" => [
                                "_all" => [
                                    "query" => $search_phrase,
                                    "operator" => "and",
                                    "analyzer" => "analyzer_cs"
                                ]
                            ]
                        ],
                        ["terms" => [ "article.topics" => $topics ] ],
                        ["range" => [ "article.date" => [ "from" => $date_from,"to" => $date_till]]]
                    ]
                ]
            ]
        ]
    ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多