//--- Constructors --- public SearchInstanceData(string indexPath, Analyzer analyzer, UpdateDelayQueue queue, TimeSpan commitInterval, TaskTimerFactory taskTimerFactory) { _analyzer = analyzer; _directory = FSDirectory.GetDirectory(indexPath); // Note (arnec): Needed with SimpleFSLock, since a hard shutdown will have left the lock dangling IndexWriter.Unlock(_directory); try { _writer = new IndexWriter(_directory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED); } catch (CorruptIndexException e) { _log.WarnFormat("The Search index at {0} is corrupt. You must repair or delete it before restarting the service. If you delete it, you must rebuild your index after service restart.", indexPath); if (e.Message.StartsWith("Unknown format version")) { _log.Warn("The index is considered corrupt because it's an unknown version. Did you accidentally downgrade your install?"); } throw; } _reader = IndexReader.Open(_directory); _searcher = new IndexSearcher(_reader); _queue = queue; _commitInterval = commitInterval; _taskTimerFactory = taskTimerFactory; if (_commitInterval != TimeSpan.Zero) { _commitTimer = _taskTimerFactory.New(_commitInterval, Commit, null, TaskEnv.None); } }
/// <summary> /// Acumula el índice de las tesis que mes a mes se van ingresando a través del /// sistema de mantenimiento /// </summary> public void BuildIndex() { try { //if (System.IO.Directory.Exists(indexPath)) //{ // System.IO.Directory.Delete(indexPath, true); //} luceneIndexDirectory = FSDirectory.GetDirectory(indexPath); writer = new IndexWriter(luceneIndexDirectory, analyzer, false); int consecTesis = 1; foreach (TesisIndx tesis in listaTesis) { Document doc = new Document(); doc.Add(new Field("Ius", tesis.Ius.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); doc.Add(new Field("RubroIndx", tesis.RubroIndx, Field.Store.YES, Field.Index.TOKENIZED)); doc.Add(new Field("TextoIndx", tesis.TextoIndx, Field.Store.YES, Field.Index.TOKENIZED)); doc.Add(new Field("Rubro", tesis.Rubro, Field.Store.YES, Field.Index.UN_TOKENIZED)); writer.AddDocument(doc); Console.WriteLine(consecTesis); consecTesis++; } writer.Optimize(); writer.Flush(); writer.Close(); luceneIndexDirectory.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void _initCacheDirectory(Directory cacheDirectory) { #if COMPRESSBLOBS CompressBlobs = true; #endif if (cacheDirectory != null) { // save it off _cacheDirectory = cacheDirectory; } else { string cachePath = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "AzureDirectory"); System.IO.DirectoryInfo azureDir = new System.IO.DirectoryInfo(cachePath); if (!azureDir.Exists) { azureDir.Create(); } string catalogPath = System.IO.Path.Combine(cachePath, _catalog); System.IO.DirectoryInfo catalogDir = new System.IO.DirectoryInfo(catalogPath); if (!catalogDir.Exists) { catalogDir.Create(); } _cacheDirectory = FSDirectory.GetDirectory(catalogPath); } CreateContainer(); }
/// <summary> /// Initializes this instance. /// </summary> /// <param name="indexFolder"></param> public void Initialize(string indexFolder) { var pathToIndex = Path.Combine(indexFolder, "Index"); _directory = FSDirectory.GetDirectory(pathToIndex); _analyzer = new StandardAnalyzer(); }
public void Initialize(String directoryProviderName, IDictionary <string, string> properties, ISearchFactoryImplementor searchFactory) { DirectoryInfo indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary)properties); try { bool create = !IndexReader.IndexExists(indexDir.FullName); indexName = indexDir.FullName; directory = FSDirectory.GetDirectory(indexName, create); if (create) { IndexWriter iw = new IndexWriter(directory, new StandardAnalyzer(), create, new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.UNLIMITED); iw.Close(); } //searchFactory.RegisterDirectoryProviderForLocks(this); } catch (IOException e) { throw new HibernateException("Unable to initialize index: " + directoryProviderName, e); } }
internal static IndexWriter GetIndexWriter(bool createNew) { Directory dir = FSDirectory.GetDirectory(SenseNet.ContentRepository.Storage.IndexDirectory.CurrentOrDefaultDirectory, createNew); return(new IndexWriter(dir, GetAnalyzer(), createNew, IndexWriter.MaxFieldLength.UNLIMITED)); //return LuceneManager.CreateIndexWriter(FSDirectory.Open(new System.IO.DirectoryInfo(IndexDirectory.CurrentOrDefaultDirectory)), createNew); }
public static void Main(System.String[] args) { bool readOnly = false; bool add = false; for (int i = 0; i < args.Length; i++) { if ("-ro".Equals(args[i])) { readOnly = true; } if ("-add".Equals(args[i])) { add = true; } } System.IO.FileInfo indexDir = new System.IO.FileInfo("index"); bool tmpBool; if (System.IO.File.Exists(indexDir.FullName)) { tmpBool = true; } else { tmpBool = System.IO.Directory.Exists(indexDir.FullName); } if (!tmpBool) { System.IO.Directory.CreateDirectory(indexDir.FullName); } IndexReader.Unlock(FSDirectory.GetDirectory(indexDir, false)); if (!readOnly) { IndexWriter writer = new IndexWriter(indexDir, ANALYZER, !add); SupportClass.ThreadClass indexerThread = new IndexerThread(writer); indexerThread.Start(); System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1000)); } SearcherThread searcherThread1 = new SearcherThread(false); searcherThread1.Start(); SEARCHER = new IndexSearcher(indexDir.ToString()); SearcherThread searcherThread2 = new SearcherThread(true); searcherThread2.Start(); SearcherThread searcherThread3 = new SearcherThread(true); searcherThread3.Start(); }
public LuceneMetadataRepository() { _analyzer = new StandardAnalyzer(); _directory = FSDirectory.GetDirectory("./index", true); _indexWriter = new IndexWriter(_directory, _analyzer, true); _indexWriter.SetMaxFieldLength(25000); _indexSearcher = new IndexSearcher(_directory); }
/// <summary> Returns <code>true</code> iff the index in the named directory is /// currently locked. /// </summary> /// <param name="directory">the directory to check for a lock /// </param> /// <throws> IOException if there is a low-level IO error </throws> public static bool IsLocked(System.String directory) { Directory dir = FSDirectory.GetDirectory(directory); bool result = IsLocked(dir); dir.Close(); return(result); }
/// <summary> Reads version number from segments files. The version number is /// initialized with a timestamp and then increased by one for each change of /// the index. /// /// </summary> /// <param name="directory">where the index resides. /// </param> /// <returns> version number. /// </returns> /// <throws> CorruptIndexException if the index is corrupt </throws> /// <throws> IOException if there is a low-level IO error </throws> public static long GetCurrentVersion(System.IO.FileInfo directory) { Directory dir = FSDirectory.GetDirectory(directory); long version = GetCurrentVersion(dir); dir.Close(); return(version); }
/// <summary> /// Gets the index directory (returns a new object instance). /// </summary> /// <value>The index directory.</value> internal Directory GetIndexDirectory(bool create) { if (IsRAMBasedSearch) { return(new RAMDirectory()); } return(FSDirectory.GetDirectory(this.indexPath, create)); }
/// <summary> /// Default constructor. /// </summary> /// <param name="physicalIndexDir">Location of the index files.</param> /// <param name="rebuildIndex">Flag to indicate if the index should be rebuilt. /// NOTE: you can not update or delete content when rebuilding the index. /// </param> public IndexBuilder(string physicalIndexDir, bool rebuildIndex) { _indexDirectory = FSDirectory.GetDirectory(physicalIndexDir, false); _rebuildIndex = rebuildIndex; InitIndexWriter(); log.Info("IndexBuilder created."); }
//public List<int> SearchIuses(string searchTerm) //{ // List<int> results = new List<int>(); // IndexSearcher searcher = new IndexSearcher(FSDirectory.GetDirectory(indexPath)); // QueryParser parser = new QueryParser("Rubro", analyzer); // parser.SetEnablePositionIncrements(false); // PhraseQuery q = new PhraseQuery(); // String[] words = searchTerm.Split(' '); // foreach (string word in words) // { // q.Add(new Term("Rubro", word)); // } // Console.WriteLine(q.ToString()); // //Query query = parser.Parse(searchTerm); // Hits hitsFound = searcher.Search(q); // TesisIndx sampleDataFileRow = null; // for (int i = 0; i < hitsFound.Length(); i++) // { // sampleDataFileRow = new TesisIndx(); // Document doc = hitsFound.Doc(i); // sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); // sampleDataFileRow.Rubro = doc.Get("Rubro"); // sampleDataFileRow.Texto = doc.Get("Texto"); // float score = hitsFound.Score(i); // sampleDataFileRow.Score = score; // results.Add(sampleDataFileRow.Ius); // } // parser = new QueryParser("Texto", analyzer); // parser.SetEnablePositionIncrements(false); // q = new PhraseQuery(); // words = searchTerm.Split(' '); // foreach (string word in words) // { // q.Add(new Term("Texto", word)); // } // // query = parser.Parse(searchTerm); // hitsFound = searcher.Search(q); // for (int i = 0; i < hitsFound.Length(); i++) // { // sampleDataFileRow = new TesisIndx(); // Document doc = hitsFound.Doc(i); // sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); // sampleDataFileRow.Rubro = doc.Get("Rubro"); // sampleDataFileRow.Texto = doc.Get("Texto"); // float score = hitsFound.Score(i); // sampleDataFileRow.Score = score; // results.Add(sampleDataFileRow.Ius); // } // results.Distinct(); // return results; //} /// <summary> /// Busca en el índice previamente construido las tesis que tengan coincidencia ya sea en el Rubro o Texto /// del término buscado /// </summary> /// <param name="searchTerm"></param> /// <returns></returns> public List <int> SearchIuses(string searchTerm) { List <int> results = new List <int>(); IndexSearcher searcher = new IndexSearcher(FSDirectory.GetDirectory(indexPath)); QueryParser parser = new QueryParser("RubroIndx", analyzer); parser.SetEnablePositionIncrements(false); Query query = parser.Parse(String.Format("\"{0}\"", searchTerm)); Console.WriteLine(query.ToString()); Hits hitsFound = searcher.Search(query); TesisIndx sampleDataFileRow = null; for (int i = 0; i < hitsFound.Length(); i++) { sampleDataFileRow = new TesisIndx(); Document doc = hitsFound.Doc(i); sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); sampleDataFileRow.RubroIndx = doc.Get("RubroIndx"); sampleDataFileRow.TextoIndx = doc.Get("TextoIndx"); float score = hitsFound.Score(i); sampleDataFileRow.Score = score; results.Add(sampleDataFileRow.Ius); } parser = new QueryParser("TextoIndx", analyzer); parser.SetEnablePositionIncrements(false); query = parser.Parse(String.Format("\"{0}\"", searchTerm)); Console.WriteLine(query.ToString()); hitsFound = searcher.Search(query); for (int i = 0; i < hitsFound.Length(); i++) { sampleDataFileRow = new TesisIndx(); Document doc = hitsFound.Doc(i); sampleDataFileRow.Ius = int.Parse(doc.Get("Ius")); sampleDataFileRow.Rubro = doc.Get("RubroIndx"); sampleDataFileRow.Texto = doc.Get("TextoIndx"); float score = hitsFound.Score(i); sampleDataFileRow.Score = score; results.Add(sampleDataFileRow.Ius); } results.Distinct(); return(results); }
/// <summary> /// Default constructor. /// </summary> /// <param name="physicalIndexDir">Location of the index files.</param> /// <param name="rebuildIndex">Flag to indicate if the index should be rebuilt. /// <param name="textExtractor">The text extractor that can be used to extract text from content.</param> public IndexBuilder(string physicalIndexDir, bool rebuildIndex, ITextExtractor textExtractor) { this._indexDirectory = FSDirectory.GetDirectory(physicalIndexDir, false); this._rebuildIndex = rebuildIndex; this._textExtractor = textExtractor; InitIndexWriter(); log.Info("IndexBuilder created."); }
/// <summary> /// Connect to Lucene index specified in the configuration file /// </summary> private void LuceneConnect() { string indexDir = ConfigurationManager.AppSettings["IndexDir"]; Console.WriteLine("Creating Index in " + indexDir); Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.GetDirectory(indexDir, true); _luceneIndexWriter = new IndexWriter(directory, analyzer, true); }
protected void Page_Load(object sender, EventArgs e) { //if (Session["KeyWords"] == null ? false : true) //{ // Response.Redirect("Search.aspx"); //} String text = Session["KeyWords"].ToString(); ChineseAnalyzer analyzer = new ChineseAnalyzer(); TokenStream ts = analyzer.TokenStream("ItemName", new System.IO.StringReader(text)); Lucene.Net.Analysis.Token token; try { int n = 0; while ((token = ts.Next()) != null) { this.lbMsg.Text += (n++) + "->" + token.TermText() + " " + token.StartOffset() + " " + token.EndOffset() + " " + token.Type() + "<br>"; // Response.Write((n++) + "->" + token.TermText() + " " + token.StartOffset() + " " //+ token.EndOffset() + " " + token.Type() + "<br>"); } } catch { this.lbMsg.Text = "wrong"; } // Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.GetDirectory(Server.MapPath("/indexFile/"), false); IndexSearcher isearcher = new IndexSearcher(directory); Query query; query = QueryParser.Parse(Session["KeyWords"].ToString(), "ItemName", analyzer); //query = QueryParser.Parse("2", "nid", analyzer); Hits hits = isearcher.Search(query); this.lbMsg.Text += "<font color=red>共找到" + hits.Length() + "条记录</font><br>"; //Response.Write("<font color=red>共找到" + hits.Length() + "条记录</font><br>"); for (int i = 0; i < hits.Length(); i++) { Document hitDoc = hits.Doc(i); this.lbMsg.Text += "编号:" + hitDoc.Get("ItemID").ToString() + "<br>" + "分类:" + hitDoc.Get("CategoryName").ToString() + "<br>" + "专题:" + hitDoc.Get("ProductName").ToString() + "<br>" + "标题:<a href=" + hitDoc.Get("visiturl").ToString() + ">" + hitDoc.Get("ItemName").ToString() + "</a><br>"; //Response.Write("编号:" + hitDoc.Get("ItemID").ToString() + "<br>"); //Response.Write("分类:" + hitDoc.Get("CategoryName").ToString() + "<br>"); //Response.Write("标题:<a href=" + hitDoc.Get("visiturl").ToString() + ">" + hitDoc.Get("ItemName").ToString() + "</a><br>"); //Response.Write("专题:" + hitDoc.Get("ProductName").ToString() + "<br>"); } isearcher.Close(); directory.Close(); }
/// <summary> /// Default constructor. /// </summary> /// <param name="physicalIndexDir">Location of the index files.</param> /// <param name="rebuildIndex">Flag to indicate if the index should be rebuilt. /// NOTE: you can not update or delete content when rebuilding the index.</param> public IndexBuilder(string physicalIndexDir, bool rebuildIndex, string indexerName) { this._indexDirectory = FSDirectory.GetDirectory(physicalIndexDir, false); _directoryPath = physicalIndexDir; _indexerName = indexerName; if (GetLastBuildDate() == DateTime.MinValue) { rebuildIndex = true; } this._rebuildIndex = rebuildIndex; InitIndexWriter(); }
/// <summary> /// IndexWriter that can be used to apply updates to an index /// </summary> /// <param name="indexPath">File system path to the target index</param> /// <param name="oAnalyzer">Lucene Analyzer to be used by the underlying IndexWriter</param> /// <param name="bCompoundFile">Setting to dictate if the index should use compound format</param> /// <returns></returns> private IndexWriter GetIndexWriter(string indexPath, Analyzer oAnalyzer, bool bCompoundFile) { bool bExists = System.IO.Directory.Exists(indexPath); if (bExists == false) { System.IO.Directory.CreateDirectory(indexPath); } bExists = IndexReader.IndexExists(FSDirectory.GetDirectory(indexPath, false)); IndexWriter idxWriter = new IndexWriter(indexPath, oAnalyzer, !bExists); idxWriter.SetUseCompoundFile(bCompoundFile); return(idxWriter); }
private static void InitialiseLucene() { var indexPath = AppDomain.CurrentDomain.BaseDirectory + @"\CodeSnippetIndex"; if (System.IO.Directory.Exists(indexPath)) { try { System.IO.Directory.Delete(indexPath, true); } catch (Exception ex) { } } Analyzer analyzer = new StandardAnalyzer(); luceneIndexDirectory = FSDirectory.GetDirectory(indexPath); writer = new IndexWriter(luceneIndexDirectory, analyzer, true); }
public Searcher(string indexDirectory) { _indexDirectory = indexDirectory; if (!Directory.Exists(indexDirectory)) { Directory.CreateDirectory(indexDirectory); } try { _directory = FSDirectory.GetDirectory(_indexDirectory, false); searcher = new IndexSearcher(_directory); } catch (Exception) { IsOperational = false; } }
public void BuildIndex(IEnumerable <DataFileRow> dataToIndex) { luceneIndexDirectory = FSDirectory.GetDirectory(indexPath); writer = new IndexWriter(luceneIndexDirectory, analyzer, true); foreach (var sampleDataFileRow in dataToIndex) { Document doc = new Document(); doc.Add(new Field("LineNumber", sampleDataFileRow.LineNumber.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); doc.Add(new Field("LineText", sampleDataFileRow.LineText.ToLower(), Field.Store.YES, Field.Index.TOKENIZED)); writer.AddDocument(doc); } writer.Optimize(); //delete doc writer.Flush(); writer.Close(); }
public Indexer(IndexerArgs args) { WriteLogo(); WriteArgs(); _args = args; _indexDirectory = FSDirectory.GetDirectory(args.IndexPath); _indexQueueLimit = new Semaphore(args.MaxThreads * 4, args.MaxThreads * 4); ThreadPool.SetMaxThreads(args.MaxThreads, 1000); ThreadPool.SetMinThreads(args.MaxThreads / 2, Environment.ProcessorCount); _contentField = new Field(FieldName.Content, _contentTokenStream); _pathField = new Field(FieldName.Path, _pathTokenStream); _externalsField = new Field(FieldName.Externals, _externalsTokenStream); _messageField = new Field(FieldName.Message, _messageTokenStream); _svn = new SharpSvnApi(args.RepositoryLocalUri, args.Credentials.User, args.Credentials.Password); }
public void Initialize(string directoryProviderName, IDictionary <string, string> properties, ISearchFactoryImplementor searchFactory) { this.properties = properties; this.directoryProviderName = directoryProviderName; // source guessing source = DirectoryProviderHelper.GetSourceDirectory(Environment.SourceBase, Environment.Source, directoryProviderName, (IDictionary)properties); if (source == null) { throw new ArgumentException("FSMasterDirectoryProvider requires a viable source directory"); } log.Debug("Source directory: " + source); indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary)properties); log.Debug("Index directory: " + indexDir); try { // NB Do we need to do this since we are passing the create flag to Lucene? bool create = !IndexReader.IndexExists(indexDir.FullName); if (create) { log.DebugFormat("Index directory not found, creating '{0}'", indexDir.FullName); indexDir.Create(); } indexName = indexDir.FullName; directory = FSDirectory.GetDirectory(indexName, create); if (create) { indexName = indexDir.FullName; IndexWriter iw = new IndexWriter(directory, new StandardAnalyzer(), create); iw.Close(); } } catch (IOException e) { throw new HibernateException("Unable to initialize index: " + directoryProviderName, e); } this.searchFactory = searchFactory; }
private void InitDatabase() { if (IndexReader.IndexExists(_indexDir)) { try { _directory = FSDirectory.GetDirectory(_indexDir, false); IndexReader indexReader = IndexReader.Open(_indexDir); _docid = (ulong)indexReader.NumDocs() + 1; indexReader.Close(); } catch (Exception e) { throw e; } } else { _directory = FSDirectory.GetDirectory(_indexDir, true); } }
/// <summary> /// 删除索引 /// </summary> public static bool Delete(int[] productIDList, bool needOptimize) { if (productIDList == null && productIDList.Length == 0) { return(false); } Lucene.Net.Store.Directory fsDir = FSDirectory.GetDirectory(PhysicalIndexDirectory, false); IndexReader reader = IndexReader.Open(fsDir); bool result = false; try { for (int i = 0; i < productIDList.Length; i++) { if (productIDList[i] != 0) { Term term = new Term(ProductIndexField.ProductID, productIDList[i].ToString()); reader.DeleteDocuments(term); } } reader.Close(); if (needOptimize) { IndexWriter fsWriter = new IndexWriter(fsDir, SearchHelper.GetChineseAnalyzer(), false); fsWriter.Optimize(); fsWriter.Close(); } result = true; } finally { fsDir.Close(); } return(result); }
public void CreateIndexes() { Lucene.Net.Store.Directory dir = FSDirectory.GetDirectory(IndexPath, true); Analyzer analyzer = new StandardAnalyzer(); var indexWriter = new IndexWriter(dir, analyzer, true, new IndexWriter.MaxFieldLength(25000)); foreach (var File in Directory.GetFiles(HelpFilePath, "*.xaml")) { var text = GetTextFromXaml(File).Replace("\r\n", " ").Replace("\n", " "); var doc = new Document(); var fldContent = new Field(ContentField, text, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_OFFSETS); var fldName = new Field(PluginField, Path.GetFileNameWithoutExtension(Path.GetFileName(File)), Field.Store.YES, Field.Index.NO, Field.TermVector.NO); doc.Add(fldContent); doc.Add(fldName); indexWriter.AddDocument(doc); } indexWriter.Optimize(); indexWriter.Close(); }
public static void Main(System.String[] args) { if (args.Length != 2) { System.Console.Out.WriteLine(typeof(SynLookup) + " <index path> <word>"); return; } FSDirectory directory = FSDirectory.GetDirectory(args[0], false); IndexSearcher searcher = new IndexSearcher(directory); System.String word = args[1]; Hits hits = searcher.Search(new TermQuery(new Term(Syns2Index.F_WORD, word))); if (hits.Length() == 0) { System.Console.Out.WriteLine("No synonyms found for " + word); } else { System.Console.Out.WriteLine("Synonyms found for \"" + word + "\":"); } for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); System.String[] values = doc.GetValues(Syns2Index.F_SYN); for (int j = 0; j < values.Length; j++) { System.Console.Out.WriteLine(values[j]); } } searcher.Close(); directory.Close(); }
static void Main(string[] args) { var connectionString = "mongodb://10.0.0.17/test"; //var connectionString = "mongodb://localhost/test"; MongoClient mongoClient = new MongoClient(connectionString); MongoServer mongoServer = mongoClient.GetServer(); MongoDatabase db = mongoServer.GetDatabase("test"); var collection = db.GetCollection <TweetItem>("TweetItems"); DateTime dtmFirst = new DateTime(2014, 05, 17); DateTime dtmLast = DateTime.Now; dtmLast = dtmLast.AddHours(-dtmLast.Hour); dtmLast = dtmLast.AddMinutes(-dtmLast.Minute); var query = Query <TweetItem> .Where(t => t.CreationDate >= dtmFirst); List <TweetItem> value = collection.Find(query).ToList(); FSDirectory dir = FSDirectory.GetDirectory(Environment.CurrentDirectory + "\\LuceneIndex"); //Lucene.Net.Store.RAMDirectory dir = new RAMDirectory(); Lucene.Net.Analysis.StopAnalyzer an = new Lucene.Net.Analysis.StopAnalyzer(); IndexWriter wr = new IndexWriter(dir, an, true); //DirectoryInfo diMain = new DirectoryInfo(dia.SelectedPath); foreach (TweetItem tweet in value) { Document doc = new Document(); doc.Add(new Field("id", tweet._id.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("created", tweet.CreationDate.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("user", tweet.User, Field.Store.YES, Field.Index.NO)); doc.Add(new Field("text", tweet.Text, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES)); wr.AddDocument(doc); } wr.Optimize(); wr.Flush(); wr.Close(); dir.Close(); }
public static void Main(System.String[] args) { if (args.Length != 2) { System.Console.Out.WriteLine(typeof(SynExpand) + " <index path> <query>"); return; } FSDirectory directory = FSDirectory.GetDirectory(args[0], false); IndexSearcher searcher = new IndexSearcher(directory); System.String query = args[1]; System.String field = "contents"; Query q = Expand(query, searcher, new StandardAnalyzer(), field, 0.9f); System.Console.Out.WriteLine("Query: " + q.ToString(field)); searcher.Close(); directory.Close(); }
protected void btnCreate_Click(object sender, EventArgs e) { Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.GetDirectory(Server.MapPath("/Web/indexFile/"), true); IndexWriter iwriter; if (IndexExist(directory)) { iwriter = new IndexWriter(directory, analyzer, false); } else { iwriter = new IndexWriter(directory, analyzer, true); } Document doc; int count = 0; //"select Items.IID,Items.ProductID,Items.Name, Products.PID,Products.CategoryID,Products.Name, Category.CID,Category.CName from Category,Items,Products,Brand where Products.CategoryID=Category.CID and Products.PID=Items.ProductID"; BLL.Subject itemInfo = new BLL.Subject(); Entity.Subject info; SqlDataReader dr = itemInfo.getItemSimpleInfo(); while (dr.Read()) { info = new Entity.Subject(); info.setItemID(dr.GetInt32(0)); info.setProductID(dr.GetInt32(1)); info.setItemName(dr.GetString(2)); info.setCategoryID(dr.GetInt32(4)); info.setProductName(dr.GetString(5)); info.setCategoryName(dr.GetString(7)); //info.setBrandID(dr.GetInt32(7)); //info.setBrandName(dr.GetString(10)); doc = new Document(); doc.Add(Field.Keyword("TypeID", "1")); doc.Add(Field.Keyword("ItemID", info.getItemID().ToString())); doc.Add(Field.Keyword("addtime", mydate)); doc.Add(Field.Text("ItemName", info.getItemName() + "站内")); doc.Add(Field.UnIndexed("ProductName", info.getProductName())); doc.Add(Field.UnIndexed("CategoryName", info.getCategoryName())); doc.Add(Field.UnIndexed("visiturl", getPath(info.getCategoryID(), info.getProductID()))); //this.lbMsg.Text= @"<div class=\"notification information png_bg\" style=\" height:40px\">"+ // "<a class=\"close" href=\"#\">"+ // @"<img alt="close" src="resources/images/icons/cross_grey_small.png" title="Close this notification" /> </a> // <div> <asp:Label ID="lbInfo" runat="server"></asp:Label> // </div> // </div> this.lbMsg.Text += count + "<ul>" + "<li>分类:" + info.getCategoryName() + "</li>" + "<li>标题:" + info.getItemName() + "</li>" + "<li><img src='../images/logo/mobile_logo.png' width='50' height='50'/></li>" + "<li>品牌:" + info.getBrandName() + "</li>" + "<li>专题:" + info.getProductName() + "</li>" + "<li>品名:<a href=" + getPath(info.getCategoryID(), info.getProductID()) + ">" + info.getItemName() + "</a></li></ul>"; this.lbMsg.Text += "<br/>"; //Response.Write(count + "<ul>" // + "<li>分类:" + info.getCategoryName() + "</li>" // + "<li>标题:" + info.getItemName() + "</li>" // + "<li><img src='../images/logo/mobile_logo.png' width='50' height='50'/></li>" // + "<li>品牌:" + info.getBrandName() + "</li>" // + "<li>专题:" + info.getProductName() + "</li>" // + "<li>品名:<a href=" + getPath(info.getCategoryID(), info.getProductID()) + ">" + info.getItemName() + "</a></li></ul>"); //Response.Write(this.lbMsg.Text); iwriter.AddDocument(doc); iwriter.Optimize(); count++; } dr = itemInfo.getItemJingDongInfo(); while (dr.Read()) { try { doc = new Document(); doc.Add(Field.Keyword("TypeID", "2")); doc.Add(Field.Keyword("ItemID", dr.GetInt32(0) + "")); //JID doc.Add(Field.Keyword("JNum", dr.GetString(1))); //JNum, doc.Add(Field.Keyword("addtime", mydate)); doc.Add(Field.Text("ItemName", dr.GetString(2) + "京东")); //JTitle doc.Add(Field.UnIndexed("ProductName", dr.GetString(2))); //JTitle doc.Add(Field.UnIndexed("CategoryName", dr.GetString(4))); //JCategory doc.Add(Field.UnIndexed("visiturl", dr.GetString(3))); //JUrl doc.Add(Field.UnIndexed("JPrice", dr.GetString(5))); //JPrice this.lbMsg.Text += count + "<ul>" + "<li>分类:" + dr.GetString(4) + "</li>" + "<li><img src='../images/logo/jingdong_logo.png' width='100' height='30'/></li>" + "<li>品名:<a href=" + dr.GetString(3) + ">" + dr.GetString(2) + "</a></li></ul>"; this.lbMsg.Text += "<br/>"; //Response.Write( count + "<ul>" // + "<li>分类:" + dr.GetString(4) + "</li>" // + "<li><img src='../images/logo/jingdong_logo.png' width='100' height='30'/></li>" // + "<li>品名:<a href=" + dr.GetString(3) + ">" + dr.GetString(2) + "</a></li></ul>"); iwriter.AddDocument(doc); } catch { } iwriter.Optimize(); count++; } iwriter.Close(); directory.Close(); this.lbMsg.Text += "<font color=red>建立" + count + "条索引成功!</font><br>"; //IndexSearcher isearcher = new IndexSearcher(directory); //Query query; //query = QueryParser.Parse("促销", "ItemName", analyzer); ////query = QueryParser.Parse("2", "nid", analyzer); //Hits hits = isearcher.Search(query); //Response.Write("<font color=red>共找到" + hits.Length() + "条记录</font><br>"); //for (int i = 0; i < hits.Length(); i++) //{ // Document hitDoc = hits.Doc(i); // Response.Write("编号:" + hitDoc.Get("ItemID").ToString() + "<br>"); // Response.Write("分类:" + hitDoc.Get("CategoryName").ToString() + "<br>"); // Response.Write("标题:<a href=" + hitDoc.Get("visiturl").ToString() + ">" + hitDoc.Get("ItemName").ToString() + "</a><br>"); // Response.Write("专题:" + hitDoc.Get("ProductName").ToString() + "<br>"); //} //isearcher.Close(); //directory.Close(); }