使用巢状弹性

时间:2019-07-28 09:44:47

标签: c# elasticsearch nest

我有一个网站电子商务。用户可以搜索产品。每个产品都有“名称”,“品牌”,“类别”列表。 如果用户搜索“ Tommy T恤衫男士”,则结果仅应返回男士Tommy T恤衫,不返回任何带有单词Tommy或T恤衫的结果。如果用户搜索“ Tommy”,则应返回所有带有单词tommy的结果。如果用户搜索“ Tommy men”(汤米男装),则只应退回tommy mens产品,而不是带有单个匹配词“ tommy”或单个匹配词“ men”的人。

我的代码如下:

public ISearchResponse<Models.Product> Search(string term, int minMatch)
{
    var response = client.Search<Models.Product>(search => search
            .Query(q => q.Bool(b => b.Should(
            s => s.Match(m => m.Query(term).Field(f => f.ProductName).Boost(5).Fuzziness(Fuzziness.EditDistance(0))),
            s => s.Match(m => m.Query(term).Field(f => f.Brand).Boost(15).Fuzziness(Fuzziness.EditDistance(0))),
            s => s.Match(m => m.Query(term).Field(f => f.Category).Boost(10).Fuzziness(Fuzziness.EditDistance(0)))
            ).MinimumShouldMatch(minMatch))));

            return response;
}

public ISearchResponse<Models.Product> Read(string term)
{
    var fixedInput = Regex.Split(term, @"[^\p{L}]*\p{Z}[^\p{L}]*");
    int minMatch;

    if (fixedInput.Count() > 1) minMatch = 2; 
    else  minMatch = 1; 

    var results = Search(term, minMatch);

    if (!results.Documents.Any() && minMatch.Equals(2))
    {
        results = Search(term, 1);
    }

    return results;
}

如果我要搜索“ Tommy men”,则第一个结果是“ Tommey men”,而其他结果是“ Diesel men”,“ Boss men”,如果搜索字词多于一个,则如何用一个单词匹配过滤结果字。

1 个答案:

答案 0 :(得分:2)

您是说下面的句子中的“ tommy”!=“ Tommy”吗?

  

一个字都没有结果   汤米或T恤衫应退还。如果用户搜索“ Tommy”   然后应返回所有带有tommy词的结果。

对于其余示例,可以将multi-match query类型的cross_fields使用。

示例应用程序:

    public class Document
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Brand { get; set; }
        public string Category { get; set; }

        public override string ToString() => $"Id: {Id} Name: {Name} Brand: {Brand} Category: {Category}";
    }

    static async Task Main(string[] args)
    {
        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
        var connectionSettings = new ConnectionSettings(pool);
        connectionSettings.DefaultIndex("documents");

        var client = new ElasticClient(connectionSettings);

        var deleteIndexResponse = await client.Indices.DeleteAsync("documents");
        var createIndexResponse = await client.Indices.CreateAsync("documents", d => d
            .Map(m => m.AutoMap<Document>()));

        var indexDocument = await client
            .IndexDocumentAsync(new Document {Id = 1, Brand = "Tommy", Category = "men"});
        var indexDocument2 = await client
            .IndexDocumentAsync(new Document {Id = 2, Brand = "Diesel", Category = "men"});
        var indexDocument3 = await client
            .IndexDocumentAsync(new Document {Id = 3, Brand = "Boss", Category = "men"});

        var refreshAsync = client.Indices.RefreshAsync();

        var query = "Tommy";
        var searchResponse = await Search(client, query);
        PrintResults(query, searchResponse);

        query = "Tommy men";
        searchResponse = await Search(client, query);
        PrintResults(query, searchResponse);

        query = "men";
        searchResponse = await Search(client, query);
        PrintResults(query, searchResponse);
    }

    private static async Task<ISearchResponse<Document>> Search(ElasticClient client, string query)
    {
        var searchResponse = await client.SearchAsync<Document>(s => s.Query(q => q
            .MultiMatch(mm => mm
                .Fields(f => f.Fields(ff => ff.Brand, ff => ff.Category, ff => ff.Name))
                .Query(query)
                .Type(TextQueryType.CrossFields)
                .MinimumShouldMatch("100%"))));
        return searchResponse;
    }

    private static void PrintResults(string query, ISearchResponse<Document> searchResponse)
    {
        Console.WriteLine($"query: {query}");
        Console.WriteLine(searchResponse.Total);
        Console.WriteLine($"results: ");
        searchResponse.Documents.ToList().ForEach(Console.WriteLine);
        Console.WriteLine();
    }
}

打印:

query: Tommy
found: 1
Id: 1 Name:  Brand: Tommy Category: men

query: Tommy men
found: 1
Id: 1 Name:  Brand: Tommy Category: men

query: men
found: 3
Id: 1 Name:  Brand: Tommy Category: men
Id: 2 Name:  Brand: Diesel Category: men
Id: 3 Name:  Brand: Boss Category: men

希望有帮助。