【问题标题】:How to build an N-Gram relationship in Elasticsearch如何在 Elasticsearch 中建立 N-Gram 关系
【发布时间】:2021-09-22 01:37:38
【问题描述】:

我是 Elasticsearch 的新手,我正在寻找构建一个包含谚语列表的前端应用程序。当用户浏览这些谚语时,我希望他们从 Proverb DB 中找到相关的 N-Gram 谚语或分析谚语。例如点击时

“注视锅从不沸腾”会带来以下建议:

  • 1 克建议: “一锅两尿”

  • 2 克建议: “注视的锅尝起来很苦”

  • 分析建议: “厨师太多会破坏肉汤”

有没有办法在 ES 中做到这一点,还是我需要构建自己的逻辑?

【问题讨论】:

  • 1-gram 和 2-gram 的建议很简单,但我没有得到分析的建议。

标签: node.js elasticsearch frontend n-gram


【解决方案1】:

1-gram 建议开箱即用,2-gram 建议可以通过shingle 轻松实现。

这是一个尝试

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "2-grams": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "shingles"
          ]
        }
      },
      "filter": {
        "shingles": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 2,
          "output_unigrams": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "standard",
        "fields": {
          "2gram": {
            "type": "text",
            "analyzer": "2-grams"
          }
        }
      }
    }
  }
}

接下来索引一些文档:

PUT test/_doc/1
{
  "text": "Two pees in a pot"
}

PUT test/_doc/2
{
  "text": "A Watched pot tastes bitter"
}

最后,您可以使用以下查询搜索 1-gram 建议,您将在响应中获得两个文档:

POST test/_search
{
  "query": {
    "match": {
      "text": "A watched pot never boils"
    }
  }
}

您还可以使用以下查询搜索 2-gram 建议,并且只会出现第二个文档:

POST test/_search
{
  "query": {
    "match": {
      "text.2gram": "A watched pot never boils"
    }
  }
}

PS:虽然不确定“分析”建议的工作原理,但请随时提供更多见解,我会更新。

【讨论】:

  • 嗨 Val,我遇到了技术问题,只是刚刚解决了这个问题。我会仔细看看的。它说我的赏金到期,我不知道那意味着什么。关于分析,我的意思是说锅煮和肉汤都是烹饪概念。
  • 一个即将到期的赏金意味着你 lost your points 没有将它们归因于其他人,这意味着你可能会较少关注你的下一个问题 ;-) 不用担心,让我知道当你有时间
猜你喜欢
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多