【问题标题】:Search match text in Elasticsearch SpringBoot by using percentage使用百分比在 Elasticsearch SpringBoot 中搜索匹配文本
【发布时间】:2021-11-03 10:53:45
【问题描述】:

我是这里的新 Elasticsearch SpringBoot。我不知道如何使用百分比在 Elasticsearch SpringBoot 中搜索匹配文本。例如,我有一个文本“Hello world”。我可以设置 50% 或 70% 的百分比来匹配我的文本吗?我已经尝试使用属性 minimumShouldMatch ,但它现在似乎不适用于我的情况。 哪位大神帮忙看看,谢谢

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: java spring-boot kotlin elasticsearch spring-data


【解决方案1】:

您可以使用should 查询,按字词分割您的搜索词组,并根据您的pourcentage 设置minimum_should_match

查询示例

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "my_field": "hello"
          }
        },
        {
          "term": {
            "my_field": "world"
          }
        },
        {
          "term": {
            "my_field": "i'm"
          }
        },
        {
          "term": {
            "my_field": "alive"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

会找到hello worldhello alive 等...

要拆分文本,您应该使用 _analysis of your index

分析和拆分术语

POST myindex/_analyze
{
  "field": "my_field",
  "text": "hello world i'm alive"
}

如果您使用自定义analyzer,那么它会为您提供这样的结果来填充您的查询,并将术语分析器与查询匹配。

{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "world",
      "start_offset" : 6,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "i'm",
      "start_offset" : 12,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "alive",
      "start_offset" : 16,
      "end_offset" : 21,
      "type" : "<ALPHANUM>",
      "position" : 3
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 2014-12-03
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多