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

时间:2017-09-27 19:17:37

标签: linux elasticsearch

我想在2个不同的字段(标题和描述)中搜索多个术语,运算符应为OR。这意味着如果任何记录包含任何这些术语(心脏,癌症),则应返回该记录。 这是我的代码:

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

但是,我收到了这个错误:

"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\"]}}}}

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我想出了如何解决它:

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