【问题标题】:elasticsearch n-gram tokenizer with match_phrase not giving expected result具有 match_phrase 的弹性搜索 n-gram 标记器未给出预期结果
【发布时间】:2022-09-27 14:19:20
【问题描述】:

我创建了一个索引如下

PUT /ngram_tokenizer
{
  \"mappings\": {
    \"properties\": {
      \"test_name\": {
      \"type\": \"text\",
      \"analyzer\": \"my_analyzer\"
    }
  } 
},
  \"settings\": {
    \"index\": {
      \"max_ngram_diff\": 20
    },
    \"analysis\": {
      \"tokenizer\": {
        \"my_tokenizer\": {
          \"type\": \"ngram\",
          \"min_gram\": 2,
          \"max_gram\": 20,
          \"token_chars\":[
            \"letter\",
            \"digit\",
            \"whitespace\",
            \"symbol\"
            ]
        }
      },
      \"analyzer\": {
        \"my_analyzer\": {
          \"tokenizer\": \"my_tokenizer\"
        }
      }
    }
  }
}

然后索引如下

POST /ngram_tokenizer/_doc
{
  \"test_name\": \"test document\"
}
POST /ngram_tokenizer/_doc
{
  \"test_name\": \"another document\"
}

然后我做了一个 match_phrase 查询,

GET /ngram_tokenizer/_search
{
  \"query\": {
    \"match_phrase\": {
      \"test_name\": \"document\"
      
  }
}
}

上面的查询按预期返回两个文档,但下面的查询没有返回任何文档

GET /ngram_tokenizer/_search
{
  \"query\": {
    \"match_phrase\": {
      \"test_name\": \"test\"
      
  }
}
}

另外,我通过以下查询检查了它生成的所有令牌

POST ngram_tokenizer/_analyze
{
  \"analyzer\": \"my_analyzer\",
  \"text\": \"test document\"
}

匹配查询工作正常,你能帮我吗

    标签: elasticsearch


    【解决方案1】:

    这是因为在搜索时,用于分析输入文本的分析器与在索引时使用的分析器相同,即my_analyzer,而match_phrase 查询比match 查询复杂一些。

    在搜索时,您应该简单地使用standard 分析器(或与 ngram 分析器不同的东西)来分析您的查询输入。

    以下查询显示了如何使其按预期工作。

    GET /ngram_tokenizer/_search
    {
      "query": {
        "match_phrase": {
          "test_name": {
            "query": "test",
            "analyzer": "standard"
          }
        }
      }
    }
    

    您还可以在映射中将standard 分析器指定为search_analyzer

    "test_name": {
      "type": "text",
      "analyzer": "my_analyzer",
      "search_analyzer": "standard"
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多