弹性搜索聚合和复杂查询

时间:2020-06-25 11:12:54

标签: elasticsearch elasticsearch-query

我已经创建了索引

PUT ten2
{
    "mappings": {
        "documents": {
            "properties": {
                "title": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                },"uid": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                },
                "publish_details": {
                    "type": "nested",
                    "properties": {
                        "environment": {
                            "type": "keyword"
                        },
                        "locale": {
                            "type": "keyword"
                        },
                        "time": {
                            "type": "date"
                        },
                        "version": {
                            "type": "integer"
                        }
                    }
                }
            }
        }
    }
}

并向其中添加文档。这是文档列表:

   [{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt69b62b48bbed1fb6_en-us",
    "_source": {
        "publish_details": [{
                "environment": "blt603fe91adbdcff66",
                "time": "2020-06-24T12:11:25.276Z",
                "locale": "en-us",
                "user": "bltaadab2f531206e9d",
                "version": 1
            },
            {
                "environment": "blt603fe91adbdcff66",
                "time": "2020-06-24T12:11:25.276Z",
                "locale": "hi-in",
                "user": "bltaadab2f531206e9d",
                "version": 1
            }
        ],
        "title": "Entry 1",
        "uid": "blt69b62b48bbed1fb6"
    }
},
{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt69b62b48bbed1fb6_mr-in",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:12:35.467Z",
            "locale": "mr-in",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 3",
        "uid": "blt69b62b48bbed1fb6"
    }
},
{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt4044c5198122a3ed_en-us",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        },{
            "environment": "blt603fe91adbdcff6690",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 10",
        "uid": "blt4044c5198122a3ed"
    }
}

] 我想要以下结果

    [
 {
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt4044c5198122a3ed_en-us",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        },{
            "environment": "blt603fe91adbdcff6690",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 10",
        "uid": "blt4044c5198122a3ed"
    }
}

]

我正在使用以下查询来获取结果

GET ten2/_search
{
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "must_not": [{
                        "bool": {
                            "must": [{
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                        "publish_details.environment": "blt603fe91adbdcff66"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "en-us"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "hi-in"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "mr-in"
                                        }
                                    }
                                }
                            }]
                        }
                    }]
                }
            }
        }
    }
}

请帮助我查询以获得期望的结果。前两个具有相同uid的dicuemtns仅publish_details.locale是不同的。我使用must_not内的must查询来获取结果,当前我正在获取所有三个文档,但是我只想要最后一个。我有一百万个文件。

1 个答案:

答案 0 :(得分:0)

要了解有关布尔查询的更多信息,请参阅此官方documentation

为您的映射,索引数据和搜索查询添加一个有效的示例

搜索查询:

"hits": [
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.53899646,
            "_source": {
                "publish_details": [
                    {
                        "environment": "blt603fe91adbdcff66",
                        "time": "2020-06-24T12:10:46.430Z",
                        "locale": "en-us",
                        "user": "bltaadab2f531206e9d",
                        "version": 1
                    },
                    {
                        "environment": "blt603fe91adbdcff6690",
                        "time": "2020-06-24T12:10:46.430Z",
                        "locale": "en-us",
                        "user": "bltaadab2f531206e9d",
                        "version": 1
                    }
                ],
                "title": "Entry 10",
                "uid": "blt4044c5198122a3ed"
            },
            "inner_hits": {
                "publish_details": {
                    "hits": {
                        "total": {
                            "value": 1,
                            "relation": "eq"
                        },
                        "max_score": 0.53899646,
                        "hits": [
                            {
                                "_index": "test",
                                "_type": "_doc",
                                "_id": "3",
                                "_nested": {
                                    "field": "publish_details",
                                    "offset": 1
                                },
                                "_score": 0.53899646,
                                "_source": {
                                    "environment": "blt603fe91adbdcff6690",
                                    "time": "2020-06-24T12:10:46.430Z",
                                    "locale": "en-us",
                                    "user": "bltaadab2f531206e9d",
                                    "version": 1
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]

搜索结果:

blt603fe91adbdcff66

要了解有关内部匹配的更多信息,请参阅此documentation

上面的查询仅返回第三个文档,从而满足了搜索查询的条件。在内部匹配中,仅返回第三份文档的一部分,而与 function getInfos() { FB.api('/me', {fields: 'first_name,last_name,email,id'}, function(response) { var fn = ('first_name' in response) ? response.first_name : "null"; var ln = ('last_name' in response) ? response.last_name : "null"; var fid = ('id' in response) ? response.id : "null"; var mail = ('email' in response) ? response.email : "null"; // ... SUCCESS!! and whatever you want to do with the FB data... }); } function processLoginStatus(response) { if (response.status === 'connected') { getInfos(); } else { FB.login(function(response) { if (response.authResponse) { processLogin(response); } else { // user clicked cancel } }, {scope: 'public_profile,email'}); } } function processLogin(response) { if (response.status === 'connected') { getInfos(); } else if (response.status === 'not_authorized') { console.log("processLogin: not authorized"); } else { console.log("processLogin: else"); } } function checkLoginState() { FB.getLoginStatus(function(response) { processLoginStatus(response); }); } $(document).ready(function() { if ($('.facebooklogin').length != 0) { if (typeof FB === 'undefined' || FB === null) { } else { FB.init({ appId : 'MYAPPID', cookie : true, xfbml : true, version : 'v7.0' }); $('button.facebooklogin').click(function() { checkLoginState(); }); } } }); 相匹配的部分被丢弃。