如何使用ElasticSearch在字符串字段中搜索确切的短语?

Xai*_*nez 4 full-text-search elasticsearch

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

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "operator": "and"
                    }
                }
            }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有包含这个短语和标题的文档,但我也得到了结果(文档),其中包含要搜索splitted的短语.我想要严格的搜索.如果没有任何具有此短语的文档,则不检索任何文档或仅检索具有该标题的文档.为什么操作员不起作用?

Raj*_*hna 7

你可以尝试下面使用类型短语.看到这里说,

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

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "type":  "phrase"
                    }
                }
            }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:我还没有尝试过.


pba*_*ski 5

第一个答案是好的但是对于ES v5使用"type":"phrase"[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"
                    }
                }
            }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)