ElasticSearch非嵌套对象类型字段查询方法咨询
Hey there! Let's work through this Elasticsearch + NEST issue together.
First off, your initial hunch is correct: since your currentname field isn't marked as a nested type, you don't need nested queries at all. Let's break down why, and figure out why your simple NEST query isn't returning results.
1. How Elasticsearch Stores Non-Nested Object Fields
When you define an object field without the nested keyword (like your currentname with a name sub-field), Elasticsearch flattens the structure under the hood. So your document gets stored as if it had two top-level fields:
registration(string)currentname.name(string)
There's no hierarchical "nesting" to account for here—object fields without the nested tag are just treated as flattened properties.
2. Correct NEST Query Examples
Let's assume your C# POCO looks something like this:
public class YourDocument { public string Registration { get; set; } public CurrentName CurrentName { get; set; } } public class CurrentName { public string Name { get; set; } }
Here's how you'd write a valid query to match documents where registration has a specific value AND currentname.name matches a target string:
var searchResponse = client.Search<YourDocument>(s => s .Query(q => q .Bool(b => b .Must( // Match registration value m => m.Match(mt => mt.Field(f => f.Registration).Query("your-registration-string")), // Match currentname.name value m => m.Match(mt => mt.Field(f => f.CurrentName.Name).Query("target-name")) ) ) ) );
If you just need to filter by currentname.name alone, it's even simpler:
var searchResponse = client.Search<YourDocument>(s => s .Query(q => q .Match(m => m.Field(f => f.CurrentName.Name).Query("target-name")) ) );
3. Common Troubleshooting Steps
If your query still isn't returning results, check these common pitfalls:
- Field Name Case Sensitivity: Elasticsearch is case-sensitive with field names. If your index uses camelCase (e.g.,
currentname.nameinstead ofCurrentName.Name), you can explicitly specify the raw field name:m => m.Match(mt => mt.Field("currentname.name").Query("target-name")) - Tokenizer Mismatch: If
currentname.nameuses a custom analyzer, your query string might not match the tokenized values stored in the index. Test this with Elasticsearch's_analyzeAPI:
Compare the output to the tokenized version of the actual value in your documents.POST _analyze { "text": "your-query-name", "field": "currentname.name" } - Verify Document Exists: First confirm there are documents matching your criteria using raw Elasticsearch JSON queries (via Kibana Dev Tools or curl):
If this returns results but your NEST query doesn't, the issue is in your C# code. If it doesn't return results, your data doesn't match the query criteria.GET your-index-name/_search { "query": { "bool": { "must": [ {"match": {"registration": "your-registration-string"}}, {"match": {"currentname.name": "target-name"}} ] } } }
When Would You Need Nested Queries?
Nested types are only necessary if you have an array of objects and need to enforce that multiple properties within the same array element match. For example, if currentname was an array like [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}] and you wanted to find documents where a single element has name: Alice AND age: 30, you'd need a nested field and nested query. For a single object field like yours, nested queries are overkill and won't work correctly.
内容的提问来源于stack exchange,提问作者Lars Holdgaard

