2016-11-16 25 views
1

我嘗試在控制檯應用程序中使用內容查詢,但它引發了一個異常「未將對象引用設置爲對象的實例」。 請幫我解決這個問題。在控制檯應用程序中使用內容查詢的Sense/net

var startSettings = new RepositoryStartSettings 
      { 
       Console = Console.Out, 
       StartLuceneManager = false, 
       IsWebContext = false,      
       PluginsPath = AppDomain.CurrentDomain.BaseDirectory, 
      }; 

      using (Repository.Start(startSettings)) 
      { 
       try 
       { 
        string path = "/Root/Sites/Default_Site/workspaces/Document/HACCP/Document_Library/SanXuat/ChonLocChuanBiDiaDiemSXRau"; 
        string fieldName1 = "Name"; 

        var content = Content.Load(path); 

        int count = ContentQuery.Query(".AUTOFILTERS:OFF .COUNTONLY Infolder:" + path).Count; 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 

回答

3

如果要執行內容查詢,您必須啓用LuceneManager當您啓動庫,因爲該組件負責查詢。

new RepositoryStartSettings 
{ 
    Console = Console.Out, 
    StartLuceneManager = true, // <-- this is necessary 
    IsWebContext = false,      
    PluginsPath = AppDomain.CurrentDomain.BaseDirectory, 
} 

請確保所有配置值都已就位(例如,索引目錄路徑,啓用外部搜索引擎)。您可以從導出或導入工具的配置文件中複製它們。

一些更多的注意事項:

  • 中請務必在引號圍路徑表達式,因爲如果在路徑上的空間,它會導致查詢錯誤是很難找到一個內容查詢

    (因爲它會返回不同的結果集)。例如:

    InTree:「/根/我的文件夾」

或者你可以使用內置的參數功能,確保一致的:

// note the @0 parameter, which is a 0-based index 
ContentQuery.Query("+TypeIs:Article +InTree:@0", null, containerPath); 
+0

我試圖使StartLuceneManager =真。 但它引發一個異常:「System.InvalidCastException:無法強制類型'SenseNet.ContentRepository.Storage.Search.InternalSearchEngine'的類型的對象鍵入'SenseNet.Search.LuceneSearchEngine」。 – dinhienhy

+0

請確認您的應用配置文件中的* unity *配置已從* Export.exe *工具的配置中複製。例如,對於ISearchEngineImpl(指向LuceneSearchEngine類)應該有一個類型別名。我的猜測是,這是沒有配置,只有默認的空實現是在回購開始時加載的。 –

+0

Unity配置已經在應用配置文件中配置。但它仍然是錯誤的。 – dinhienhy

相關問題