0

我的映射如下,

mapping = {
        "mappings":{
          "document": {
            "properties": {
               "title": {"type": "text"},
               "users": {
                "type": "nested",
                "properties": {
                    "user_id": {"type": "integer"},
                    "name": {"type": "text"},
                }
            }
        }
    }
}

我可以很好地添加数据,但是当我使用搜索时

res = es.search(index="test", body={"query": {
    "nested": {
        "path": "users", "query": {
            "match": {'users.user_id': 1}
        }
    }
}
}

它返回整个文档而不是特定行。

更多信息:

我正在为弹性搜索提供一个表格:

用户 ID | 姓名

1.............乔

2 ......玛丽

如果我在搜索中将 1 作为 user_id,我需要弹性搜索来返回 {user_id:1, name:Joe}

4

1 回答 1

1

您应该查看嵌套查询,他们在按文档上的嵌套对象过滤时使用bool > must 。此外,当您需要过滤时,请使用term而不是match 。匹配用于全文搜索

像这样试试

res = es.search(index="test", body={"query": {
        "nested": {
            "path": "users", 
            "query": {
                "bool": {
                    "must": [
                        {                           
                            term": {
                                'users.user_id': 1
                            }
                        }
                    ]
                }
            }
        }
    }
}
于 2017-12-06T07:31:03.133 回答