【问题标题】:Elasticseach: Match phrase and termElasticsearch:匹配短语和术语
【发布时间】:2015-10-01 06:04:06
【问题描述】:

在 elasticsearch 中,我正在构建过滤查询以查找包含短语术语的文档。以下查询不起作用。它似乎返回了带有查询数组中的项目的结果,但好像应用了“或”运算符。

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

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

【问题讨论】:

  • 您能否发布您的地图和一些示例文档(您希望返回的那些)?

标签: elasticsearch


【解决方案1】:

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

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

如果您引用match query docsand 运算符确保所有术语都在 post_content 字段中,而不考虑术语的位置。我认为匹配查询归结为一个布尔查询,其中查询中的每个术语都由一个子句表示。所以运营商并没有完全按照你的意愿去做。

我认为以下内容可以满足您的需求:

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

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多