在elasticsearch中使用NEST进行索引时,$ id始终为1

时间:2015-02-13 17:25:01

标签: c# .net elasticsearch entity-framework-6 nest

我已经在MVC5 / EF6中创建了一个应用程序,DB首先将一些日期索引到elasticsearch中。

示例:

public static ElasticClient ElasticClient
        {
            get
            {
                Uri uri = new Uri("http://localhost:9200");
                var setting = new ConnectionSettings(uri, "crude");
                setting.SetJsonSerializerSettingsModifier(x =>
                {
                    x.ContractResolver = new CustomContractResolver();
                    x.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    x.PreserveReferencesHandling = PreserveReferencesHandling.All;
                    x.NullValueHandling = NullValueHandling.Ignore;
                    x.Formatting = Formatting.None;

                });
                return new ElasticClient(setting);
            }
        }

ElasticClient.IndexMany<Customer>(db.Customer.Where(x => x.Active == true).ToList());  

ElasticClient.IndexMany<CustomerContract>(db.CustomerContract.Where(a => a.Active == true).ToList());

我已经编写了自定义合约解析程序,因此他没有索引所有嵌套类型,它基本上丢弃了所有IOCollections:

using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMDBv2.Helpers
{
    public class CustomContractResolver : DefaultContractResolver 
    {
        protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, Newtonsoft.Json.MemberSerialization memberSerialization)
        {
            JsonProperty prop = base.CreateProperty(member, memberSerialization);

            if (prop.PropertyType.ToString().ToLower().Contains("icollection"))
            {
                prop.ShouldSerialize = obj => false;
            }

            return prop;
        }
    }
}

我看到的是,在建立索引后,$id始终为1

ElasticHQ Result

我在搜索时遇到问题:

 ElasticSearchClient.Search<CustomerContract>(body => body.AllIndices().Size(500).Query(query => query.Bool(b => b.Must(m => m.QueryString(qs => qs.Query("MyQRY")))))).Documents.ToList();

例外:

A different value already has the Id '1'

堆栈跟踪:

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.AddReference(JsonReader reader, String id, Object value)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Populate(JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerProxy.PopulateInternal(JsonReader reader, Object target)
   at Newtonsoft.Json.JsonSerializer.Populate(JsonReader reader, Object target)
   at Nest.ConcreteTypeConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in C:\code\elasticsearch-net\src\Nest\Resolvers\Converters\ConcreteTypeConverter.cs:line 90
   at Nest.DefaultHitConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in C:\code\elasticsearch-net\src\Nest\Resolvers\Converters\ConcreteTypeConverter.cs:line 36
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)

奇怪的是,他认识到ID的ID很好。我也不明白为什么他会在映射中创建一个$id字段:

enter image description here

那么,我怎样才能告诉NEST A)不创建$ id,或者B)给它一个合适的值?

0 个答案:

没有答案