简体   繁体   English

使用elasticsearch在多个字段中查询多个术语

[英]query multiple terms in multiple fields using elasticsearch

I want to search for multiple terms in 2 different fields (title, and description), the operator should be OR. 我想在2个不同的字段(标题和描述)中搜索多个术语,运算符应为OR。 Meaning that if any records contains any of these terms (heart, cancer) then that record should be returned. 这意味着,如果任何记录包含以下任何术语(心脏,癌症),则应返回该记录。 Here is my code: 这是我的代码:

curl -XGET 'localhost:9200/INDEXED REPOSITORY/_search?pretty' -H 'Content-
Type: application/json' -d'{"query" : {"constant_score" : {"filter" : {"terms" 
: {"description","title" : ["heart","cancer"]}}}}}'

But, I get this error: 但是,我得到这个错误:

"error" : "SearchPhaseExecutionException[Failed to execute phase [query], 
all shards failed; shardFailures {[6hWIW7xlSbSqKi4dNg_1bg][geo_021017cde]
[0]: SearchParseException[[geo_021017cde][0]: from[-1],size[-1]: Parse 
Failure [Failed to parse source [{\"query\" : {\"constant_score\" : 
{\"filter\" : {\"terms\" : {\"description\",\"title\" : 
[\"heart\",\"cancer\"]}}}}

Am I missing anything? 我有什么想念的吗?

I figured out how to resolve it: 我想出了解决方法:

{
    "query": {
        "constant_score": {
            "filter": {
                "bool": {
                    "should": [
                        {
                            "terms": {
                                "description": [
                                    "heart",
                                    "cancer"
                                ]
                            }
                        },
                        {
                            "terms": {
                                "title": [
                                    "heart",
                                    "cancer"
                                ]
                            }
                        }
                    ]
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM