コード例 #1
0
ファイル: UnitTest1.cs プロジェクト: wingahi/DevLib.Comm
        public void TestMethod1()
        {
            Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            string s = "我日中华人民共和国";

            System.IO.StringReader          reader = new System.IO.StringReader(s);
            Lucene.Net.Analysis.TokenStream ts     = a.TokenStream(s, reader);
            bool hasnext = ts.IncrementToken();

            Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;

            while (hasnext)
            {
                ita = ts.GetAttribute <Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                Console.WriteLine(ita.Term);
                hasnext = ts.IncrementToken();
            }

            Console.WriteLine("over");


            ts.CloneAttributes();
            reader.Close();
            a.Close();
        }
コード例 #2
0
        // add/update/clear search index data
        public static void AddUpdateLuceneIndex()
        {
            using (var context = new CreativeNetworkEntities())
            {

                // init lucene
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);
                using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    // add data to lucene search index (replaces older entries if any)
                    foreach (var creative in context.Creative)
                    {
                        _addToLuceneIndex(creative, writer);
                        foreach (var chapter in creative.Chapter)
                        {
                            _addToLuceneIndex(chapter, writer);
                        }
                    }

                    // close handles
                    analyzer.Close();
                    writer.Dispose();
                }
            }
        }
コード例 #3
0
        private static IEnumerable<CustomerId> _search(string searchQuery)
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<CustomerId>();

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false))
            {
                var hits_limit = 1000;
                var analyzer = new StandardAnalyzer(Version.LUCENE_29);

                {
                    //var query = new TermQuery(new Term("CustomerName", searchQuery));
                    var query = new BooleanQuery();
                    query.Add(new TermQuery(new Term("CustomerName", searchQuery)), Occur.MUST);
                    var hits = searcher.Search(query, hits_limit).ScoreDocs;
                    var results = _mapLuceneToDataList(hits, searcher);

                    analyzer.Close();
                    searcher.Dispose();
                    return results;
                }
                
            }
        }
コード例 #4
0
        /// <summary>
        /// Performs the explanation.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="searchQuery">The search query.</param>
        /// <param name="resultId">The result identifier.</param>
        /// <returns></returns>
        protected virtual string PerformExplain(Version luceneVersion, FSDirectory fsDirectory, string searchQuery, int resultId)
		{
			/*
			 * The obvious problem here is that we're not using the exact same search as the real one.
			 */

			var explanation = string.Empty;

			using (var indexSearcher = new IndexSearcher(fsDirectory, false))
			{
				var analyzer = new StandardAnalyzer(luceneVersion);
				
				var queryParser = new MultiFieldQueryParser(luceneVersion, new[] { "Id".ToLowerInvariant() }, analyzer)
									{
										DefaultOperator = QueryParser.Operator.AND
									};
				
				var query = this.searchQueryParser.ParseQuery(searchQuery, queryParser);

				explanation = indexSearcher.Explain(query, resultId).ToHtml();

				analyzer.Close();
			}

			return explanation;
		}
コード例 #5
0
ファイル: Class1.cs プロジェクト: reckcn/DevLib.Comm
        public void v()
        {

            //Analyzer analyzer = new CJKAnalyzer();
            //TokenStream tokenStream = analyzer.TokenStream("", new StringReader("我爱你中国China中华人名共和国"));
            //Lucene.Net.Analysis.Token token = null;
            //while ((token = tokenStream.Next()) != null)
            //{
            //    Response.Write(token.TermText() + "<br/>");
            //}

            Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            string s = "我日中华人民共和国";
            System.IO.StringReader reader = new System.IO.StringReader(s);
            Lucene.Net.Analysis.TokenStream ts = a.TokenStream(s, reader);
            bool hasnext = ts.IncrementToken();
            Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
            while (hasnext)
            {
                ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                Console.WriteLine(ita.Term);
                hasnext = ts.IncrementToken();
            }
            ts.CloneAttributes();
            reader.Close();
            a.Close();
            Console.ReadKey();
        }
コード例 #6
0
        public void v()
        {
            //Analyzer analyzer = new CJKAnalyzer();
            //TokenStream tokenStream = analyzer.TokenStream("", new StringReader("我爱你中国China中华人名共和国"));
            //Lucene.Net.Analysis.Token token = null;
            //while ((token = tokenStream.Next()) != null)
            //{
            //    Response.Write(token.TermText() + "<br/>");
            //}

            Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            string s = "我日中华人民共和国";

            System.IO.StringReader          reader = new System.IO.StringReader(s);
            Lucene.Net.Analysis.TokenStream ts     = a.TokenStream(s, reader);
            bool hasnext = ts.IncrementToken();

            Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
            while (hasnext)
            {
                ita = ts.GetAttribute <Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                Console.WriteLine(ita.Term);
                hasnext = ts.IncrementToken();
            }
            ts.CloneAttributes();
            reader.Close();
            a.Close();
            Console.ReadKey();
        }
コード例 #7
0
		// main search method
		private static IEnumerable<SampleData> _search(string searchQuery, string searchField = "") {
			// validation
			if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<SampleData>();

			// set up lucene searcher
			using (var searcher = new IndexSearcher(_directory, false)) {
				var hits_limit = 1000;
				var analyzer = new StandardAnalyzer(Version.LUCENE_29);

				// search by single field
				if (!string.IsNullOrEmpty(searchField)) {
					var parser = new QueryParser(Version.LUCENE_29, searchField, analyzer);
					var query = parseQuery(searchQuery, parser);
					var hits = searcher.Search(query, hits_limit).ScoreDocs;
					var results = _mapLuceneToDataList(hits, searcher);
					analyzer.Close();
					searcher.Close();
					searcher.Dispose();
					return results;
				}
				// search by multiple fields (ordered by RELEVANCE)
				else {
					var parser = new MultiFieldQueryParser
						(Version.LUCENE_29, new[] {"Id", "Name", "Description"}, analyzer);
					var query = parseQuery(searchQuery, parser);
					var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs;
					var results = _mapLuceneToDataList(hits, searcher);
					analyzer.Close();
					searcher.Close();
					searcher.Dispose();
					return results;
				}
			}
		}
コード例 #8
0
ファイル: QueryBuilder.cs プロジェクト: VirtueMe/ravendb
		public static Query BuildQuery(string query)
		{
			var untokenizedMatches = untokenizedQuery.Matches(query);
			var rangeMatches = rangeQuery.Matches(query);
		    var standardAnalyzer = new StandardAnalyzer(Version.LUCENE_29);
		    try
		    {
                if (untokenizedMatches.Count == 0 && rangeMatches.Count == 0)
                    return new QueryParser(Version.LUCENE_29, "", standardAnalyzer).Parse(query);
                var sb = new StringBuilder(query);
                var booleanQuery = new BooleanQuery();
                AddUntokenizedTerms(untokenizedMatches, booleanQuery, sb, query);
		    	AddRangeTerms(rangeMatches, booleanQuery, sb, query);
                var remaining = sb.ToString();
		    	remaining = hangingConditionAtStart.Replace(remaining, "");
		    	remaining = hangingConditionAtEnd.Replace(remaining, "");
				remaining = remaining.Trim();
		    	if (remaining.Length > 0)
                {
                    booleanQuery.Add(new QueryParser(Version.LUCENE_29, "", standardAnalyzer).Parse(remaining), BooleanClause.Occur.SHOULD);
                }
                return booleanQuery;
		    }
		    finally
		    {
		        standardAnalyzer.Close();
		    }
		}
コード例 #9
0
ファイル: LuceneClient.cs プロジェクト: Workker/EHRLucene
 public void AddUpdateLuceneIndex(IEnumerable<IPatient> sampleDatas)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         foreach (var sampleData in sampleDatas) _addToLuceneIndex(sampleData, writer);
         analyzer.Close();
     }
 }
コード例 #10
0
 public void Optimize()
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         analyzer.Close();
         writer.Optimize();
     }
 }
コード例 #11
0
 public void AddToIndex(IEnumerable<Student> students)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         foreach (var student in students)
             AddToIndex(student, writer);
         analyzer.Close();
     }
 }
コード例 #12
0
 public static void ClearIndexRecord(int id)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_29);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         var searchQuery = new TermQuery(new Term("Id", Convert.ToString(id)));
         writer.DeleteDocuments(searchQuery);
         analyzer.Close();
     }
 }
コード例 #13
0
 public void AddToIndex(IEnumerable<Project> projects)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         foreach (var group in projects)
             AddToIndex(group, writer);
         analyzer.Close();
     }
 }
コード例 #14
0
 public void DeleteIndex(int id)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         var searchQuery = new TermQuery(new Term(SearchingFields.Id.ToString(), id.ToString()));
         writer.DeleteDocuments(searchQuery);
         analyzer.Close();
     }
 }
コード例 #15
0
ファイル: GoLucene.cs プロジェクト: AntonGavr/GavrilchikAnton
        public static void AddUpdateLuceneIndex(IEnumerable<TrackData> trackDatas)
        {
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                foreach (var trackData in trackDatas) _addToLuceneIndex(trackData, writer);

                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #16
0
        /// <summary>
        /// Optimizes the Lucene index.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
		public virtual void Optimize(Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
		{
			var analyzer = new StandardAnalyzer(luceneVersion);

			using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
			{
				analyzer.Close();

				indexWriter.Optimize();
			}
		}
コード例 #17
0
        /// <summary>
        /// Clears the entire index.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
		public virtual void ClearIndex(Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
		{
			var analyzer = new StandardAnalyzer(luceneVersion);

			using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
			{
				indexWriter.DeleteAll();

				analyzer.Close();
			}
		}
コード例 #18
0
 public static void AddDocument(Document document)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_29);
     using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         writer.AddDocument(document);
         writer.Optimize();
         writer.Commit();
         analyzer.Close();
     }
 }
コード例 #19
0
ファイル: LuceneSearch.cs プロジェクト: cgideon/CSharpSandbox
        public static void ClearLuceneIndexRecord(int record_id)
        {
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                var searchQuery = new TermQuery(new Term("Id", record_id.ToString()));
                writer.DeleteDocuments(searchQuery);

                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #20
0
ファイル: SearchService.cs プロジェクト: felldek/test
        /// <summary>
        /// Clears the index.
        /// </summary>
        public void ClearIndex()
        {
            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            using (var writer = new IndexWriter(directoryTemp, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                writer.DeleteAll();

                analyzer.Close();
                writer.Optimize();
                writer.Dispose();
            }
        }
コード例 #21
0
ファイル: SearchService.cs プロジェクト: felldek/test
 /// <summary>
 /// Deletes the manual from index '
 /// </summary>
 /// <param name="manualId">The manual identifier.</param>
 public void DeleteManualFromIndex(int manualId)
 {
     var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
     using (var writer = new IndexWriter(directoryTemp, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
     {
         var query = new TermQuery(new Term("Id", manualId.ToString()));
         writer.DeleteDocuments(query);
         analyzer.Close();
         writer.Optimize();
         writer.Dispose();
     }
 }
コード例 #22
0
 public static List<Document> MultiQuery(string search, string[] searchFields)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_29);
     using (var indexSearcher = new IndexSearcher(Directory, false))
     {
         var queryParser = new MultiFieldQueryParser(Version.LUCENE_29, searchFields, analyzer);
         var query = queryParser.Parse(search);
         var hits = indexSearcher.Search(query,null,999,Sort.RELEVANCE).ScoreDocs;
         analyzer.Close();                
         return hits.Select(hit => indexSearcher.Doc(hit.Doc)).ToList();
     }
 }
コード例 #23
0
 public static void AddUpdateLuceneIndex(IEnumerable<Postcard> postcards)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(PostcardDirectory, analyzer,
         IndexWriter.MaxFieldLength.UNLIMITED))
     {
         foreach (var postcard in postcards)
         {
             AddToLuceneIndex(postcard, writer);
         }
         analyzer.Close();
     }
 }
コード例 #24
0
        /// <summary>
        /// Clears an item from the index.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
        public void ClearIndex(string id, Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
        {
            var analyzer = new StandardAnalyzer(luceneVersion);

            using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
            {
                var searchQuery = new TermQuery(new Term("Key", id));

                indexWriter.DeleteDocuments(searchQuery);

                analyzer.Close();
            }
        }
コード例 #25
0
 public static void AddUpdateLuceneIndex(IEnumerable<HashTag> hashTags)
 {
     var analyzer = new StandardAnalyzer(Version.LUCENE_30);
     using (var writer = new IndexWriter(HashtagDirectory, analyzer,
         IndexWriter.MaxFieldLength.UNLIMITED))
     {
         foreach (var hashTag in hashTags)
         {
             AddToLuceneIndex(hashTag, writer);
         }
         analyzer.Close();
     }
 }
コード例 #26
0
        public static void AddUpdateLuceneIndex(IEnumerable<ContentEntity> contents)
        {
            // init lucene           
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)
                foreach (var content in contents) _addToLuceneIndex(content, writer);

                // close handles
                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #27
0
        public void AddUpdateIndex(IEnumerable<CourseUnit> units)
        {
            // init lucene
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entries if any)
                foreach (var unit in units) AddToLuceneIndex(unit, writer);

                // close handles
                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #28
0
        public static void AddUpdateLuceneIndex(IEnumerable<KnowledgeBox.Models.Item> itemsData)
        {
            // init lucene
            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)
                foreach (var itemData in itemsData) _addToLuceneIndex(itemData, writer);

                // close handles
                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #29
0
        private static IEnumerable<LuceneSearchEO> _search(LuceneSearchType searchType, string searchQuery, string searchField = "")
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<LuceneSearchEO>();

            // set up lucene searcher
            using (var searcher = new IndexSearcher(getFSDirectory(searchType), false))
            {
                var hits_limit = 20;
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);

                // search by single field
                if (!string.IsNullOrEmpty(searchField))
                {
                    var parser = new QueryParser(Version.LUCENE_30, searchField, analyzer);
                    var query = parseQuery(searchQuery, parser);
                    var hits = searcher.Search(query, hits_limit).ScoreDocs;
                    var results = _mapLuceneToDataList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return results;
                }
                // search by multiple fields (ordered by RELEVANCE)
                else
                {
                    string[] fields = getLuceneSearchEOFields();
                    var parser = new MultiFieldQueryParser
                        (Version.LUCENE_30, fields, analyzer);
                    var query = parseQuery(searchQuery, parser);
                    var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs;
                    var results = _mapLuceneToDataList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return results;
                }
            }
        }
コード例 #30
0
 public static List<string> SearchLucene(string searchQuery)
 {
     var directory = FSDirectory.Open(new DirectoryInfo("C:\\rm_index"));
     using (var searcher = new IndexSearcher(directory, false))
     {
         var analyzer = new StandardAnalyzer(Version.LUCENE_30);
         var parser = new QueryParser(Version.LUCENE_30, "DIRECCION_FINAL", analyzer);
         var query = ParseQuery(searchQuery, parser);
         var hits = searcher.Search(query, hits_limit).ScoreDocs;
         var results = hits.Select(hit => MapLuceneDocumentToData(searcher.Doc(hit.Doc))).ToList();
         analyzer.Close();
         searcher.Dispose();
         return results;
     }
 }
コード例 #31
0
        public void Insert(BaseCollectionItem baseCollectionItem)
        {
            baseCollectionItem.Id = Guid.NewGuid();
            baseCollectionItem.CreatedDate = DateTime.Now;
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            using (var writer = new IndexWriter(LuceneService.Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)

                AddToLuceneIndex(baseCollectionItem, writer);
                // close handles
                analyzer.Close();
                writer.Dispose();
            }
        }
コード例 #32
0
ファイル: GoLucene.cs プロジェクト: Workker/EHRIntegracao
        public static void AddUpdateLuceneIndex(IEnumerable<SampleData> sampleDatas)
        {
            // init lucene
            var analyzer = new StandardAnalyzer(Version.LUCENE_29);
            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entries if any)
                foreach (var sampleData in sampleDatas) _addToLuceneIndex(sampleData, writer);

                // close handles
                analyzer.Close();
                writer.Close();
                writer.Dispose();
            }
        }