You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Trigram Tokenizer的Elasticsearch检索及最低匹配分数设置需求

Hey there! Let's walk through how to set up your Elasticsearch search using trigram tokenization to meet that 60% minimum match requirement (at least 3 out of the 5 target trigrams: ela, las, ast, sti, tic). Here's a step-by-step breakdown:

Step 1: Create an Index with a Custom Trigram Analyzer

First, we need to define an analyzer that splits text into 3-character trigrams—this ensures both your indexed content and search queries get broken down the same way. We'll use an ngram tokenizer configured for exactly 3-grams (which is equivalent to a dedicated trigram tokenizer):

PUT /trigram_search_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "trigram_analyzer": {
          "tokenizer": "trigram_tokenizer"
        }
      },
      "tokenizer": {
        "trigram_tokenizer": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 3
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "target_field": {
        "type": "text",
        "analyzer": "trigram_analyzer",
        "search_analyzer": "trigram_analyzer"
      }
    }
  }
}

Note: Replace target_field with the actual field name you're searching against. Using the same analyzer for indexing and searching ensures consistency in how trigrams are generated.

Step 2: Add Test Documents (Optional)

To verify the setup works, let's insert some sample texts:

POST /trigram_search_index/_doc/1
{
  "target_field": "elastic"
}

POST /trigram_search_index/_doc/2
{
  "target_field": "elast"
}

POST /trigram_search_index/_doc/3
{
  "target_field": "last"
}

POST /trigram_search_index/_doc/4
{
  "target_field": "astute"
}
Step 3: Build the Query with Minimum Match

We'll use a bool query with should clauses for each target trigram, then set minimum_should_match to either 3 (since 5 trigrams × 60% = 3) or "60%"—both will work to enforce your requirement:

GET /trigram_search_index/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"target_field": "ela"}},
        {"match": {"target_field": "las"}},
        {"match": {"target_field": "ast"}},
        {"match": {"target_field": "sti"}},
        {"match": {"target_field": "tic"}}
      ],
      "minimum_should_match": 3
    }
  }
}

What this query does:

  • It will return documents where the field matches at least 3 of your target trigrams.
  • For example:
    • elastic matches all 5 trigrams → included in results
    • elast matches ela, las, ast → 3 matches → included
    • last only matches las, ast → 2 matches → excluded
    • astute only matches ast → 1 match → excluded
Quick Notes
  • If you're working with an existing index, you can't modify the analyzer of an existing field. You'll need to create a new index with the correct mapping and reindex your data into it.
  • If you want to prioritize documents that match more trigrams (higher relevance), Elasticsearch's default scoring will handle this automatically—documents with more matches will have higher scores.

内容的提问来源于stack exchange,提问作者spsanthoshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:18:06