在其类型中使用通配符时,DocumentExists()将失败 [英] DocumentExists() fails when a wildcard is used in its Type

查看:107
本文介绍了在其类型中使用通配符时,DocumentExists()将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用类型通配符的Update()问题也存在,但是我发现DocumentExists()也是一样的,所以我在这里解决了这个问题:

The problem also exists for Update() using a Type wildcard, but I found that DocumentExists() does the same thing, so I've distilled the issue down here:

这个工程...

var docExists = client.DocumentExists<object>(d => d
    .Index(indexname)
    .Id(myId)
    .Type("Abcdef"));

但这失败了...

var docExists = client.DocumentExists<object>(d => d
    .Index(indexname)
    .Id(myId)
    .Type("Abc*"));

如果我完全省略类型,它也会失败。
任何人都知道如何使这项工作? (即使它的工作,无论文档的类型对我的目的都会很好。)

It also fails if I omit the Type altogether. Anyone know how to make this work? (Even if it worked regardless of the type of the document would be fine for my purpose.)

推荐答案

据我所知您不能在类型名称中指定通配符,但您可以做一个技巧。

As far as I know it's not possible to specify wildcard in type name, but you can do a trick.

您可以使用特定的ID查询索引,并使用前缀过滤器来缩小搜索范围在某些类型上。

You can query your index for documents with specific id and use prefix filter to narrow down search on certain types.

var searchResponse = client.Search<dynamic>(s => s
    .Type(string.Empty)
    .Query(q => q.Term("id", 1))
    .Filter(f => f.Prefix("_type", "type")));

这是完整的例子:

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

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

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

        client.CreateIndex(descriptor => descriptor.Index(indexName));

        client.Index(new Type1 {Id = 1, Name = "Name1"}, descriptor => descriptor.Index(indexName));
        client.Index(new Type1 {Id = 11, Name = "Name2"}, descriptor => descriptor.Index(indexName));
        client.Index(new Type2 {Id = 1, City = "City1"}, descriptor => descriptor.Index(indexName));
        client.Index(new Type2 {Id = 11, City = "City2"}, descriptor => descriptor.Index(indexName));
        client.Index(new OtherType2 {Id = 1}, descriptor => descriptor.Index(indexName));

        client.Refresh();

        var searchResponse = client.Search<dynamic>(s => s
            .Type(string.Empty)
            .Query(q => q.Term("id", 1))
            .Filter(f => f.Prefix("_type", "type")));

        var objects = searchResponse.Documents.ToList();
    }
}

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

class Type2
{
    public int Id { get; set; }
    public string City { get; set; }
}

class OtherType2
{
    public int Id { get; set; }
}

不了解你的大局,但也许你可以改变您使用索引而不是类型的解决方案?您可以在通配符名称中放置通配符。 看看。

Don't know much about your big picture, but maybe you can change your solution to use indexes instead of types? You can put wildcard in the index names. Take a look.

这篇关于在其类型中使用通配符时,DocumentExists()将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆