MySQL 全文搜索 "AND" bool 模式到 ElasticSearch

标签 mysql elasticsearch

我最近尝试从 MySQL 全文搜索迁移到 ElasticSearch,我对翻译一些查询有点困惑。

我有这个问题。

 "SELECT * FROM Books WHERE MATCH (description) AGAINST ('+Harry +Potter' IN BOOLEAN MODE)"

这意味着“Harry”和“Potter”都必须出现在描述栏中,无论顺序或位置如何。 (为了举例,请假设“Harry”和“Potter”可以相互独立。)

我用 ElasticSearch 试过了

{
    "query": {
        "query_string": {
            "query": "Harry Potter",
            "fields": ["description"]
        }
    }
}

但它仍然会给出一些只包含“Harry”或“Potter”的结果。

这个我也试过,

{
    "query": {
        "bool": {
           "must" : {
               "term" : { "description" : "Harry Potter" }
           }
        }
    }
}

这个返回所有包含“Harry Potter”的结果,而不是“Harry Bla Bla Bla Potter”和“Potter Bla Bla Bla Harry”。

返回与上述 MySQL 查询相同结果的最简单(或者可能也是最快)的 ElasticSearch 查询是什么。

更新

我刚找到这样的东西

{
    "query": {
        "match" : {
            "description" : {
                "query" : "Harry Potter",
                "operator" : "and"
            }
         }
    }
}

结果似乎是正确的。但是还有其他更常见的方式吗?

最佳答案

Vis 回答的更多信息

如果你想“Harry Potter”也匹配“Harry blabla Potter”,你可以将query_string的phrase_slop调整为> 0,这是匹配项之间允许的距离: - 默认为 0 意味着需要精确的短语匹配,搜索阶段之间没有其他术语 - 设置为 1 意味着在它之间允许 1 个术语,所以“Harry blalal Potter”将匹配,但“Harry blabla bloblo Potter”不会匹配。 - ...

关于MySQL 全文搜索 "AND" bool 模式到 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18825246/

相关文章:

php - 在php中提交表单后如何从下拉列表中获取选定的值

mysql - 重新排序/重置自动增量主键

elasticsearch - 为什么在Elasticsearch中的多个查询文档中,同一术语的idf值会有所不同?

php - MySql 表架构性能问题

mysql - 排除相关记录 - 关联犯罪

sorting - Elasticsearch根据多个字段排序,然后按随机种子排序

elasticsearch - Elasticsearch-如何索引计算字段?

python - Elasticsearch 不敏感搜索重音

mysql - 设计: use independent or correlated status for parent and child

php - mysql select查询的where子句顺序的改变是否会提高性能?