ElasticSearch - 布尔查询过滤器问题

ElasticSearch - Problem with filter on Boolean query

布尔查询中的过滤器有问题。

我想应用基于 3 个字段的过滤器,其中至少有 1 个过滤器匹配:

$params = [
    'from' => 0,
    'size' => 25,
    'index' => 'document',
    'body' => [
        'query' => [
            'bool' => [
                'filter' => [
                    'bool' => [
                        'minimum_should_match' => 1,
                        'should' => [
                            'term' => [
                                'VISIBILITE' => 'T'
                            ],
                            'term' => [
                                'ECRITURE' => 'M'
                            ],
                            'term' => [
                                'LECTURE' => 'M'
                            ],
                        ]
                    ]
                ],
                'must' => [
                    [
                        'bool' => [
                            'should' => [ 
                                [
                                    'match' => [
                                        'OBJET' => $recherche,
                                    ]
                                ],
                            ] 
                        ]
                    ],
                ],
            ],
        ],
    ],
];

我没有得到这个查询的结果,但是我在索引中看到很多相关文档。


Opster Elasticsearch Ninja 测试:

比如你给我推荐的例子1,我有很多返回的结果。

但是,当我想在 OBJECT 字段上执行 must 查询时,我没有得到完全匹配的过滤器的相同结果。

这是一个例子:

  1. 仅使用 must 子句搜索
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1268,
            "relation": "eq"
        },
        "max_score": 13.616098,
        "hits": [
            {
                "_index": "document",
                "_type": "_doc",
                "_id": "26685",
                "_score": 13.616098,
                "_source": {
                    "NUMDOCUMENT": "26685",
                    "TYPEDOCUMENT": "Proc\u00e9dure",
                    "OBJET": "Proc\u00e9dure d'importation des index dans Marco 2",
                    "MOTCLES": "",
                    "LECTURE": "S",
                    "VISIBILITE": "T", // Must match on second search
                    "ECRITURE": "M" // Must match on second search
                }
            }
        ]
    }
}
  1. 使用 must 子句和过滤器进行搜索
{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 0,
        "hits": [
            {
                "_index": "document",
                "_type": "_doc",
                "_id": "431",
                "_score": 0,
                "_source": {
                    "NUMDOCUMENT": "431",
                    "TYPEDOCUMENT": "Document",
                    "OBJET": "Diagnostic informatique SAFC",
                    "LECTURE": "M",
                    "VISIBILITE": "T",
                    "ECRITURE": "M"
                }
            }
        ]
    }
}

不再是最先出现的同一个文档(虽然文档对应过滤器)。就好像搜索过滤器会影响搜索的分数和相关性一样。

问题似乎与您的 bool 查询有关,如果您在顶层查看您的查询,您有两个构造

  1. 具有 3 个应该条件的过滤块,其中至少有 1 个应该匹配,这将过滤即减少下一个 must 子句将在其上执行的文档集.

  2. 必须阻止,我怀疑在第 1 步的缩减文档集上没有匹配任何内容,这导致您的查询 return 没有。

为了调试问题,您应该独立尝试第一个块,然后再合并以查看是否得到结果,因为您的 must 块没有正确的数据,我我创建了下面的示例,它显示了您是否有正确的数据,它 return 是数据:

{
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "VISIBILITE": "T"
                    }
                },
                {
                    "term": {
                        "ECRITURE": "T"
                    }
                },
                {
                    "term": {
                        "LECTURE": "T"
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}

和搜索查询结果,显示匹配文档_source

"hits": [
            {
                "_index": "minshouldmatch",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.5686158,
                "_source": {
                    "VISIBILITE": "T", 
                    "ECRITURE": "T",
                    "LECTURE": "T"
                }
            },
            {
                "_index": "minshouldmatch",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.18232156,
                "_source": {
                    "VISIBILITE": "T", // note even only 1 condition matches still it comes in SR
                    "ECRITURE": "M",
                    "LECTURE": "M"
                }
            }
        ]

我找到了解决方案。我忘记了应该过滤的钩子。

不好:

'bool' => [
                'filter' => [
                    'bool' => [
                        'minimum_should_match' => 1,
                        'should' => [
                            'term' => [
                                'VISIBILITE' => 'T'
                            ],
                            'term' => [
                                'ECRITURE' => 'M'
                            ],
                            'term' => [
                                'LECTURE' => 'M'
                            ],
                        ]
                    ]
                ],

好:

'bool' => [
                'filter' => [
                    'bool' => [
                        'minimum_should_match' => 1,
                        'should' => [[ // Double hook
                            'term' => [
                                'VISIBILITE' => 'T'
                            ],
                            'term' => [
                                'ECRITURE' => 'M'
                            ],
                            'term' => [
                                'LECTURE' => 'M'
                            ],
                        ]]
                    ]
                    
                ],