【问题标题】:How to search exact phrase in string field with ElasticSearch?如何使用 ElasticSearch 在字符串字段中搜索确切的短语?
【发布时间】:2017-06-16 11:55:26
【问题描述】:

我想在文档中搜索“社交网络营销”。全部一起。但是我继续得到单词分开的结果。我有以下 DSL 查询:

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "operator": "and"
                    }
                }
            }]
        }
    }
}

我没有包含此短语和标题的文档,但我也得到了包含要搜索的短语单词的结果(文档)。我要严格搜索。如果没有任何文档具有此短语,则不要检索任何文档或仅检索具有该标题的文档。 为什么operator and不起作用?

【问题讨论】:

  • "and" 表示给我包含所有这些单词的文件。使用下面答案中提到的“match_phrase”查询。它会给你正确的结果。

标签: elasticsearch full-text-search


【解决方案1】:

你能像下面这样使用类型短语吗?请参阅here 它说,

query 首先分析查询字符串以生成术语列表。它 然后搜索所有术语,但只保留包含 所有搜索词,相对于彼此在相同的位置

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "type":  "phrase"
                    }
                }
            }]
        }
    }
}

P.S:我还没试过。

【讨论】:

  • 好的,"type": "phrase" 似乎效果很好。稍后我将对其进行深入测试。
  • @XairoMartínez 同时你可以接受答案:-)
【解决方案2】:

第一个答案很好,但对于使用 "type":"phrase" 的 ES v5 将在标题中返回 [WARN ][org.elasticsearch.deprecation.common.ParseField] Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]
所以正确的查询应该包含match_phrase:

 {
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match_phrase": {
                    "content": {
                        "query": "Marketing in social networks"
                    }
                }
            }]
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多