所以我要做的是使用ElasticSearch构建指定为SQL视图的各种索引模型的索引。
索引时我只是简单地做了:
public void IndexAllFoos()
{
var client = new ElasticClient(settings);
using (var db = new Db())
{
// where IndexModelFoos are an sql view.
var foos = db.IndexModelFoos.Select(p => p).ToList();
client.IndexMany(foos);
}
}
这没有任何问题。
当我尝试搜索索引时出现问题:
var client = new ElasticClient(settings);
client.Search<IndexModelFoos>.Search(s =>
s.Query(q =>
q.Bool(bq =>
bq.Must(m =>
m.QueryString(qs =>
qs.Query("bar")
)
)
)
)
);
当我这样做时,我得到以下异常:
{"A different value already has the Id '1'."}
经过一番搜索,我做到了这一点:
internal class CustomContractResolver : DefaultContractResolver
{
public override JsonContract ResolveContract(Type type)
{
var contract = base.ResolveContract(type);
contract.IsReference = false;
return contract;
}
}
var settings = new ConnectionSettings(...);
settings.SetJsonSerializerSettingsModifier(x =>
{
x.ContractResolver = new CustomContractResolver();
});
var client = new ElasticClient(settings);
它解决了整个&#34;一个不同的价值已经有了Id&#39; 1&#39;&#34;问题。 但是,执行此操作后,尝试使用相同的查询进行搜索时出现以下异常:
{"Could not create an instance of type Nest.IHit`1[Database.IndexModelFoo].
Type is an interface or abstract class and cannot be instantiated.
Path 'hits.hits[0]._index', line 1, position 133."}
对于此错误,我无法找到任何解决方案。因此问这里。有人对如何解决这个问题有任何想法吗?
注意: IndexModelFoo的类型为EntityObject,由Entity Framework
生成