Exemplo n.º 1
0
        public void FullTextDatasetLucene1()
        {
            //Lucene Index
            Directory             dir     = new RAMDirectory();
            LuceneSubjectsIndexer indexer = new LuceneSubjectsIndexer(dir, new StandardAnalyzer(LuceneTestHarness.LuceneVersion), new DefaultIndexSchema());

            //Test Dataset
            InMemoryDataset        memData = new InMemoryDataset();
            FullTextIndexedDataset dataset = new FullTextIndexedDataset(memData, indexer, false);

            //Test Graph
            Graph g = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");

            dataset.AddGraph(g);
            Assert.True(dataset.HasGraph(g.BaseUri), "Graph should exist in dataset");

            //Now do a search to check all the triples got indexed
            String searchTerm = "http";
            IEnumerable <Triple> searchTriples = g.Triples.Where(t => t.Object.NodeType == NodeType.Literal && t.Object.ToString().Contains("http"));
            LuceneSearchProvider searcher      = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, dir);

            foreach (Triple searchTriple in searchTriples)
            {
                INode targetNode = searchTriple.Subject;
                IEnumerable <IFullTextSearchResult> results = searcher.Match(searchTerm);
                Assert.True(results.Any(r => r.Node.Equals(targetNode)), "Did not find expected node " + targetNode.ToString(this._formatter) + " in search results using search term '" + searchTerm + "' (found " + results.Count() + " results)");
                Console.WriteLine();
            }

            //Now remove the Graph
            dataset.RemoveGraph(g.BaseUri);

            //Repeat the search to check all the triples got unindexed
            foreach (Triple searchTriple in searchTriples)
            {
                INode targetNode = searchTriple.Subject;
                IEnumerable <IFullTextSearchResult> results = searcher.Match(searchTerm);
                Assert.False(results.Any(r => r.Node.Equals(targetNode)), "Found unexpected node " + targetNode.ToString(this._formatter) + " in search results using search term '" + searchTerm + "' (found " + results.Count() + " results)");
                Console.WriteLine();
            }

            searcher.Dispose();
            indexer.Dispose();
        }
        /// <summary>
        /// Tries to load an object based on information from the Configuration Graph.
        /// </summary>
        /// <param name="g">Configuration Graph.</param>
        /// <param name="objNode">Object Node.</param>
        /// <param name="targetType">Target Type.</param>
        /// <param name="obj">Output Object.</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            INode index           = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "index"));
            INode indexerProperty = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "indexer"));
            INode searcher        = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "searcher"));
            INode analyzer        = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "analyzer"));
            INode schema          = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "schema"));
            INode version         = g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "version"));

            Object tempIndex, tempAnalyzer, tempSchema;
            int    ver = DefaultVersion;

            //Always check for the version
            ver = ConfigurationLoader.GetConfigurationInt32(g, objNode, version, DefaultVersion);

            switch (targetType.FullName)
            {
            case DefaultIndexSchema:
                obj = new DefaultIndexSchema();
                break;

            case FullTextIndexedDataset:
                //Need to get the inner dataset
                INode datasetNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingDataset)));
                if (datasetNode == null)
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Indexed Dataset specified by the Node '" + objNode.ToString() + "' as there was no value specified for the required dnr:usingDataset property");
                }
                Object tempDataset = ConfigurationLoader.LoadObject(g, datasetNode);
                if (tempDataset is ISparqlDataset)
                {
                    //Then load the indexer associated with the dataset
                    INode indexerNode = ConfigurationLoader.GetConfigurationNode(g, objNode, indexerProperty);
                    if (indexerNode == null)
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Full Text Indexed Dataset specified by the Node '" + objNode.ToString() + " as there was no value specified for the required dnr-ft:indexer property");
                    }
                    Object tempIndexer = ConfigurationLoader.LoadObject(g, indexerNode);
                    if (tempIndexer is IFullTextIndexer)
                    {
                        bool indexNow = ConfigurationLoader.GetConfigurationBoolean(g, objNode, g.CreateUriNode(UriFactory.Create(FullTextHelper.PropertyIndexNow)), false);
                        obj = new FullTextIndexedDataset((ISparqlDataset)tempDataset, (IFullTextIndexer)tempIndexer, indexNow);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Full Text Indexed Dataset specified by the Node '" + objNode.ToString() + "' as the value specified for the dnr-ft:indexer property pointed to an object which could not be loaded as a type that implements the required IFullTextIndexer interface");
                    }
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Indexed Dataset specified by the Node '" + objNode.ToString() + "' as the value specified for the dnr:usingDataset property pointed to an object which could not be loaded as a type that implements the required ISparqlDataset interface");
                }
                break;

            case FullTextOptimiser:
                //Need to get the Search Provider
                INode providerNode = ConfigurationLoader.GetConfigurationNode(g, objNode, searcher);
                if (providerNode == null)
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Optimiser specified by the Node '" + objNode.ToString() + "' as there was no value specified for the required dnr-ft:searcher property");
                }
                Object tempSearcher = ConfigurationLoader.LoadObject(g, providerNode);
                if (tempSearcher is IFullTextSearchProvider)
                {
                    obj = new FullTextOptimiser((IFullTextSearchProvider)tempSearcher);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Optimiser specified by the Node '" + objNode.ToString() + "' as the value specified for the dnr-ft:searcher property pointed to an object which could not be loaded as a type that implements the required IFullTextSearchProvider interface");
                }
                break;

            case LuceneObjectsIndexer:
            case LucenePredicatesIndexer:
            case LuceneSubjectsIndexer:
            case LuceneSearchProvider:
                //For any Lucene Indexer/Search Provider need to know the Index, Analyzer and Schema to be used

                //Then get the Index
                tempIndex = ConfigurationLoader.GetConfigurationNode(g, objNode, index);
                if (tempIndex == null)
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as there was no value specified for the required dnr-ft:index property");
                }
                tempIndex = ConfigurationLoader.LoadObject(g, (INode)tempIndex);
                if (tempIndex is Directory)
                {
                    //Next get the Analyzer (assume Standard if none specified)
                    tempAnalyzer = ConfigurationLoader.GetConfigurationNode(g, objNode, analyzer);
                    if (tempAnalyzer == null)
                    {
                        tempAnalyzer = new StandardAnalyzer(this.GetLuceneVersion(ver));
                    }
                    else
                    {
                        tempAnalyzer = ConfigurationLoader.LoadObject(g, (INode)tempAnalyzer);
                    }

                    if (tempAnalyzer is Analyzer)
                    {
                        //Finally get the Schema (assume Default if none specified)
                        tempSchema = ConfigurationLoader.GetConfigurationNode(g, objNode, schema);
                        if (tempSchema == null)
                        {
                            tempSchema = new DefaultIndexSchema();
                        }
                        else
                        {
                            tempSchema = ConfigurationLoader.LoadObject(g, (INode)tempSchema);
                        }

                        if (tempSchema is IFullTextIndexSchema)
                        {
                            //Now we can create the Object
                            switch (targetType.FullName)
                            {
                            case LuceneObjectsIndexer:
                                obj = new LuceneObjectsIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LucenePredicatesIndexer:
                                obj = new LucenePredicatesIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LuceneSubjectsIndexer:
                                obj = new LuceneSubjectsIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LuceneSearchProvider:
                                //Before the Search Provider has been loaded determine whether we need to carry out auto-indexing
                                List <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "buildIndexFor"))).ToList();
                                if (sources.Count > 0)
                                {
                                    //If there are sources to index ensure we have an indexer to index with
                                    INode indexerNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "buildIndexWith")));
                                    if (indexerNode == null)
                                    {
                                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as there were values specified for the dnr-ft:buildIndexFor property but no dnr-ft:buildIndexWith property was found");
                                    }
                                    IFullTextIndexer indexer = ConfigurationLoader.LoadObject(g, indexerNode) as IFullTextIndexer;
                                    if (indexer == null)
                                    {
                                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:buildIndexWith property pointed to an Object which could not be loaded as a type that implements the required IFullTextIndexer interface");
                                    }

                                    try
                                    {
                                        //For Each Source load it and Index it
                                        foreach (INode sourceNode in sources)
                                        {
                                            Object source = ConfigurationLoader.LoadObject(g, sourceNode);
                                            if (source is ISparqlDataset)
                                            {
                                                indexer.Index((ISparqlDataset)source);
                                            }
                                            else if (source is ITripleStore)
                                            {
                                                foreach (IGraph graph in ((ITripleStore)source).Graphs)
                                                {
                                                    indexer.Index(graph);
                                                }
                                            }
                                            else if (source is IGraph)
                                            {
                                                indexer.Index((IGraph)source);
                                            }
                                            else
                                            {
                                                throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as a value given for the dnr-ft:buildIndexFor property ('" + sourceNode.ToString() + "') pointed to an Object which could not be loaded as a type that implements one of the required interfaces: IGraph, ITripleStore or ISparqlDataset");
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        indexer.Dispose();
                                    }
                                }

                                //Then we actually load the Search Provider
                                bool autoSync = ConfigurationLoader.GetConfigurationBoolean(g, objNode, g.CreateUriNode(UriFactory.Create(FullTextHelper.PropertyIndexSync)), true);
                                obj = new LuceneSearchProvider(this.GetLuceneVersion(ver), (Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema, autoSync);
                                break;
                            }
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:schema property pointed to an Object which could not be loaded as a type that implements the required IFullTextIndexSchema interface");
                        }
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:analyzer property pointed to an Object which could not be loaded as a type that derives from the required Lucene.Net.Analysis.Analyzer type");
                    }
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:index property pointed to an Object which could not be loaded as a type that derives from the required Lucene.Net.Store.Directory type");
                }
                break;

            default:
                try
                {
                    if (this._luceneAnalyzerType.IsAssignableFrom(targetType))
                    {
                        //Create an Analyzer
                        //Try to create passing Lucene Version wherever possible
                        if (targetType.GetConstructor(new Type[] { typeof(LucVersion) }) != null)
                        {
                            obj = Activator.CreateInstance(targetType, new Object[] { this.GetLuceneVersion(ver) });
                        }
                        else
                        {
                            obj = Activator.CreateInstance(targetType);
                        }
                    }
                    else if (this._luceneDirectoryType.IsAssignableFrom(targetType))
                    {
                        //Create a Directory aka a Lucene Index
                        String dir = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile)));
                        if (dir != null)
                        {
                            try
                            {
                                obj = Activator.CreateInstance(targetType, new Object[] { dir });
                            }
                            catch
                            {
                                MethodInfo method = targetType.GetMethod("Open", new Type[] { typeof(DirInfo) });
                                if (method != null)
                                {
                                    obj = method.Invoke(null, new Object[] { new DirInfo(ConfigurationLoader.ResolvePath(dir)) });
                                }
                            }
                        }
                        else
                        {
                            obj = Activator.CreateInstance(targetType);
                        }
                        //Ensure the Index if necessary
                        if (obj != null)
                        {
                            if (ConfigurationLoader.GetConfigurationBoolean(g, objNode, g.CreateUriNode(UriFactory.Create(FullTextHelper.FullTextConfigurationNamespace + "ensureIndex")), false))
                            {
                                IndexWriter writer = new IndexWriter((Directory)obj, new StandardAnalyzer(this.GetLuceneVersion(ver)), IndexWriter.MaxFieldLength.UNLIMITED);
                                writer.Dispose();
                            }
                        }
                    }
                }
                catch
                {
                    //Since we know we don't allow loading of all analyzers and directories we allow for users to inject other object factories
                    //which may know how to load those specific instances
                    obj = null;
                }
                break;
            }

            return(obj != null);
        }