使用Type通配符的Update()也存在问题,但是我发现DocumentExists()做了同样的事情,所以我在这里提出了这个问题:
这有效......
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*"));
如果我完全省略Type,它也会失败。 有谁知道如何使这项工作? (即使它无论文件的类型如何都有用,对我来说也没问题。)
答案 0 :(得分:0)
据我所知,它不能在类型名称中指定通配符,但你可以做一个技巧。
您可以在索引中查询具有特定ID的文档,并使用前缀过滤器来缩小对某些类型的搜索范围。
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; }
}
不太了解你的大局,但也许你可以改变你的解决方案来使用索引而不是类型?您可以将通配符放在索引名称中。 Take a look.