jeme commented on issue #858:
URL: https://github.com/apache/lucenenet/issues/858#issuecomment-1817804574
First of, your enum and use looks a bit weird to me, I would simply do:
```
// Dots for 0 as I think it makes it easier to see. Btw, Today you can
define enums as bit masks with: 0b_0000_0000, 0b_0000_0001, 0b_0000_0010 etc.
Before that we often used bit shifting to make it easier to define: Red = 1,
Blue = 1 << 1, Green = 1 << 2
[Flags]
public enum Colors
{
None=0, // ............
Red=0x1, // ...........1
Blue=0x2, // ..........1.
Green=0x4, // .........1..
Combination1 = 0x3f, // ......111111 -> I would not have this one, not
like this anyways.
Yellow = 0x40, // .....1......
Orange= 0x80, // ....1.......
Violet = 0x100, // ...1........
Combination2 = 0xFC0, // 111111...... -> I would not have this one, not
like this anyways.
All = 0xFFF // 111111111111 -> Having a All can make sense,
largely OK although we are setting flags here that is not defined, which is odd
to me.
// Instead of Combination1 and Combination2, If i REALLY wanted to define
them on the ENUM (which I would often not do), Then I would do this insead:
RedBlueGree = Red|Blue|Green // .........111
YellowOrangeViolet = Yellow|Oragen|Violet // ...111......
// However, they are bitwise not the same values as your Combination1 and
Combination2, as they set undefined flags, which the context of the issue lacks
an explanation off.
}
//Defining my data i would:
var temp = new List<Test>();
temp.Add(new Test{ Name= "ABC", Color = Colors.Red});
temp.Add(new Test{ Name= "DEF", Color = Colors.Red | Colors.Blue |
Colors.Green });
temp.Add(new Test{ Name= "PQR", Color = Colors.All});
temp.Add(new Test{ Name= "XYZ", Color = Colors.Orange | Colors.Yellow |
Colors.Violet});
temp.Add(new Test{ Name= "AAA", Color = Colors.Red | Colors.Blue });
temp.Add(new Test{ Name= "BBB", Color = Colors.Red | Colors.Yellow });
```
One simple way of doing this is to add the field for each color. So:
```CSharp
foreach(var val in temp)
{
var doc = new Document {nameField };
nameField.SetStringValue(val.Name);
if(val.Color == Colors.None) {
doc.AddField(new StringField("Color", "None", Field.Store.YES)
}
if(val.Color.HasFlag(Colors.Red)) {
doc.AddField(new StringField("Color", "Red", Field.Store.YES)
}
if(val.Color.HasFlag(Colors.Blue)) {
doc.AddField(new StringField("Color", "Blue", Field.Store.YES)
}
//... And so on - yes you can add the same field multiple times -
obviously this code scales badly for large enums. To do that better you need
first to decide what the combinations should add to the index. Then you can
look at
https://stackoverflow.com/questions/4171140/how-to-iterate-over-values-of-an-enum-having-flags
writer.AddDocument(doc);
}
```
Another ways is to use the text field, then concat the values so it becomes
"Red, Blue, Green" and then rely on that the Analyzer you use will make that
work for you. (As it will index Red, Blue, Green as separate terms)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]