如何编写一个percolator查询到嵌套程序

时间:2015-05-14 16:10:53

标签: elasticsearch nest

我在弹性搜索中有一个过滤器查询,如下所示。

put /skill-index

PUT /skill-index/skill-type/_mapping
{
    "properties" : {
        "message" : {
            "type": "string"
        }

    }

}

PUT /skill-index/.percolator/101
{
    "query" : {
        "match" : {
            "message" : "crossstitch"
        }
    }

}

PUT /skill-index/.percolator/102
{
    "query" : {
        "match" : {
            "message" : "chainstitch"
        }
    }

}

PUT /skill-index/.percolator/103
{
    "query" : {
        "match" : {
            "message" : "stemstitch"
        }
    }

}

PUT /skill-index/.percolator/104
{
    "query" : {
        "match" : {
            "message" : "longandshort"
        }
    }

}



GET /skill-index/skill-type/_percolate  
{
    "doc" : {
        "message" : "Know chainstitch and stemstitch"
    }
}

现在,我想在我的NEST计划中获得GET查询。 例如。我有一个叫做skillentity的实体。我将传递一个变量字符串,例如"知道chainstitch和stemstitch"。

我想将值检索为102,103

所以我应该像这样构建一些东西 声明一个清单 列出技能列表=新列表

之后我想要一个lambda表达式查询,比如。

var skillsList = client.Percolate(......传递变量并获得结果..)

你能否帮助我在NEST中构建查询

1 个答案:

答案 0 :(得分:3)

这个例子将解释如何处理NEST中的过滤器

internal class Program
{
    private static void Main(string[] args)
    {
        var indexName = "indexname";

        var uri = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(uri)
            .SetDefaultIndex(indexName)
            .EnableTrace();
        var client = new ElasticClient(settings);

        var indicesResponse = client.DeleteIndex(descriptor => descriptor.Index(indexName));

        client.CreateIndex(descriptor => descriptor.Index(indexName).AddMapping<Document>(m => m.MapFromAttributes()));

        var percolate = client.RegisterPercolator<Document>("p1",
            descriptor => descriptor.Index(indexName).Query(q => q.Match(m => m.OnField(f => f.Name).Query("test"))));

        var percolate2 = client.RegisterPercolator<Document>("p2",
            descriptor => descriptor.Index(indexName).Query(q => q.Match(m => m.OnField(f => f.Name).Query("something"))));

        var percolateResponse =
            client.Percolate<Document>(descriptor => descriptor.Index(indexName).Document(new Document {Name = "this is a test"}));

        //Matches contain percolator p1 
        var percolatorIds = percolateResponse.Matches.Select(x => x.Id);

        Console.ReadKey();
    }
}

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