elasticsearch - 复杂结构中的过滤搜索

标签 elasticsearch

我有这样一种模式的人的名单:

{
    "id": 125018,
    "nom": "GALLA",
    "prenom": "patrick",
    "departement": 91,
    "comptes": [
        {"id": 83557, "type": "facebook", "login": "patrick"},
        {"id": 83558, "type": "google", "login": "patrick"},
        {"id": 83558, "type": "twitter", "login": "patrick"}
    ]
}

我将搜索departement等于91的人,并提高拥有google帐户的人的得分。

我尝试了这种请求:

{
"query": {
    "bool": {
        "must": [
            { "term": { "departement": "91" } }
        ],
        "should" : [
            { "match" : { "comptes.1.type" : "google" } }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}
}

但是comptes.1.typecomptes.*.type匹配不好。

您是否有更好的查询语法来查找拥有Google帐户的人?

编辑:

是的,谢谢妮可:您的搜索查询效果很好!

错误出在我的输入查询中。我们需要注意PHP json_encode 函数,这些函数没有为我提供正确的输入语法。因此搜索效果不佳...

PHP json_encode输入语法:

{
    "id": 125018,
    "nom": "GALLA",
    "prenom": "patrick",
    "departement": 91,
    "comptes": {
        83557 : {"id": 83557, "type": "facebook", "login": "patrick"},
        83558 : {"id": 83558, "type": "google", "login": "patrick"},
        83559 : {"id": 83559, "type": "twitter", "login": "patrick"}
    }
}

良好的inpout语法:

{
    "id": 125018,
    "nom": "GALLA",
    "prenom": "patrick",
    "departement": 91,
    "comptes": [
        {"id": 83557, "type": "facebook", "login": "patrick"},
        {"id": 83558, "type": "google", "login": "patrick"},
        {"id": 83559, "type": "twitter", "login": "patrick"}
    ]
}

最佳答案

即使在多值字段中,您也可以通过parent.child表示法指定一种关系,例如您的情况下为comptes.type

要对此进行测试,您可以尝试查找具有Google帐户的用户:

"query": {
    "bool": {
        "must": [
            { "term": { "departement": 91 } },
            { "term": { "comptes.type": "google" } }
        ]
    }
}

一个与https://www.found.no/play/gist/90a9290e806226e2d41f一起玩的演示

所以您的提升查询可能看起来像

"query": {
    "bool": {
        "must": [
            { "term": { "departement": 91 } }
        ],
        "should" : [
            { "term" : { // you're looking for an exact term 
                "comptes.type" : {
                    "value": "google",
                    "boost": 2.0 
                }
            }}
        ]
    }
}

https://www.found.no/play/gist/ac979072963875ba79f9
boost属性不是必需的,因为Elastic会自动增强匹配项,但可以派上用场。

关于elasticsearch - 复杂结构中的过滤搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898021/

相关文章:

c# - 使用 NEST 创建自定义 token 过滤器

elasticsearch - 在Elasticsearch中将Unix时间戳转换为日期时间

elasticsearch - 如何提高Elasticsearch索引编制速度?

Elasticsearch:按日期字段排序(降序):gauss 或 field_value_factor?

elasticsearch - 删除旧索引的索引生命周期策略

javascript - Promise 解析后在 'then' 中发送响应

python - python 中的 elasticsearch-dsl 库在使用 search.from_dict() 方法从字典语法构造查询时给出双重结果

elasticsearch - 弹性查询更改了映射

search - ElasticSearch - 更新或新索引?

elasticsearch - Logstash-ElasticSearch中的聚合