问题
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:
This works...
var docExists = client.DocumentExists<object>(d => d
.Index(indexname)
.Id(myId)
.Type("Abcdef"));
but this fails...
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.)
回答1:
As far as I know it's not possible to specify wildcard in type name, but you can do a trick.
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")));
Here is the full example:
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.
来源:https://stackoverflow.com/questions/29565607/documentexists-fails-when-a-wildcard-is-used-in-its-type