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

基于Azure Search的多字段人员匹配及评分配置的C# SDK实现咨询

Absolutely, you can build a robust person matching system with Azure Search that aligns exactly with your requirements—including weighted scoring, fuzzy name matching, and returning top-scoring results. Let’s walk through how to implement this using the Azure Search C# SDK, including the Lucene query support you’re looking for.

Step 1: Verify Your Index Setup

First, make sure your Azure Search index is configured to support your scoring profile and fuzzy matching:

  • Field Definitions: For name fields (Fname, Lname, Mname), set them as Searchable and use the default standard.lucene analyzer (it works perfectly for fuzzy queries). For exact-match fields like DOB, DriversLicense, etc., mark them as Filterable and Searchable to enable direct querying.
  • Scoring Profile: Ensure your defined scoring profile is included in the index definition. Here’s how you’d register it when creating/updating the index:
var index = new SearchIndex("person-index")
{
    Fields = new FieldBuilder().Build(typeof(Person)),
    ScoringProfiles = new[] 
    {
        new ScoringProfile() 
        { 
            Name = "PersonMatchScorer", 
            TextWeights = new TextWeights() 
            { 
                Weights = new Dictionary<string, double>() 
                { 
                    {"Fname",2}, {"Lname",2}, {"Mname",0.3}, {"DOB",3}, 
                    {"DriversLicense",6}, {"IdentificationNumber",6}, {"PhoneCell",4}, 
                    {"Gender",0.2}, {"PhoneHome",1} 
                } 
            } 
        }
    }
};

// Create or update the index via SearchClient
await searchClient.CreateOrUpdateIndexAsync(index);

(Note: Replace Person with your actual model class that maps to your index fields.)

Step 2: Build the Lucene Query String

To support fuzzy matching for names and exact matches for other fields, construct a Lucene-style query. For example, if you have partial input like first name "John", last name "Doe", and DOB "1990-01-01", your query would look like:

Fname:John~1 OR Lname:Doe~1 AND DOB:1990-01-01
  • The ~1 suffix enables fuzzy matching (allows up to 1 edit distance—adjust to ~2 for more leniency if needed for common name typos).
  • For fields requiring exact matches (like DriversLicense), use syntax like DriversLicense:XYZ123 without the fuzzy suffix.
  • Dynamically build this query based on which properties are provided (skip any null/empty input fields to avoid unnecessary clauses).
Step 3: C# SDK Implementation with Lucene Query

Here’s a complete example of using the Azure Search C# SDK to execute the query, apply your scoring profile, and return the top 5 results sorted by score:

using Azure.Search.Documents;
using Azure.Search.Documents.Models;

// Initialize SearchClient (replace with your service details)
var searchClient = new SearchClient(
    new Uri("https://your-search-service.search.windows.net"),
    "person-index",
    new AzureKeyCredential("your-admin-api-key"));

// Sample input properties (this would come from your user input)
var personInput = new 
{
    Fname = "John",
    Lname = "Doe",
    DOB = "1990-01-01",
    PhoneCell = "555-1234"
};

// Build the Lucene query dynamically
var queryParts = new List<string>();
if (!string.IsNullOrEmpty(personInput.Fname))
    queryParts.Add($"Fname:{personInput.Fname}~1");
if (!string.IsNullOrEmpty(personInput.Lname))
    queryParts.Add($"Lname:{personInput.Lname}~1");
if (!string.IsNullOrEmpty(personInput.DOB))
    queryParts.Add($"DOB:{personInput.DOB}");
if (!string.IsNullOrEmpty(personInput.PhoneCell))
    queryParts.Add($"PhoneCell:{personInput.PhoneCell}");

string luceneQuery = string.Join(" AND ", queryParts);

// Configure search options
var searchOptions = new SearchOptions
{
    // Apply your custom scoring profile
    ScoringProfileName = "PersonMatchScorer",
    // Enable Lucene query syntax
    QueryType = SearchQueryType.Lucene,
    // Return top 5 highest-scoring results
    Size = 5,
    // Sort results by score in descending order
    OrderBy = { "search.score() desc" }
};

// Execute the search
var response = await searchClient.SearchAsync<Person>(luceneQuery, searchOptions);

// Process results and filter by your threshold (e.g., score > 3.0)
var matchingPersons = new List<Person>();
await foreach (var result in response.Value.GetResultsAsync())
{
    if (result.Score.HasValue && result.Score.Value > 3.0)
    {
        matchingPersons.Add(result.Document);
        Console.WriteLine($"Match found: {result.Document.Fname} {result.Document.Lname}, Score: {result.Score.Value:F2}");
    }
}
Step 4: Key Notes
  • Fuzzy Matching Limits: Azure Search supports fuzzy queries with edit distances up to 2, but ~1 is usually sufficient for common name typos (e.g., "Jon" vs "John").
  • Score Threshold Filtering: You can filter results either after fetching (as shown) or using a filter in SearchOptions.Filter like search.score() gt 3.0—this works fine with your text-weighted scoring profile.
  • Partial Input Handling: The dynamic query building ensures you only include fields that have input values, so you can seamlessly handle scenarios where some properties are missing.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:09:07