1

I try to use content query in console application but it throw an exception "Object reference not set to an instance of an object". Please give help me resolve that problem.

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);
                }  
           }
dinhienhy
  • 15
  • 6

1 Answers1

3

if you want to execute a content query, you have to enable LuceneManager when you start the repository, because that component is responsible for querying.

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

Please make sure that all the config values are in place (e.g. index directory path, enable outer search engine). You can copy them from the Export or Import tool's config file.

A few more notes:

  • in a content query please always enclose path expressions in quotes, because if there is a space in the path, it causes a query error that is hard to find (because it would return a different result set). For example:

    InTree:'/Root/My Folder'

Or you can use the built-in parameter feature that makes sure the same:

// note the @0 parameter, which is a 0-based index
ContentQuery.Query("+TypeIs:Article +InTree:@0", null, containerPath); 
Miklós Tóth
  • 1,490
  • 13
  • 19
  • I tried to enable StartLuceneManager = true. but it throw an exception: "System.InvalidCastException: Unable to cast object of type 'SenseNet.ContentRepository.Storage.Search.InternalSearchEngine' to type 'SenseNet.Search.LuceneSearchEngine". – dinhienhy Nov 17 '16 at 16:19
  • Please confirm that you have the *unity* configs in your app config file copied from the *Export.exe* tool's configuration. For example you should have a type alias for ISearchEngineImpl (that points to the LuceneSearchEngine class). My guess is that this is not configured and only the default empty implementation is loaded when the repo starts. – Miklós Tóth Nov 17 '16 at 16:29
  • Unity config has already configured in app config file. But it's still error. – dinhienhy Nov 18 '16 at 09:51
  • Please also check that the appSettings/EnableOuterSearchEngine line is also there in the config. It has to be set to 'true'. – Miklós Tóth Nov 20 '16 at 15:20