2017-07-11 40 views
0

索引我非常新RavenDB和遲髮型和我創建一個後臺任務字段不能查詢,但我得到一個索引錯誤,當我查詢ravendb文件,如下圖所示:場不被索引,對未在Raven.Database.Indexing

public static void UpdateDoNotDisturb() 
    { 
     IList<HotelRoom> rooms = new List<HotelRoom>(); 
     Hotel project; 
     IQueryable<HotelRoom> queryableObj; 
     //Update Rooms with DND status and DndUntil greater than now. 
     using (var session = MvcApplication.DocumentStore.OpenSession()) 
     { 
      queryableObj = session.Query<HotelRoom, HotelRoom_Search>().Where(x => x.Status == "DND" && x.DndUntilUtc < DateTime.Now).OfType<HotelRoom>(); 
      //queryableObj = session.Query<HotelRoom, HotelRoom_Search>().Where(x => x.Status == "DND").OfType<HotelRoom>(); 
      using (var enumerator = session.Advanced.Stream(queryableObj)) 
      { 
       while (enumerator.MoveNext()) 
       { 
        var room = enumerator.Current.Document; 
        rooms.Add(room); 
       } 
      } 
     } 

    } 

的文件:

{ 
    "HotelId": "hotels/1474", 
    "RoomNumber": "407", 
    "Floor": "4th Floor", 
    "Status": "DND", 
    "RoomType": "Guest Room", 
    "RoomTemplate": "1474_Install_SBBs.json", 
    "Json": { 
    "main": { 
     "picRoomNumber": "Images/Photos/d5/d5de2652-08c3-49c7-a75a-446934962a1d.jpg", 
     "qrCode": "http://2d.my/36tn" 
    }, 
    "taskSBB1": { 
     "deviceTvModelValue": "Other", 
     "tvLocation": "None", 
     "deviceTvModel": "Other", 
     "itemSbbRemoved": 0, 
     "deviceSbbQr": "http://2d.my/36tm", 
     "deviceTvMountType": "Articulating wall mount", 
     "deviceTvMountTypeValue": "Articulating wall mount", 
     "tvLocationValue": "", 
     "taskTechName": "Anton Anskin", 
     "taskName": "Install SBB", 
     "deviceTvSn": "703rmcj83595", 
     "taskStatus": "In Progress", 
     "deviceSbbPhotoMount": "Images/Photos/84/8410b77b-20f2-4146-b9cb-19e9dd3259ef.jpg", 
     "taskStarted": "2017-06-06T14:50:34.046000Z" 
    }, 
    "tasks": [ 
     "taskSBB1" 
    ], 
    "survey": { 
     "sbbCount": "1", 
     "damageBoolean": 0 
    } 
    }, 
    "LastUpdatedBy": "Mark", 
    "LastUpdatedUtc": "2017-07-10T18:46:27.9305296Z", 
    "StartedBy": null, 
    "StartedUtc": "2017-06-06T14:50:34.0460000Z", 
    "CompletedBy": null, 
    "CompletedUtc": "0001-01-01T00:00:00.0000000", 
    "CompletedDate": "0001-01-01T00:00:00.0000000", 
    "DndBy": "Mark", 
    "DndUntilUtc": "2017-07-10T21:46:00.0000000Z", 
    "RoomPhotos": [ 
    { 
     "Key": "main", 
     "Name": "picRoomNumber", 
     "ImagePath": "Images/Photos/d5/d5de2652-08c3-49c7-a75a-446934962a1d.jpg", 
     "AddedBy": "Anton", 
     "AddedUtc": "2017-06-06T14:51:16.6010049Z" 
    }, 
    { 
     "Key": "taskSBB1", 
     "Name": "deviceSbbPhotoMount", 
     "ImagePath": "Images/Photos/84/8410b77b-20f2-4146-b9cb-19e9dd3259ef.jpg", 
     "AddedBy": "Anton", 
     "AddedUtc": "2017-06-06T14:51:17.2572542Z" 
    } 
    ], 
    "Issues": [], 
    "Scans": [], 
    "ChangeLog": [], 
    "CurrentPhase": null 
} 

而我得到的錯誤:

 "Error": "System.ArgumentException: The field 'DndUntilUtc' is not indexed, cannot query on fields that are not indexed\r\n at Raven.Database.Indexing.Index.AssertQueryDoesNotContainFieldsThatAreNotIndexed(IndexQuery indexQuery, AbstractViewGenerator viewGenerator)\r\n at Raven.Database.Indexing.Index.IndexQueryOperation.<Query>d__57.MoveNext()\r\n at Raven.Database.Util.ActiveEnumerable`1..ctor(IEnumerable`1 enumerable)\r\n at Raven.Database.DocumentDatabase.<>c__DisplayClass96.<Query>b__8b(IStorageActionsAccessor actions)\r\n at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action, EsentTransactionContext transactionContext)\r\n at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action)\r\n at Raven.Database.DocumentDatabase.Query(String index, IndexQuery query, CancellationToken externalCancellationToken, Action`1 headerInfo, Action`1 onResult)\r\n at Raven.Database.Server.Responders.QueryStreams.Respond(IHttpContext context)\r\n at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx)\r\n at Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx)" 
    } 

     Source=Raven.Client.Lightweight 
     StackTrace: 
     at Raven.Client.Connection.HttpJsonRequest.RawExecuteRequest() 
     at Raven.Client.Connection.ServerClient.StreamQuery(String index, IndexQuery query, QueryHeaderInformation& queryHeaderInfo) 
     at Raven.Client.Document.DocumentSession.Stream[T](IDocumentQuery`1 query, QueryHeaderInformation& queryHeaderInformation) 
     at Raven.Client.Document.DocumentSession.Stream[T](IQueryable`1 query, QueryHeaderInformation& queryHeaderInformation) 
     at Raven.Client.Document.DocumentSession.Stream[T](IQueryable`1 query) 

我很好奇,如何解決這個問題的錯誤及其原因。謝謝。

回答

4

你查詢與屬性「DndUntilUtc」索引「HotelRoom_Search」,但指數不包含它。

在lucene/ravendb世界中,如果您通過特定屬性查詢索引[,雖然集合(或sql世界中的表)包含它],但只有在索引包含該索引時纔有效。 僅僅是場「DndUntilUtc」添加到您的索引。

你沒有包含在您的索引定義,所以我會猜它是這樣的:

public class HotelRoom_Search : AbstractIndexCreationTask<HotelRoom> 
{ 
    public HotelRoom_Search() 
    { 
     Map = rooms => from room in rooms 
         select new 
         { 
          //bla bla 
          room.DndUntilUtc // add this line 
          //bla bla 
         }; 

     Indexes.Add(x => x.DndUntilUtc, FieldIndexing.Analyzed); 

    } 
} 

,不要忘了運行這一點,所以服務器將索引定義的更新:

var store = new DocumentStore { //....} 
new HotelRoom_Search().Execute(Store); 

UPDATE:

克服這一點,你也可以查詢對象的空房不指定索引(通過只是實體爲T到session.Query)。這會使根據您在查詢中使用的屬性ravendb創建自動索引。

ravendb文檔:

索引用於通過服務器來滿足查詢,只要用戶發出一個查詢,如果它匹配查詢;如果沒有找到匹配創建一個新的RavenDB將使用現有的索引。 通過發出查詢創建的索引稱爲動態索引或自動索引,並且可以很容易地識別,因爲它們的名稱以自動/前綴開頭。由用戶明確創建 索引是calledstatic。

通常你想控制烏鴉索引(和檢索)文檔的方式來調整性能,所以它不是一個好的選項,但是很好默認。

當然,這不會修復錯誤。

+0

非常感謝你的詳細方法和原因。非常確切的答案。 – yams

+1

不客氣。順便說一句,我強烈建議閱讀有關lucene,因爲ravendb是基於lucene。嘗試書中lucene的行動,舉止酒吧。如果你接受它作爲答案,我將不勝感激。 –