有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java搜索使用百分比匹配Elasticsearch SpringBoot中的文本

我是一个新的Elasticsearch SpringBoot。我不知道如何使用百分比在Elasticsearch SpringBoot中搜索匹配文本。例如,我有一个文本“Hello world”。我可以设置50%或70%的百分比来匹配我的文本吗?我已经尝试使用property minimumShouldMatch,但现在似乎对我的案例不起作用。 谁能帮帮我,谢谢


共 (1) 个答案

  1. # 1 楼答案

    您可以使用should查询,按术语拆分搜索短语,并根据您的词组设置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

    要按术语拆分文本,您应该使用索引的分析

    分析和拆分术语

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

    它给出这样的结果来填充查询,并将术语分析器与查询相匹配,例如,如果您使用custom 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
        }
      ]
    }