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

如何在Elasticsearch的test_nested索引中检索指定前缀的唯一名称

Fetch Unique Names with Prefix Match from Elasticsearch's test_nested Index

Alright, let's tackle this problem step by step. You want to retrieve all unique names from your test_nested index that start with the prefix wi, and get exactly [Microsoft Windows, Widows 10, Windows XP] as the result. Since you've already checked the index mappings and run basic search requests, let's dive into the correct approach.

1. Verify Your Field Mapping First

First off, let's confirm your name field's mapping structure—this is critical for getting accurate unique results. If you ran GET test_nested/_mappings, you’ll want to check if your name field has a keyword sub-field.

Why? Because using a text field directly for aggregation will return split, tokenized terms (not full unique names). A keyword sub-field preserves the full string, perfect for fetching unique values. Your ideal mapping should look something like this:

{
  "test_nested": {
    "mappings": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

If your name field is nested inside an object (since your index is named test_nested), your mapping might look like this instead:

{
  "test_nested": {
    "mappings": {
      "properties": {
        "products": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            }
          }
        }
      }
    }
  }
}

2. Build the Correct Search Query

We’ll combine a prefix query to match names starting with wi with a terms aggregation to fetch unique values. Here are the two scenarios:

Scenario A: name is a Top-Level Field

Run this search request—we set size: 0 because we don’t need raw documents, just the aggregation results:

GET test_nested/_search
{
  "size": 0,
  "query": {
    "prefix": {
      "name": "wi" # Matches any name where the tokenized text starts with "wi"
    }
  },
  "aggs": {
    "unique_matching_names": {
      "terms": {
        "field": "name.keyword", # Uses the keyword sub-field to get full unique names
        "size": 10 # Adjust this to ensure you capture all matching results
      }
    }
  }
}

Scenario B: name is a Nested Field

If your name lives inside a nested object (like the products field above), you need to wrap the query and aggregation in a nested context:

GET test_nested/_search
{
  "size": 0,
  "query": {
    "nested": {
      "path": "products",
      "query": {
        "prefix": {
          "products.name": "wi"
        }
      }
    }
  },
  "aggs": {
    "nested_products_context": {
      "nested": {
        "path": "products"
      },
      "aggs": {
        "unique_matching_names": {
          "terms": {
            "field": "products.name.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

3. Handle Case Insensitivity (Optional)

If you need the prefix match to ignore case (e.g., match Wi, WI, or wi), add a lowercase normalizer to your keyword sub-field:

First, update the index settings:

PUT test_nested/_settings
{
  "analysis": {
    "normalizer": {
      "lowercase_normalizer": {
        "type": "custom",
        "filter": ["lowercase"]
      }
    }
  }
}

Then update the name field mapping (you’ll need to reindex if the index already has data):

PUT test_nested/_mappings
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        }
      }
    }
  }
}

Now your prefix query with wi will match all case variations.

4. Extract the Results

After running the correct search, look for the aggregations.unique_matching_names.buckets array in the response. Each bucket’s key will be one of your unique names. Extracting these keys will give you exactly the list you need: [Microsoft Windows, Widows 10, Windows XP].

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:27:20