我正在使用ElasticSearch搜索文档。但是,我需要确保当前用户能够看到这些文档。每个文档都绑定到一个社区,用户可能属于这个社区。
以下是我的文档的映射:
export const mapping = {
properties: {
amazonId: { type: 'text' },
title: { type: 'text' },
subtitle: { type: 'text' },
description: { type: 'text' },
createdAt: { type: 'date' },
updatedAt: { type: 'date' },
published: { type: 'boolean' },
communities: { type: 'nested' }
}
}我目前正在将文档所属社区的in保存在字符串数组中。例:["edd05cd0-0a49-4676-86f4-2db913235371", "672916cf-ee32-4bed-a60f-9a7c08dba04b"]
目前,当我使用{term: { communities: community.id } }过滤查询时,它会返回所有文档,而不管它绑定到哪个社区。
以下是完整的查询:
{
index: 'document',
filter_path: { filter: {term: { communities: community.id } } },
body: {
sort: [{ createdAt: { order: 'asc' } }]
}
}这是基于"b7d28e7f-7534-406a-981e-ddf147b5015a"社区id的以下结果。注意:--这是我的graphql的返回,所以文档上的社区在解析ES查询的命中之后是实际的完整对象。
"hits": [
{
"title": "The One True Document",
"communities": [
{
"id": "edd05cd0-0a49-4676-86f4-2db913235371"
},
{
"id": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
}
]
},
{
"title": "Boring Document 1",
"communities": []
},
{
"title": "Boring Document 2",
"communities": []
},
{
"title": "Unpublished",
"communities": [
{
"id": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
}
]
}
]当我试图将社区映射为{type: 'keyword', index: 'not_analyzed'}时,我会收到一个声明为[illegal_argument_exception] Could not convert [communities.index] to boolean的错误。
那么,我需要改变我的映射,我的过滤器,还是两者都改变?在6.6名医生周围搜索,我看到terms需要non_analyzed映射。
更新
我将社区映射更新为keyword,如下所示。然而,我仍然得到了同样的结果。
我将查询更新为以下内容(使用包含文档的社区id ):
query: { index: 'document',
body:
{ sort: [ { createdAt: { order: 'asc' } } ],
from: 0,
size: 5,
query:
{ bool:
{ filter:
{ term: { communities: '672916cf-ee32-4bed-a60f-9a7c08dba04b' } } } } } }这给了我以下的结果:
{
"data": {
"communities": [
{
"id": "672916cf-ee32-4bed-a60f-9a7c08dba04b",
"feed": {
"documents": {
"hits": []
}
}
}
]
}
}看来我的过滤器工作得太好了?
发布于 2019-03-27 03:24:53
因为您正在存储社区的ids,所以您应该确保这些ids不会被分析。对于这个communities,应该是keyword类型的。其次,您希望存储社区ids数组,因为用户可以属于多个社区。要做到这一点,您不需要使用nested类型。嵌套有所有不同的用例。对于作为数组的值,您需要确保在索引时始终将值作为数组传递给字段,即使值是单个值。
您需要更改映射和针对字段communities索引值的方式。
1.更新映射如下:
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"amazonId": {
"type": "text"
},
"title": {
"type": "text"
},
"subtitle": {
"type": "text"
},
"description": {
"type": "text"
},
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
},
"published": {
"type": "boolean"
},
"communities": {
"type": "keyword"
}
}
}
}
}2.向索引中添加文档:
PUT my_index/_doc/1
{
"title": "The One True Document",
"communities": [
"edd05cd0-0a49-4676-86f4-2db913235371",
"672916cf-ee32-4bed-a60f-9a7c08dba04b"
]
}3.通过社区id:进行过滤
GET my_index/_doc/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"communities": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
}
}
]
}
}
}嵌套场法
1.映射:
PUT my_index_2
{
"mappings": {
"_doc": {
"properties": {
"amazonId": {
"type": "text"
},
"title": {
"type": "text"
},
"subtitle": {
"type": "text"
},
"description": {
"type": "text"
},
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
},
"published": {
"type": "boolean"
},
"communities": {
"type": "nested"
}
}
}
}
}2.索引文档:
PUT my_index_2/_doc/1
{
"title": "The One True Document",
"communities": [
{
"id": "edd05cd0-0a49-4676-86f4-2db913235371"
},
{
"id": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
}
]
}3.查询(用于嵌套查询):
GET my_index_2/_doc/_search
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "communities",
"query": {
"term": {
"communities.id.keyword": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
}
}
}
}
]
}
}
}你可能注意到我用的是communities.id.keyword而不是communities.id。要理解这种情况的原因,请通过这。
https://stackoverflow.com/questions/55365621
复制相似问题