{"id":271,"date":"2015-01-11T18:50:51","date_gmt":"2015-01-11T17:50:51","guid":{"rendered":"http:\/\/www.cymbeline.ch\/?p=271"},"modified":"2015-01-19T22:25:26","modified_gmt":"2015-01-19T21:25:26","slug":"lucene-net-object-mapping","status":"publish","type":"post","link":"https:\/\/cymbeline.ch\/2015\/01\/11\/lucene-net-object-mapping\/","title":{"rendered":"Lucene.Net Object Mapping"},"content":{"rendered":"
Today I finally took some time to turn a little library I’ve used for a while now into a NuGet package, called Lucene.Net.ObjectMapping<\/a><\/strong>. At the same time, I also uploaded the code to GitHub<\/a><\/strong>. But let’s look at Lucene.Net Object Mapping in more detail.<\/p>\n Since this is a NuGet package, installation is as simple as running the following command in the Package Manager Console<\/p>\n Alternatively, you can just search for Lucene.Net.ObjectMapping<\/em> in the package manager and you should find it.<\/p>\n Using object mapping is as simple as calling two methods: ToDocument<\/em> to convert an object into a document and ToObject<\/em> to convert a Document (that was created with the ToDocument<\/em> method) into the original object.<\/p>\n Under the covers, the library is JSON-serializing the object and stores the JSON in the actual Lucene.Net document. In addition, it stores some metadata like the actual and the static types of the object you stored, as well as the timestamp (ticks) of when the document was created. The type information is used when you search for documents that were created for a specific type. The static type is the type you pass in as the type parameter to ToDocument<\/em>, whereas the actual type is the actual (dynamic) type of the object you’re passing in. Since all this information is stored in the document too, there are no issues re-creating objects from an class hierarchy too. Each field is created with the following mapping of field types:<\/p>\n Let’s look at a simple example of an object and its mapping to a Lucene.Net Document. Consider the following object model.<\/p>\n The mapping rules called out above will add the following fields for searching to the document. Please note that I’m not calling out the fields needed for the internal workings of the Lucene.Net.ObjectMapping library.<\/p>\n The mapper is by no means complete. Ideas to extend it in the future exist, including functionality to<\/p>\n I’ll talk a little more on how to use this all when searching for documents in your Lucene.Net index. But as a sneak preview: the library also provides extension methods to the Searcher<\/em> class from Lucene.Net that you can use to specify an object type to filter your documents on.<\/p>\n <\/p>","protected":false},"excerpt":{"rendered":" Today I finally took some time to turn a little library I’ve used for a while now into a NuGet package, called Lucene.Net.ObjectMapping. At the same time, I also uploaded the code to GitHub. But let’s look at Lucene.Net Object Mapping in more detail. How To Install Since this is a NuGet package, installation is … Continue reading “Lucene.Net Object Mapping”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[17,12,77,78,79],"yoast_head":"\nHow To Install<\/h2>\n
Install-Package Lucene.Net.ObjectMapping<\/pre>\n
How To Use It?<\/h2>\n
\r\nMyObject obj = ...;\r\nDocument doc = obj.ToDocument();\r\n\/\/ Save the document to your Lucene.Net Index\r\n\r\n\/\/ Later, load the document from the index again\r\nDocument docFromIndex = ...;\r\nMyObject objFromDoc = docFromIndex.ToObject<MyObject>();\r\n<\/pre>\n
How does it work?<\/h2>\n
\nIn addition to storing the object information itself, the library also indexes the individual properties of the object you’re storing, including nested properties. By default, it uses a mapper which works as follows.<\/p>\n\n
\n
Example Mapping<\/h2>\n
\r\npublic class MyObject\r\n{\r\n public int Id { get; set; }\r\n public string Name { get; set; }\r\n public ObjectMeta Meta { get; set; }\r\n}\r\n\r\npublic class ObjectMeta\r\n{\r\n public DateTime LastModified { get; set; }\r\n public string ModifiedBy { get; set; }\r\n public string[] Modifications { get; set; }\r\n}\r\n\r\n\/\/ Create an instance of MyObject\r\nMyObject obj = new MyObject()\r\n{\r\n Id = 1234,\r\n Name = "My Lucene.Net mapped Object",\r\n Meta = new ObjectMeta()\r\n {\r\n LastModified = DateTime.UtcNow,\r\n ModifiedBy = "the dude",\r\n Modifications = new string[] { "changed a", "removed b", "added c" },\r\n },\r\n};\r\n\r\nDocument doc = obj.ToDocument();\r\n<\/pre>\n
\n
\n Field Name<\/th>\n Type<\/th>\n Value<\/th>\n<\/tr>\n \n Id<\/td>\n Numeric \/ Long<\/td>\n 1234<\/td>\n<\/tr>\n \n Name<\/td>\n String \/ ANALYZED<\/td>\n My Lucene.Net mapped Object<\/td>\n<\/tr>\n \n Meta.LastModified<\/td>\n Numeric \/ Long<\/td>\n < the number of ticks at the current time ><\/td>\n<\/tr>\n \n Meta.ModifiedBy<\/td>\n String \/ ANALYZED<\/td>\n the dude<\/td>\n<\/tr>\n \n Meta.Modifications<\/td>\n String \/ ANALYZED<\/td>\n changed a<\/td>\n<\/tr>\n \n Meta.Modifications<\/td>\n String \/ ANALYZED<\/td>\n removed b<\/td>\n<\/tr>\n \n Meta.Modifications<\/td>\n String \/ ANALYZED<\/td>\n added c<\/td>\n<\/tr>\n<\/table>\n \n