{"id":311,"date":"2015-01-30T18:30:34","date_gmt":"2015-01-30T17:30:34","guid":{"rendered":"http:\/\/www.cymbeline.ch\/?p=311"},"modified":"2015-02-10T21:48:16","modified_gmt":"2015-02-10T20:48:16","slug":"improvements-lucene-net-objectmapping","status":"publish","type":"post","link":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/","title":{"rendered":"Improvements to Lucene.Net.ObjectMapping"},"content":{"rendered":"

I’d like to discuss some improvements to Lucene.Net.ObjectMapping which I published yesterday as a new version (1.0.3) to NuGet<\/a>. In addition, I want to take this opportunity to give a quick outlook on what’s to come next.<\/p>\n

CRUD Operations<\/h2>\n

The library now comes with support for all of the CRUD operations. Let’s look at them one by one, starting with Create.<\/p>\n

Create \/ Add<\/h3>\n

In Lucene.Net terms, that would be AddDocument. Since the library does object to document mapping, this is simplified to an Add operation.<\/p>\n

\r\nIndexWriter myIndexWriter = ...;\r\nMyClass myObject = new MyClass(...);\r\n\r\nmyIndexWriter.Add(myObject);\r\n<\/pre>\n

Or, if you need a specific analyzer for the document the object gets mapped to, you can use the overload which accepts a second parameter of type Analyzer.<\/p>\n

\r\nIndexWriter myIndexWriter = ...;\r\nMyClass myObject = new MyClass(...);\r\n\r\nmyIndexWriter.Add(myObject, new MyOwnAnalyzer());\r\n<\/pre>\n

Retrieve \/ Query<\/h3>\n

The retrieve operation, or mapping of a document to an object hasn’t changed since v1.0.0. There are examples for how to query and retrieve in my previous post<\/a>. Of course, if you happen to know the ID of the document without a query, then you can just map that document to your class without going through a query. But since the document IDs can change over time, it’s usually more practical to pivot off a query.<\/p>\n

Update<\/h3>\n

Update is maybe the most interesting operation here. Since document IDs can change over time, there’s really no good way to reliably update a specific document, without making a query. That’s why the UpdateDocument method from the IndexReader asks you for a query\/term to use to match the document to update. And that’s why it’s generally a good idea to bring your own unique identifier to the game. Suppose your class has a property of type Guid and name “Id”, which is used as your unique identifier for the objects of that type.<\/p>\n

\r\nIndexWriter myIndexWriter = ...;\r\nMyClass myObject = ...;\r\n\r\nmyObject.MyPropertyToUpdate = "new value";\r\n\r\nmyIndexWriter.Update(\r\n    myObject,\r\n    new TermQuery(new Term("Id", myObject.Id.ToString())));\r\n<\/pre>\n

Under the covers, this will find all the documents matching the query and matching the type (MyClass), delete them and then add a new document for the mapped myObject. If you need an analyzer, for the newly mapped document, you can use the second overload.<\/p>\n

\r\nIndexWriter myIndexWriter = ...;\r\nMyClass myObject = ...;\r\n\r\nmyObject.MyPropertyToUpdate = "new value";\r\n\r\nmyIndexWriter.Update(\r\n    myObject,\r\n    new TermQuery(new Term("Id", myObject.Id.ToString())),\r\n    new MyOwnAnalyzer());\r\n<\/pre>\n

Delete<\/h3>\n

Just like the retrieve operation, the Delete operation is also supported since v1.0.0. I realize though that I haven’t given any examples yet. But really, it’s quite simple again. You give the type of objects you want to delete the mapped documents for, and you give a query to identify the objects to delete. No magic at all.<\/p>\n

\r\nIndexWriter myIndexWriter = ...;\r\nmyIndexWriter.DeleteDocuments<MyClass>(\r\n    new TermQuery(new Term("Tag", "deleted")));\r\n<\/pre>\n

Naturally, you can use any Query you want for the delete operation (as well as for updates). You can make them arbitrarily complex as long as they’re still supported by Lucene.Net.<\/p>\n

Summary and Outlook<\/h2>\n

That’s it, CRUD with no magic, no tricks. Let me know if there’s functionality you’d like to see added, either by commenting here or by opening a bug\/enhancement\/whatever on GitHub<\/a>. I’ve started working on LINQ support for the ObjectMapping library too, with the goal that you can write LINQ queries like the following.<\/p>\n

\r\nvar query = from myObject in mySearcher.AsQueryable<MyClass>()\r\n            where myObject.Tag == "history"\r\n            select myObject;\r\n<\/pre>\n

It will likely take a little longer to get that stable, but I’ll try to make a pre-release on NuGet in the next few weeks.<\/p>\n

<\/p>","protected":false},"excerpt":{"rendered":"

I’d like to discuss some improvements to Lucene.Net.ObjectMapping which I published yesterday as a new version (1.0.3) to NuGet. In addition, I want to take this opportunity to give a quick outlook on what’s to come next. CRUD Operations The library now comes with support for all of the CRUD operations. Let’s look at them … Continue reading “Improvements to Lucene.Net.ObjectMapping”<\/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,80,77,78,79],"yoast_head":"\nImprovements to Lucene.Net.ObjectMapping - Tales of a Code Monkey<\/title>\n<meta name=\"description\" content=\"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Improvements to Lucene.Net.ObjectMapping - Tales of a Code Monkey\" \/>\n<meta property=\"og:description\" content=\"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/\" \/>\n<meta property=\"og:site_name\" content=\"Tales of a Code Monkey\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-30T17:30:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-02-10T20:48:16+00:00\" \/>\n<meta name=\"author\" content=\"roger\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"roger\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/\",\"url\":\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/\",\"name\":\"Improvements to Lucene.Net.ObjectMapping - Tales of a Code Monkey\",\"isPartOf\":{\"@id\":\"https:\/\/cymbeline.ch\/#website\"},\"datePublished\":\"2015-01-30T17:30:34+00:00\",\"dateModified\":\"2015-02-10T20:48:16+00:00\",\"author\":{\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\"},\"description\":\"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.\",\"breadcrumb\":{\"@id\":\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cymbeline.ch\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Improvements to Lucene.Net.ObjectMapping\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cymbeline.ch\/#website\",\"url\":\"https:\/\/cymbeline.ch\/\",\"name\":\"Tales of a Code Monkey\",\"description\":\"... the adventures of a guy making software.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cymbeline.ch\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\",\"name\":\"roger\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"caption\":\"roger\"},\"sameAs\":[\"https:\/\/www.cymbeline.ch\"],\"url\":\"https:\/\/cymbeline.ch\/author\/roger\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Improvements to Lucene.Net.ObjectMapping - Tales of a Code Monkey","description":"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/","og_locale":"en_US","og_type":"article","og_title":"Improvements to Lucene.Net.ObjectMapping - Tales of a Code Monkey","og_description":"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.","og_url":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/","og_site_name":"Tales of a Code Monkey","article_published_time":"2015-01-30T17:30:34+00:00","article_modified_time":"2015-02-10T20:48:16+00:00","author":"roger","twitter_misc":{"Written by":"roger","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/","url":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/","name":"Improvements to Lucene.Net.ObjectMapping - Tales of a Code Monkey","isPartOf":{"@id":"https:\/\/cymbeline.ch\/#website"},"datePublished":"2015-01-30T17:30:34+00:00","dateModified":"2015-02-10T20:48:16+00:00","author":{"@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916"},"description":"Mapped Objects in Lucene.Net -- Improvements to Lucene.Net.ObjectMapping added with version 1.0.3, full support for CRUD operations.","breadcrumb":{"@id":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cymbeline.ch\/2015\/01\/30\/improvements-lucene-net-objectmapping\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cymbeline.ch\/"},{"@type":"ListItem","position":2,"name":"Improvements to Lucene.Net.ObjectMapping"}]},{"@type":"WebSite","@id":"https:\/\/cymbeline.ch\/#website","url":"https:\/\/cymbeline.ch\/","name":"Tales of a Code Monkey","description":"... the adventures of a guy making software.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cymbeline.ch\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916","name":"roger","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","caption":"roger"},"sameAs":["https:\/\/www.cymbeline.ch"],"url":"https:\/\/cymbeline.ch\/author\/roger\/"}]}},"_links":{"self":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/311"}],"collection":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":14,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/311\/revisions\/326"}],"wp:attachment":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}