Elasticsearch:使用条件过滤器搜索文档

时间:2016-02-16 13:12:43

标签: elasticsearch full-text-search

我的索引中有两个文档(相同类型):

{
    "first_name":"John",
    "last_name":"Doe",
    "age":"24",
    "phone_numbers":[
        {
            "contract_number":"123456789",
            "phone_number":"987654321",
            "creation_date": ...
        },
        {
            "contract_number":"123456789",
            "phone_number":"012012012",
            "creation_date": ...
        }
    ]
}

{
    "first_name":"Roger",
    "last_name":"Waters",
    "age":"36",
    "phone_numbers":[
        {
            "contract_number":"546987224",
            "phone_number":"987654321",
            "creation_date": ...,
            "expired":true
        },
        {
            "contract_number":"87878787",
            "phone_number":"55555555",
            "creation_date": ...
        }
    ]
}

客户希望执行全文搜索。好的,这里没问题

我的问题: 在此全文搜索中,有时用户将按 phone_number 进行搜索。在这种情况下,有一个参数,如expired=true

示例: 第一个客户搜索请求:"987654321" expired缺席或设置为false

- >结果:仅第一个文档

第二次客户搜索请求:"987654321" expired设置为true

- >结果:两个文件

我怎样才能做到这一点?

这是我的映射:

{
"user": {
    "_all": {
        "auto_boost": true,
        "omit_norms": true
    },
    "properties": {
        "phone_numbers": {
            "type": "nested",
            "properties": {
                "phone_number": {
                    "type": "string"
                },
                "creation_date": {
                    "type": "string",
                    "index": "no"
                },
                "contract_number": {
                    "type": "string"
                },
                "expired": {
                    "type": "boolean"
                }
            }
        },
        "first_name":{
            "type": "string"
        },
        "last_name":{
            "type": "string"
        },
        "age":{
            "type": "string"
        }
    }
}

}

谢谢!

MC

编辑:

我尝试了这个查询:

{
"query": {
    "filtered": {
        "query": {
            "query_string": {
                "query": "987654321",
                "analyze_wildcard": "true"
            }
        },
        "filter": {
            "nested": {
                "path": "phone_numbers",
                "filter": {
                    "bool": {
                        "should":[
                           {
                             "bool": {
                                "must": [
                                  {
                                    "term": {
                                        "phone_number": "987654321"
                                    }
                                },
                                {
                                    "missing": {
                                        "field": "expired"
                                    }
                                }
                              ]
                            }       
                          },
                          {
                            "bool": {
                                "must_not": [
                                    {
                                        "term": {
                                            "phone_number": "987654321"
                                        }
                                    }
                                ]
                            }       
                         }
                        ]
                    }
                }
             }
        }
    }
}}

但我得到两个文件而不是只获得第一个

1 个答案:

答案 0 :(得分:0)

你非常接近。尝试使用mustshould的组合,must子句确保phone_number与搜索值匹配,should子句确保{ {1}}字段缺失或设置为expired。例如:

false

我使用您的映射和示例文档运行此查询,并按预期返回John Doe的一个文档。