首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >筛选数组ElasticSearch中项的文档

筛选数组ElasticSearch中项的文档
EN

Stack Overflow用户
提问于 2019-03-26 20:20:36
回答 1查看 1.1K关注 0票数 0

我正在使用ElasticSearch搜索文档。但是,我需要确保当前用户能够看到这些文档。每个文档都绑定到一个社区,用户可能属于这个社区。

以下是我的文档的映射:

代码语言:javascript
复制
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 } }过滤查询时,它会返回所有文档,而不管它绑定到哪个社区。

以下是完整的查询:

代码语言:javascript
复制
{
  index: 'document',
  filter_path: { filter: {term: { communities: community.id } } },
  body: {
    sort: [{ createdAt: { order: 'asc' } }]
  }
}

这是基于"b7d28e7f-7534-406a-981e-ddf147b5015a"社区id的以下结果。注意:--这是我的graphql的返回,所以文档上的社区在解析ES查询的命中之后是实际的完整对象。

代码语言:javascript
复制
"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 ):

代码语言:javascript
复制
query: { index: 'document',
  body: 
   { sort: [ { createdAt: { order: 'asc' } } ],
     from: 0,
     size: 5,
     query: 
      { bool: 
         { filter: 
            { term: { communities: '672916cf-ee32-4bed-a60f-9a7c08dba04b' } } } } } }

这给了我以下的结果:

代码语言:javascript
复制
{
  "data": {
    "communities": [
      {
        "id": "672916cf-ee32-4bed-a60f-9a7c08dba04b",
        "feed": {
          "documents": {
            "hits": []
          }
        }
      }
    ]
  }
}

看来我的过滤器工作得太好了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-27 03:24:53

因为您正在存储社区的ids,所以您应该确保这些ids不会被分析。对于这个communities,应该是keyword类型的。其次,您希望存储社区ids数组,因为用户可以属于多个社区。要做到这一点,您不需要使用nested类型。嵌套有所有不同的用例。对于作为数组的值,您需要确保在索引时始终将值作为数组传递给字段,即使值是单个值。

您需要更改映射和针对字段communities索引值的方式。

1.更新映射如下:

代码语言:javascript
复制
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.向索引中添加文档:

代码语言:javascript
复制
PUT my_index/_doc/1
{
  "title": "The One True Document",
  "communities": [
    "edd05cd0-0a49-4676-86f4-2db913235371",
    "672916cf-ee32-4bed-a60f-9a7c08dba04b"
  ]
}

3.通过社区id:进行过滤

代码语言:javascript
复制
GET my_index/_doc/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "communities": "672916cf-ee32-4bed-a60f-9a7c08dba04b"
          }
        }
      ]
    }
  }
}

嵌套场法

1.映射:

代码语言:javascript
复制
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.索引文档:

代码语言:javascript
复制
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.查询(用于嵌套查询):

代码语言:javascript
复制
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。要理解这种情况的原因,请通过

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55365621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档