mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

[QUESTION] Complex Index using calculated values of field #2166

Closed dethknite closed 2 years ago

dethknite commented 2 years ago

Is it possible to create a complex index such that it has concatenated values calculated from fields. (Example): Fields Description, DateTime

I want to make an index that contains "Description + DateTime + "#year=" + DateTime.ToString("yyyy") + "#dow=" + DateTime.DayOfWeek.ToString() ... etc ... so that I can do a query EQ or Contains or the like and hit it.

From the Documentation I have found.. I can figure this much: col.EnsureIndex("textsearch", "$.Description + ';' + $DateTime", false);

How would one manage to apply DateTime.ToString() on the values such as col.EnsureIndex("textsearch", "$.Description + ';' + $DateTime + ';#year=' + $DateTime.ToString('yyyy')", false);

Or is this even possible?

blackholeearth commented 2 years ago

did you tried this ?

use stopwatch

 class mymodel
{
    public string name{ get; set; }
    public datetime birthdate { get; set; }
    public string description { get; set; }

    public bool complexProperty_Indexed =>   description + "; " +  "year=" + birthdate.ToString("yyyy")

    public bool complexProperty_NotIndexed =>  description + "; " +  "year=" + birthdate.ToString("yyyy")
 }

usage :

  // add 50k item tol table
  //todo ...

  col.EnsureIndex( x=> x.complexProperty_Indexed , false);

  // will make table scan from first to last
   string  valuecantbefound =  "findsometing appropriate"; 

  //test time differences stopwatch -
  va sw = new stopwatch();
  sw.start();
  col.Find(x=>  complexProperty_Indexed == valuecantbefound  );
  sw.stop();
  console.writeline("indexed: elapsed_ms:" + sw.elapsedmiliseconds );

  sw = new stopwatch();
  sw.start();
  col.Find(x=>  complexProperty_NotIndexed == valuecantbefound  );
  sw.stop();
  console.writeline("NOT_indexed: elapsed_ms:" + sw.elapsedmiliseconds );

https://www.litedb.org/docs/indexes/

dethknite commented 2 years ago

The public getter/setter logic here is what I was aiming for. Thanks.