简体   繁体   中英

ElasticSearch Nest Flags c#

Basicly I have a flag field similar to this

[Flags]
public enum MyFlags
{
   FirstFlag = 1,
   SecondFlag = 2
}

public class MyClass
{
    public int Id {get;set;}
    public MyFlags MyFlagProperty { get; set; }
}

Client.Search<T, TResult>(c=>c.Query ??? )

My problem here is that I cant find any way to search for entries where only my FirstFlag is required. Am I approaching this wrong? Should I use 2 properties instead?

public class MyClass
{
    public int Id {get;set;}
    public bool MyFirstFlag { get; set; }
    public bool MySecondFlag { get; set; }
}

Client.Search<T, TResult>(c => c.Query(d => d.Term("MyFirstFlag", true)));

What is the best approach here? Would you recommend to not use flags at all and just go by properties?

There are several approaches you could take here

  1. Make the property a set of enum values
  2. Use a custom JsonConverter to serialize an enum with the FlagsAttribute to a collection and deserialize a collection to an enum
  3. Have separate properties as you have suggested

I'd personally choose number 2 as it will allow you to work with the enum as is in code, yet store the field as a collection in Elasticsearch and easily query it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM