该字段未编入索引,无法查询未在Raven.Database.Indexing中编制索引的字段

时间:2017-07-11 16:37:28

标签: c# ravendb hangfire

我是RavenDB和HangFire的新手,我正在创建后台任务,但是当我查询ravendb文档时出现索引错误,如下所示:

    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)

我很好奇如何修复此错误以及原因。谢谢。

1 个答案:

答案 0 :(得分:5)

您使用属性'DndUntilUtc'查询索引'HotelRoom_Search',但索引不包含它。

在lucene / ravendb世界中,如果按特定属性查询索引[虽然集合(或sql world中的表)包含它],但只有当索引包含它时才会起作用。 只需将字段“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);

更新:

要克服这个问题,您还可以在不指定索引的情况下查询对象HotelRoom(仅将实体作为T传递给session.Query)。这将使ravendb根据您在查询中使用的属性创建一个Auto索引。

ravendb docs:

  

当用户发出查询时,服务器使用索引来满足查询。如果匹配查询,RavenDB将使用现有索引,如果未找到匹配,则创建新索引。   通过发出查询创建的索引称为动态索引或自动索引,并且由于其名称以“自动/前缀”开头,因此可以轻松识别。   用户明确创建的索引称为静态。

通常你想控制乌鸦索引(和检索)文档调整性能的方式,所以它不是一个好的选择,但是默认好。

当然不能解决这个问题。