Вопрос

I am using NEST to search an elasticsearch index in C#.
When I run the search query in the Google Chrome extension Sense it works.
But when I try to search from C# code I get the following exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: JsonConverter DictionaryKeysAreNotPropertyNamesJsonConverter on System.Collections.Generic.IDictionary2[System.String,System.Double] _IndicesBoost is not compatible with member type IDictionary2.

My C# class looks like this:

[ElasticType(Name = "ElasticSearchModel")]
public class ElasticSearchModel
{
    [ElasticProperty(Type = FieldType.string_type)]
    public string Id { get; private set; }
    [ElasticProperty(Type = FieldType.string_type)]
    public string Url { get; set; }
    protected Dictionary<string, string> Properties { get; set; }
    [ElasticProperty(Type = FieldType.nested)]
    public Array ModelProperties 
    {
        get
        {
            List<string[]> returnvalue = new List<string[]>();
            foreach (var keyvaluepair in Properties)
            {
                if (!string.IsNullOrEmpty(keyvaluepair.Key))
                {
                    string[] props = new string[2];
                    props[0] = keyvaluepair.Key;
                    props[1] = keyvaluepair.Value;
                    returnvalue.Add(props);
                }
            }

            return returnvalue.ToArray();
        }
    }

    public ElasticSearchModel(string id)
    {
        this.Id = id;
        this.Properties = new Dictionary<string, string>();
    }

    public void AddProperty(string key, string value)
    {
        Properties.Add(key, value);
    }
}


And this code is used to get the search results:

var descriptor = new SearchDescriptor<ElasticSearchModel>().Indices(new string[] { "kickstartconcept" });
searchresult = ElasticClient.Search(descriptor.QueryString(terms)); //Where terms is a string with the content of the query


I tried first to index the dictionary but that gave the same exception.
So I tried it with the Array but that doesn't work either.
Does somebody have an idea where the exception comes from and how to solve it?
Thanks for your time, Corné

(EDIT) This is an entry in my ElasticSearch index:

"_source": {
           "id": "1082",
           "url": "/",
           "modelProperties": [
              [
                 "title",
                 "MyPageTitle"
              ],
              [
                 "content",
                 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec lacus luctus, pulvinar ante vitae, feugiat velit. In gravida, urna ac lacinia tincidunt, metus erat pellentesque sem, sed congue massa velit non enim. Suspendisse metus sapien, ornare vel lectus ut, pulvinar feugiat justo. In diam metus, ultricies id augue interdum, dapibus placerat est. Cras vel nulla sed arcu dictum molestie eu ut sem. Suspendisse potenti. Ut mattis odio a aliquam vehicula. Proin varius commodo quam, sed semper orci mattis et. Suspendisse lacinia purus quis arcu semper rhoncus. Integer ut quam ut elit pharetra malesuada."
              ],
              [
                 "hideinnavigation",
                 "0"
              ],
              [
                 "subtitle",
                 ""
              ],
              [
                 "gridLayout",
                 "<content><area name=\"Body\"><block>1137</block><block>1139</block><block>1080</block></area><area name=\"Footer\"><block>1080</block></area></content>"
              ],
              [
                 "searchpage",
                 ""
              ]
           ]
        }
Это было полезно?

Решение 2

I could not fix this issue.
So I decided to recreate the whole thing.

My new model looks like this:

public class ElasticSearchDocument
{
    public string Id { get; set; }
    public Dictionary<string, string> Properties { get; private set; }

    // Constructors and methods
}

I removed all the data anotations from the properties and recreated the elasticsearch schema.
Next I updated NEST to version 1.0 (PreRelease).

Now I can search using this line:

SearchDescriptor<ElasticSearchDocument> descriptor = new SearchDescriptor<ElasticSearchDocument>();
descriptor.AllTypes();
descriptor.QueryString(term);
descriptor.Size(20);

// Search
var results = c.Search<ElasticSearchDocument>(d => d = descriptor);

Haven't had a problem for a while now. I hope that maybe this helps for somebody.

Другие советы

Thanks for posting the sample item from your index. If you want to retrieve hits that match that exact structure, change your class to have the following property.

 [ElasticType(Index = FieldType.@object]
 public List<Dictionary<string,string>> ModelProperties { get; set; }

If you are looking for more of a pure Dictionary use the following:

 [ElasticType(Index = FieldType.@object]
 public Dictionary<string,string> ModelProperties { get; set; }

But in order to accommodate this, you would need to change your index item to the following:

   {
       "id": "1082",
       "url": "/",
       "modelProperties": {
          "title": "MyPageTitle",
          "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec lacus luctus, pulvinar ante vitae, feugiat velit. In gravida, urna ac lacinia tincidunt, metus erat pellentesque sem, sed congue massa velit non enim. Suspendisse metus sapien, ornare vel lectus ut, pulvinar feugiat justo. In diam metus, ultricies id augue interdum, dapibus placerat est. Cras vel nulla sed arcu dictum molestie eu ut sem. Suspendisse potenti. Ut mattis odio a aliquam vehicula. Proin varius commodo quam, sed semper orci mattis et. Suspendisse lacinia purus quis arcu semper rhoncus. Integer ut quam ut elit pharetra malesuada.",
          "hideinnavigation": "0",
          "subtitle": "",
          "gridLayout": "<content><area name=\"Body\"><block>1137</block><block>1139</block><block>1080</block></area><area name=\"Footer\"><block>1080</block></area></content>",   
          "searchpage": ""
       }
    }

Hope this helps...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top