Elasticseach:匹配短语和术语

时间:2015-09-30 21:15:06

标签: elasticsearch

在elasticsearch中,我构建了一个过滤后的查询,以便查找包含两者一个词组和一个的文档。以下查询无效。它似乎返回查询数组中的项目的结果,但好像有一个'或'运营商应用。

编辑:由于我使用PHP,以下示例是一个php数组。

'query' => [
    'filtered' => [
        'query' => [
            'match' => [
                "post_content" => [
                    'query' => ['ambulance services', 'veteran'],
                    'operator' => 'and',
                    'type' => 'phrase'
                ]
            ]
        ],
        'filter' => [
            ...
        ]
    ]
]

1 个答案:

答案 0 :(得分:1)

我之前从未见过匹配查询的语法,您可以像之前那样为查询提供数组。但我确实在版本0.90中尝试了这一点,并看到它只返回第二个字符串的结果。所以使用JSON,我尝试的是这样的:

{
    "query" : {
        "filtered" : {
            "query" : {
                "match" : {
                    "post_content" : {
                        "query" : [ "test string 1", "test string 2" ]
                    }
                }
            }
        }
    }
}

如果您引用match query docsand运营商会确保所有条款都在post_content字段中,而不考虑这些条款的位置。我认为匹配查询只归结为一个bool查询,其中查询中的每个术语都由一个子句表示。因此,操作员并不完全按照您的意愿行事。

我认为以下内容适用于您想要的内容:

{
    "query" : {
        "bool" : {
            "should" : [
                {
                    "match" : {
                        "post_content" : {
                            "type" : "phrase",
                            "query" : "ambulance services"
                        }
                    }
                },
                {
                    "match" : {
                        "post_content" : {
                            "query" : "veteran"
                        }
                    }
                }
            ],
            "minimum_should_match" : 2
        }
    }
}
相关问题