elasticsearch自动完成某些字段值

vof42yt1  于 5个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(46)

我在elasticsearch中存储文章,比如在文章索引中:

{
  "title": "my title",
  "text": "article text",
  "date": "2023-11-28T00:00:00Z",
  "authors": ["author 1", "author 2"],
  "section": "article section"
}

字符串
使用以下Map:

{
  "title": { type: "text" },
  "text": { type: "text" },
  "date": { type: "date" },
  "authors": { type: "keyword" },
  "section": { type: "keyword" }


现在,在我的应用程序的搜索UI中,我想给予用户一种方法来搜索存储在所有文档中的作者,这样他就可以对他们进行筛选。
有没有一种方法可以只查询文章索引字段中的离散值,或者我需要为作者和章节建立一个单独的索引?

brc7rcf0

brc7rcf01#

您可以使用terms aggregation来实现这一点:

POST index/_search
{
  "size": 0,
  "aggs": {
    "authors": {
      "terms": {
        "field": "authors",
        "size": 10
      }
    },
    "sections": {
      "terms": {
        "field": "section",
        "size": 10
      }
    }
  }
}

字符串
另一种方法是使用suggesters,如term或completion标记。

t9eec4r0

t9eec4r02#

刚刚找到了Terms enum API,它可以很容易地对值进行前缀搜索。

GET /articles/_terms_enum
{
    "field" : "authors",
    "string" : "searchterm"
}

字符串
尽管@瓦尔suggestion可以用于任何查询,但我认为这是一种更好的方法。

相关问题