public SearchResult(Document doc, float score)
        {
            Fields = new Dictionary<string, string>();
            string id = doc.Get("id");
            if (string.IsNullOrEmpty(id))
            {
                id = doc.Get("__NodeId");
            }
            Id = int.Parse(id);
            Score = score;
           
            //we can use lucene to find out the fields which have been stored for this particular document
            //I'm not sure if it'll return fields that have null values though
            var fields = doc.GetFields();

            //ignore our internal fields though
            foreach (Field field in fields.Cast<Field>())
            {
                string fieldName = field.Name();
                Fields.Add(fieldName, doc.Get(fieldName));
                //Examine returns some fields as e.g. __FieldName rather than fieldName
                if (fieldName.StartsWith(LuceneIndexer.SpecialFieldPrefix))
                {
                    int offset = LuceneIndexer.SpecialFieldPrefix.Length;
                    string tidiedFieldName = Char.ToLower(fieldName[offset]) + fieldName.Substring(offset + 1);
                    if (!Fields.ContainsKey(tidiedFieldName))
                    {
                        Fields.Add(tidiedFieldName, doc.Get(fieldName));
                    }
                }
            }
        }
Example #2
0
        private static void SearchByFld2(string fld, string txt)
        {
            string   strIndexDir = @"D:\Index";
            Analyzer std         = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir));
            Lucene.Net.Search.Searcher srchr     =
                new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(directory, true));


            var parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, fld, std);

            Lucene.Net.Search.Query qry = parser.Parse(txt);

            var cllctr = srchr.Search(qry, 1000);

            Console.WriteLine(cllctr.TotalHits);

            ScoreDoc[] hits = cllctr.ScoreDocs;
            for (int i = 0; i < hits.Length; i++)
            {
                int   docId = hits[i].Doc;
                float score = hits[i].Score;
                Lucene.Net.Documents.Document doc = srchr.Doc(docId);
                Console.WriteLine("索引时间:" + doc.Get("addtime"));
                Console.WriteLine("Searched from Text: " + doc.Get(fld));
            }
            Console.WriteLine("over");
        }
        // Activity 7
        /// <summary>
        /// Outputs results to the screen
        /// </summary>
        /// <param name="results">Search results</param>
        public void DisplayResults(TopDocs results)
        {
            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;

                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                if (doc.GetField("passage_text") != null)
                {
                    string Result_passage_text = doc.Get("passage_text").ToString();
                    string Result_url          = doc.Get("url").ToString();
                    Console.WriteLine("\n\nRank:    " + rank + "\nPassage text:   " + Result_passage_text);
                    Console.WriteLine("url:     " + Result_url);
                }
                else
                {
                    string Result_query   = doc.Get("query").ToString();
                    string Result_answers = doc.Get("answers").ToString();
                    Console.WriteLine("\n\nRank:    " + rank + "\nQuery:   " + Result_query);
                    Console.WriteLine("answers:     " + Result_answers);
                }
                //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue);
            }
        }
Example #4
0
 public SearchResult(Document searchDoc,string sample,float score)
 {
     this.Id = searchDoc.Get("contentid");
     this.Type = searchDoc.Get("type");
     this.Sample = sample;
     this.Score = score;
 }
Example #5
0
        public TopDocs SearchIndex(string querytext)
        {
            querytext = querytext.ToLower();
            //  var multiFieldQuery = Parse
            Query   query   = parser.Parse(querytext);
            TopDocs results = searcher.Search(query, 500);

            totalresultLabel.Text = "Total number of result is: " + (results.TotalHits).ToString();
            int i    = 0;
            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                Array.Resize <string>(ref searchResultList, i + 1);
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                //string myFieldValue = doc.Get(TEXT_FN).ToString();

                string titleValue    = doc.Get(TITLE_FN).ToString();
                string abstractValue = doc.Get(ABSTRACT_FN).ToString();

                searchResultList[i] = "Q0 " + (scoreDoc.Doc + 1) + " " + rank + " " + scoreDoc.Score + " ";
                i++;
            }

            return(results);
        }
Example #6
0
        /// <summary>
        /// Outputs results to the screen
        /// </summary>
        /// <param name="results">Search results</param>
        public void DisplayResults(TopDocs results, int pagingIndex, int maxDisplay)
        {
            listView.Items.Clear();
            int offset      = pagingIndex * maxDisplay;
            int realDisplay = Math.Min(maxDisplay, results.ScoreDocs.Length - offset);

            string[] delimiter1 = new string[] { ".I", ".T", ".A", ".B", ".W", " ." };
            int      rank       = offset;

            string[] delimiter2 = new string[] { " ." };
            //for (ScoreDoc scoreDoc in results.ScoreDocs)
            for (int j = offset; j < offset + realDisplay; j++)
            {
                ScoreDoc scoreDoc = results.ScoreDocs[j];
                rank++;
                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string       titleValue           = doc.Get(TITLE_FN).ToString();
                string       authorValue          = doc.Get(AUTHOR_FN).ToString();
                string       biblioValue          = doc.Get(BIBLIOGRAPHIC_FN).ToString();
                string       abstractValue        = doc.Get(ABSTRACT_FN).ToString();
                string[]     array = abstractValue.Split(delimiter2, StringSplitOptions.None);
                ListViewItem item  = new ListViewItem(rank.ToString());

                item.SubItems.Add(titleValue);
                item.SubItems.Add(authorValue);
                item.SubItems.Add(biblioValue);
                item.SubItems.Add(array[0]);
                item.SubItems.Add(abstractValue);

                listView.Items.Add(item);
            }
        }
Example #7
0
 public LogEntry(Document document)
 {
     Id = Guid.Parse(document.Get("Id"));
     Text = document.Get("Text");
     Timestamp = new DateTime(long.Parse(document.Get("Timestamp")));
     SourceHost = document.Get("SourceHost");
     SourceFile = document.Get("SourceFile");
 }
 private Project MapSearchResult(Document doc)
 {
     return new Project
     {
         Id = int.Parse(doc.Get(SearchingFields.Id.ToString())),
         Title = doc.Get(SearchingFields.Name.ToString())
     };
 }
		/// <summary>
		/// Maps the lucene document to data.
		/// </summary>
		/// <returns>The lucene document to data.</returns>
		/// <param name="doc">Document.</param>
		private static SearchModel _mapLuceneDocumentToData (Document doc)
		{
			return new SearchModel {
				Id = Convert.ToInt64 (doc.Get ("content_id")),
				ContentTitle = doc.Get ("content_title"),
				ContentSource = Convert.ToInt64(doc.Get ("source_id"))
			};
		}
 private Group MapSearchResult(Document doc)
 {
     return new Group
     {
         Id = int.Parse(doc.Get(SearchingFields.Id.ToString())),
         Name = doc.Get(SearchingFields.Name.ToString())
     };
 }
Example #11
0
 public LuceneArticle(Document document)
 {
     if (document != null)
     {
         this.Id = Convert.ToInt32(document.Get("Id"));
         this.Title = document.Get("Title");
         this.Content = document.Get("Content");
     }
 }
Example #12
0
 protected override ISearchResult BuildSearchResult(float score, Document document)
 {
     var searchResult = base.BuildSearchResult(score, document);
     searchResult.Content.Add("author_id", document.Get("author_id"));
     searchResult.Content.Add("author_name", document.Get("author_name"));
     searchResult.Content.Add("author_firstName", document.Get("author_firstName"));
     searchResult.Content.Add("author_lastName", document.Get("author_lastName"));
     searchResult.Content.Add("biography", document.Get("biography"));
     return searchResult;
 }
Example #13
0
        protected virtual ISearchResult BuildSearchResult(float score, Document document)
        {
            Guid entityId = Guid.Empty;
            Guid.TryParse(document.Get(IdFieldName), out entityId);
            var timestamp = DateTime.FromFileTime(long.Parse(document.Get(TimeStampFieldName)));
            var documentType = document.Get(TypeFieldName);
            string content = document.Get(ContentFieldName);

            return new SearchResult(entityId, content, score, timestamp, documentType);
        }
Example #14
0
 protected override ISearchResult BuildSearchResult(float score, Document document)
 {
     var searchResult = base.BuildSearchResult(score, document);
     //  searchResult.Content.Add("photo_id", document.Get("photo_id"));
     searchResult.Content.Add("owner", document.Get("owner"));
     searchResult.Content.Add("title", document.Get("title"));
     searchResult.Content.Add("owner_id", document.Get("owner_id"));
     searchResult.Content.Add("description", document.Get("description"));
     return searchResult;
 }
 private Lecturer MapSearchResult(Document doc)
 {
     return new Lecturer
     {
         Id = int.Parse(doc.Get(SearchingFields.Id.ToString())),
         FirstName = doc.Get(SearchingFields.FirstName.ToString()),
         MiddleName = doc.Get(SearchingFields.MiddleName.ToString()),
         LastName = doc.Get(SearchingFields.LastName.ToString()),
         Skill = doc.Get(SearchingFields.Name.ToString()) //логин
     };
 }
        /// <summary>
        /// Get the paths of Lemmas Images.
        /// <para>Give the Lemma Name to get the images path for.</para>
        /// <para>Returns a string array with the path of each image.</para>
        /// </summary>
        public string[] GetLemmaImagesPath(string lemmaName)
        {
            string directory = System.IO.Directory.GetCurrentDirectory();

            string[] splitDir = directory.Split('\\');
            if (splitDir[splitDir.Length - 1] == "Debug")
            {
                System.IO.Directory.SetCurrentDirectory("..\\..");
            }

            string[] splitLemmaName = lemmaName.Split('(');
            string[] imageTypes     = new string[] { "jpg", "svg", "png", "gif" };

            string[] results  = null;
            string   indexDir = "Index";

            using (Lucene.Net.Store.Directory dir = FSDirectory.Open(indexDir))
                using (IndexSearcher searcher = new IndexSearcher(dir))
                {
                    Term          term, term2;
                    WildcardQuery q, q2 = null;
                    BooleanQuery  bq = new BooleanQuery();

                    term = new Term("title", "*" + splitLemmaName[0].ToLower() + "*");
                    q    = new WildcardQuery(term);
                    bq.Add(q, Occur.MUST);


                    TopDocs hits = searcher.Search(bq, 10);

                    int j = 0;
                    if (hits.TotalHits > 10)
                    {
                        results = new string[10];
                    }
                    else
                    {
                        results = new string[hits.TotalHits];
                    }
                    foreach (ScoreDoc d in hits.ScoreDocs)
                    {
                        Lucene.Net.Documents.Document doc = searcher.Doc(d.Doc);
                        foreach (string type in imageTypes)
                        {
                            if (doc.Get("dataType").ToString() == type)
                            {
                                results[j++] = doc.Get("Content").ToString() + "." + doc.Get("dataType").ToString();
                            }
                        }
                    }
                }
            return(results);
        }
Example #17
0
        /* Open pre-lockless index, add docs, do a delete &
         * setNorm, and search */
        public virtual void  ChangeIndexNoAdds(System.String dirName)
        {
            dirName = FullDir(dirName);

            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirName));

            // make sure searching sees right # hits
            IndexSearcher searcher = new IndexSearcher(dir, true);

            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(34, hits.Length, "wrong number of hits");
            Document d = searcher.Doc(hits[0].Doc);

            Assert.AreEqual("21", d.Get("id"), "wrong first document");
            searcher.Close();

            // make sure we can do a delete & setNorm against this
            // pre-lockless segment:
            IndexReader reader     = IndexReader.Open(dir, false);
            Term        searchTerm = new Term("id", "6");
            int         delCount   = reader.DeleteDocuments(searchTerm);

            Assert.AreEqual(1, delCount, "wrong delete count");
            reader.SetNorm(22, "content", (float)2.0);
            reader.Close();

            // make sure they "took":
            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(33, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            TestHits(hits, 33, searcher.IndexReader);
            searcher.Close();

            // optimize
            IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);

            writer.Optimize();
            writer.Close();

            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(33, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            TestHits(hits, 33, searcher.IndexReader);
            searcher.Close();

            dir.Close();
        }
        public NamespaceModel(Document document)
        {
            Name = document.Get(NamespaceNameField);
            AssemblyName = document.Get(NamespaceAssemblyNameField);
            PackageName = document.Get(NamespacePackageNameField);
            PackageVersion = document.Get(NamespacePackageVersionField);
            var targetFrameworksFieldValue = document.Get(NamespaceTargetFrameworksField);

            TargetFrameworks = new List<string>();
            if (!string.IsNullOrEmpty(targetFrameworksFieldValue))
            {
                TargetFrameworks.AddRange(targetFrameworksFieldValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
            }
        }
        public static List <SearchResult> SeartchContact(string keyword, int startIndex, int pageSize, out int totalCount)
        {
            FSDirectory   directory = FSDirectory.Open(new DirectoryInfo(ContactIndexPath), new NoLockFactory());
            IndexReader   reader    = IndexReader.Open(directory, true);
            IndexSearcher searcher  = new IndexSearcher(reader);

            IEnumerable <string> keyList = SplitHelper.SplitWords(keyword);

            PhraseQuery queryContact = new PhraseQuery();

            foreach (var key in keyList)
            {
                queryContact.Add(new Term("contactInfo", key));
            }
            queryContact.SetSlop(100);

            BooleanQuery query = new BooleanQuery();

            query.Add(queryContact, BooleanClause.Occur.SHOULD); // SHOULD => 表示或者

            // TopScoreDocCollector:盛放查询结果的容器
            TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);

            // 使用query这个查询条件进行搜索,搜索结果放入collector
            searcher.Search(query, null, collector);
            // 首先获取总条数
            totalCount = collector.GetTotalHits();
            // 从查询结果中取出第m条到第n条的数据
            ScoreDoc[] docs = collector.TopDocs(startIndex, pageSize).scoreDocs;
            // 遍历查询结果
            List <SearchResult> resultList = new List <SearchResult>();

            for (int i = 0; i < docs.Length; i++)
            {
                // 拿到文档的id
                int docId = docs[i].doc;
                // 所以查询结果中只有id,具体内容需要二次查询
                // 根据id查询内容:放进去的是Document,查出来的还是Document
                Lucene.Net.Documents.Document doc = searcher.Doc(docId);
                SearchResult result = new SearchResult();
                result.UserId      = doc.Get("id");
                result.Name        = doc.Get("name");
                result.Email       = doc.Get("email");
                result.PhoneNumber = doc.Get("phone");
                result.Position    = doc.Get("position");
                resultList.Add(result);
            }

            return(resultList);
        }
        public void SaveResult(string filePath)
        {
            string query = searchQuery;

            CreateSearcher();
            StreamWriter sw      = new StreamWriter(filePath, true, Encoding.Default);//实例化StreamWriter
            TopDocs      results = searchResultDocs;

            // TopDocs results = SearchText(query, searcher.MaxDoc);
            saveQuery.Add(query);
            int    idx     = saveQuery.IndexOf(query) + 1;
            int    length  = Math.Abs(idx).ToString().Length;
            string queryId = "";

            if (length == 1)
            {
                queryId = "00" + idx.ToString();
            }
            if (length == 2)
            {
                queryId = "0" + idx.ToString();
            }
            if (length == 3)
            {
                queryId = idx.ToString();
            }


            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank += 1;

                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string myFieldValue = doc.Get(TEXT_FN).ToString();
                string passage_ID   = doc.Get(ID_FN_PASSAGEID).ToString();
                // string query_ID = doc.Get(ID_FN_QUERYID).ToString();
                // resultsStr += "Rank " + rank + " text " + myFieldValue + "\n";
                //string save = query + "\t" + "Q0" + "\t" + scoreDoc.Score + "\t" + "n9916113_our team";

                sw.WriteLine(queryId + "\t" + "Q0" + "\t" + passage_ID + "\t" + rank + "\t" + scoreDoc.Score + "\t" + "n9916113_n10290320_n10381112_Climbers");
                // sw.WriteLine(query_ID + "\t" + "Q0" + "\t" + passage_ID + "\t" + rank + "\t" + scoreDoc.Score + "\t" + "n9916113_n10290320_n10381112_Climbers"); //for simulation

                //resultList.Add(save);
            }
            sw.Close();
            CleanUpSearcher();
        }
Example #21
0
        //override public List<long> GetRelevantDocs(string querytext)
        override public Tuple <long, List <long> > GetRelevantDocs(string querytext)
        {
            long matched_query = 0;

            querytext = querytext.ToLower();
            Query   query   = parser.Parse(querytext);
            TopDocs results = searcher.Search(query, 10000);

            long        rank         = 0;
            float       toprank      = 0f;
            List <long> revevantList = new List <long>();

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                if (scoreDoc.Score < threshHold)
                {
                    break;
                }

                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                long            id   = Convert.ToInt64(doc.Get(QID_FN).ToString());
                string          text = doc.Get(TEXT_FN).ToString();
                CollectionQuery cq   = collectionProvider.Querys[id];
                Dictionary <long, CollectionPassage> cpd = collectionProvider.Passages;

                if (rank == 0)
                {
                    toprank       = scoreDoc.Score;
                    matched_query = cq.query_id;
                }
                rank++;


                foreach (var pid in cq.passages_id)
                {
                    if (cpd[pid].is_selected == 1)
                    {
                        revevantList.Add(pid);
                    }
                }
                Console.WriteLine(rank.ToString() + ", " + text + "(" + scoreDoc.Score.ToString("0.000") + ":" +
                                  (scoreDoc.Score / toprank).ToString("0.000") + ") ");
            }

            Console.WriteLine(string.Join(", ", revevantList));

            //return revevantList;
            return(new Tuple <long, List <long> >(matched_query, revevantList));
        }
Example #22
0
        public AcadamicPublication GetAcadamicPublicationAt(int rank)
        {
            ScoreDoc[] results = topdocs.ScoreDocs;

            AcadamicPublication newAP = new AcadamicPublication();

            Lucene.Net.Documents.Document doc = indexSearcher.Doc(results[i].Doc);
            newAP.DocID  = doc.Get(DOCID_FN);
            newAP.Title  = doc.Get(TITLE_FN);
            newAP.Author = doc.Get(AUTHOR_FN);
            newAP.BibliographicInformation = doc.Get(BIBLIOGRAPHICINFORMATION_FN);
            newAP.Abstract = doc.Get(ABSTRACT_FN);

            return(newAP);
        }
Example #23
0
        public List <String> SearchAndDisplayResults(string querytext)
        {
            topicID += 1;
            List <String> resultList = new List <String>();

            txtCont.Clear();
            resultList.Add(querytext);

            querytext = querytext.ToLower();

            Dictionary <String, float> boosts = new Dictionary <string, float>();

            boosts["passage_text"] = 2;
            boosts["title"]        = 10;

            parser = new MultiFieldQueryParser(AppLuceneVersion, new String[] { "passage_text", "title" }, analyzer, boosts);

            Query query = parser.Parse(querytext);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            TopDocs results = searcher.Search(query, 100000);

            stopwatch.Stop();

            int hits = results.TotalHits;

            resultList.Add(hits.ToString());
            resultList.Add(stopwatch.ElapsedMilliseconds.ToString());

            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string url          = doc.Get("url");
                string passage_text = doc.Get("passage_text");
                string title        = doc.Get("title");
                string id           = doc.Get("passage_id");
                resultList.Add("\nRank " + rank + "\ntitle: " + title + "\nurl: " + url + "\npassage_text: " + passage_text + "\n");
                string txt = topicID.ToString().PadLeft(3, '0') + " Q0 " + id + " " + rank + " " + scoreDoc.Score + " " + "n10057862_" + "n10296255_"
                             + "n10056084_" + "n10153853_" + "HyperGloryTeam" + "\n";
                txtCont.Add(txt);
            }
            return(resultList);
        }
Example #24
0
        /// <summary>
        /// Outputs results to the screen
        /// </summary>
        /// <param name="results">Search results</param>
        public void DisplayResults(TopDocs results)
        {
            List <String> evalrecords = new List <String>();
            int           rank        = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string field_URL = doc.Get(TEXT).ToString();
                //string field_Text = doc.Get(TEXT_PASSAGE).ToString();
                //Console.WriteLine("Rank " + rank + " score " + scoreDoc.Score + " text " + myFieldValue);
                Console.WriteLine();
                Console.WriteLine("Rank #" + rank);
                Console.WriteLine("Text: " + field_URL);
                Console.WriteLine();

                string topicId   = "001";
                string studgroup = "10124021";
                String record    = topicId + " Q0 " + TEXT + "" + rank + " " + scoreDoc.Score + " " + studgroup;
                evalrecords.Add(record);
            }
            System.IO.File.WriteAllLines(@"C:\Users\Suprith Kangokar\Desktop\SEM 3\IFN647- Advance Info & Retreival\baseline.txt", evalrecords);
        }
Example #25
0
        public IEnumerable <Guid> SearchDocuments(string criteria)
        {
            List <Guid> result = new List <Guid>();

            using (var searcher = new IndexSearcher(LuceneDirectory, false))
            {
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);

                try
                {
                    criteria = string.Format("*{0}*", criteria);

                    var queryParser = new QueryParser(Version.LUCENE_30, "Body", analyzer);
                    queryParser.AllowLeadingWildcard = true;

                    var query = queryParser.Parse(criteria);

                    var hits = searcher.Search(query, searcher.MaxDoc).ScoreDocs;

                    foreach (var hit in hits)
                    {
                        LuceneDocument luceneDocument = searcher.Doc(hit.Doc);
                        result.Add(Guid.Parse(luceneDocument.Get("FileID")));
                    }
                }
                catch { }

                analyzer.Close();
            }

            return(result);
        }
Example #26
0
        public List <string[]> DisplayResults(TopDocs results, string label)
        {
            List <string[]> retrievedDocs = new List <string[]>();

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string[] temp = new string[2];
                temp[0] = doc.Get("ID");
                temp[1] = doc.Get(label);
                retrievedDocs.Add(temp);
            }

            return(retrievedDocs);
        }
        public List <KeyValuePair <int, string> > FilterSearchResult(string filterText)
        {
            var result = new List <KeyValuePair <int, string> >();

            //Setup searcher
            IndexSearcher searcher = new IndexSearcher(idx);

            //prepare search
            var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, LUCENE_FIELD_NAME, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
            var query  = parser.Parse(filterText);

            //Query query = new TermQuery(new Term(LUCENE_FIELD_NAME, filterText));

            //Do the search
            TopDocs hits = searcher.Search(query, int.MaxValue);

            if (hits != null)
            {
                ScoreDoc[] scoreDocs = hits.ScoreDocs;
                foreach (ScoreDoc scoreDoc in scoreDocs)
                {
                    Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                    result.Add(new KeyValuePair <int, string>(Convert.ToInt32(doc.Get(LUCENE_ID_NAME)), doc.Get(LUCENE_FIELD_NAME)));
                }
            }

            return(result);
        }
        /// <summary>
        /// Get a ranked list of results
        /// </summary>
        /// <param name="query">A set of results</param>
        public List <string> GetResults(string query)
        {
            searchQuery = query;

            CreateSearcher();
            TopDocs results = SearchText(query, 100);

            searchResultDocs = results;



            List <string> resultList = new List <string>();
            int           resultNum  = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                // if (resultNum >= 10) { break; }
                resultNum += 1;
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string myFieldValue = doc.Get(TEXT_FN).ToString();
                // resultsStr += "Rank " + rank + " text " + myFieldValue + "\n";
                resultList.Add(myFieldValue);
            }

            CleanUpSearcher();

            return(resultList);
        }
Example #29
0
        public Tuple <List <float>, List <string>, int> SearchText_baseline(string querytext)
        {
            List <float>  valueListBase   = new List <float>();
            List <string> docsIdsListBase = new List <string>();

            querytext = querytext.ToLower();
            Query   query   = parser.Parse(querytext);
            TopDocs results = searcher2.Search(query, max_hits);

            if (results.TotalHits != 0)                                              // Check if there are found results
            {
                for (int i = 0; i < results.TotalHits; i++)                          // Loop through the top 10 ranked documents
                {
                    ScoreDoc scoreDoc = results.ScoreDocs[i];                        // Get the ranked document
                    Lucene.Net.Documents.Document doc = searcher2.Doc(scoreDoc.Doc); // Get document contents
                    string text  = doc.Get(TEXT_FN).ToString();                      // Get document contents by fields
                    string score = scoreDoc.Score.ToString();                        // Get document score
                    int    idxI  = text.IndexOf(".I ") + 3;                          // Get ID starting index
                    int    idxT  = text.IndexOf(".T\r\n");                           // Get title starting index
                    string id    = text.Substring(idxI, idxT - 1 - idxI);            // Get ID string
                    docsIdsListBase.Add(id.Trim());
                    valueListBase.Add(scoreDoc.Score);
                }
            }
            var result = Tuple.Create(valueListBase, docsIdsListBase, results.TotalHits);

            return(result);
        }
Example #30
0
        public static TestCustomSerializerClass CustomDeserializeFunction(Document doc)
        {
            var obj = new TestCustomSerializerClass();

            obj.PropStringList = TestCustomSerializerClass.StringToCustomList(doc.Get("CustomList"));

            return obj;
        }
Example #31
0
        public static void Highlight(Document d, string query, Analyzer analyzer)
        {
            string contents = d.Get("contents");
            SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\"><b>", "</b></span>");
            //SpanGradientFormatter formatter = new SpanGradientFormatter(10.0f, null, null, "#F1FD9F", "#EFF413");
            //SimpleHTMLEncoder encoder = new SimpleHTMLEncoder();
            SimpleFragmenter fragmenter = new SimpleFragmenter(250);
            Highlighter hiliter = new Highlighter(formatter, new QueryScorer(QueryParser.Parse(query, "contents", analyzer)));

            hiliter.SetTextFragmenter(fragmenter);
            int numfragments = contents.Length / fragmenter.GetFragmentSize() + 1;// +1 ensures its never zero. More than the required number of fragments dont harm.
            StringBuilder result = new StringBuilder("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><style>.highlight{background:yellow;}</style><head><title>Search Results - ");
            result.Append(d.Get("filename"));
            result.Append("</title></head><body><font face=Arial size=5>");
            TokenStream tokenstream = analyzer.TokenStream("contents", new System.IO.StringReader(contents));
            TextFragment[] frags = hiliter.GetBestTextFragments(tokenstream, contents, false, numfragments);
            foreach (TextFragment frag in frags)
            {
                if (frag.GetScore() > 0)
                {
                    result.Append(frag.ToString() + "<br/><hr/><br/>");

                }

            }

            string contentspath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "contents.html");
            result.Append("</font><a target=_self href=\"file:///");
            result.Append(contentspath);
            result.Append("\">View Original Document...</a>");
            result.Append("</body></html>");
            result.Replace("\n", "<br/>");

            string resultspath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "results.html");
            System.IO.File.WriteAllText(resultspath, result.ToString());
            //webBrowser1.Url = new Uri("file:///" + resultspath);

            Highlighter hiliter2 = new Highlighter(formatter, new QueryScorer(QueryParser.Parse(query, "contents", analyzer)));
            hiliter2.SetTextFragmenter(fragmenter);
            TokenStream tokstr = analyzer.TokenStream(new System.IO.StringReader(contents));
            StringBuilder htmlcontents = new StringBuilder("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><style>.highlight{background:yellow;}</style><body><font face=Arial size=5>");
            htmlcontents.Append(hiliter2.GetBestFragments(tokstr, contents, numfragments, "..."));
            htmlcontents.Append("</font></body></html>");
            htmlcontents.Replace("\n", "<br/>");
            System.IO.File.WriteAllText(contentspath, htmlcontents.ToString());
        }
 public static void AddField(JObject obj, Document document, string to, string from)
 {
     string value = document.Get(from);
     if (value != null)
     {
         obj[to] = value;
     }
 }
 public static void AddFieldBool(JObject obj, Document document, string to, string from)
 {
     string value = document.Get(from);
     if (value != null)
     {
         obj[to] = value.Equals("True", StringComparison.InvariantCultureIgnoreCase);
     }
 }
Example #34
0
        private void StartSearch()
        {
            string searchText = txtSearch.Text;
            string whitelist  = "^[a-zA-Z0-9-,. ]+$";
            Regex  pattern    = new Regex(whitelist);

            if (!pattern.IsMatch(searchText))
            {
                throw new Exception("Invalid Search Criteria");
            }

            //Supply conditions
            Analyzer    analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            QueryParser parser   = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Contents", analyzer);

            Query query = parser.Parse(searchText);

            string indexPath = HttpContext.Current.Request.PhysicalPath.Replace("Default.aspx", "Indexes\\");

            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath));

            Lucene.Net.Search.Searcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.IndexReader.Open(dir, true));

            TopDocs topDocs = searcher.Search(query, 100);

            int countResults = topDocs.ScoreDocs.Length;

            if (lblSearchResults.Text.Length > 0)
            {
                lblSearchResults.Text = "";
            }

            if (countResults > 0)
            {
                string results;

                results = string.Format("<br />Search Results <br />");
                for (int i = 0; i < countResults; i++)
                {
                    ScoreDoc scoreDoc = topDocs.ScoreDocs[i];
                    int      docId    = scoreDoc.Doc;
                    float    score    = scoreDoc.Score;

                    Lucene.Net.Documents.Document doc = searcher.Doc(docId);

                    string docPath = doc.Get("FileName");
                    string urlLink = "~/" + docPath.Substring(docPath.LastIndexOf("Help"), docPath.Length - docPath.LastIndexOf("Help")).Replace("\\", "/");
                    results += "Text found in: <a href=" + urlLink.Replace("~/Help/", "") + "?txtSearch=" + searchText + ">" + urlLink + "</a><br />";
                }
                lblSearchResults.Text += results;
            }
            else
            {
                lblSearchResults.Text = "No records found for \"" + searchText + "\"";
            }

            searcher.Dispose();
        }
Example #35
0
        public static TestCustomSerializerClass CustomMapperFunction2(Document doc)
        {
            var obj = new TestCustomSerializerClass();

            obj.PropStringList = doc.Get("PropStringList").Split(new[] { "," }, StringSplitOptions.None).ToList();

            obj.StringList = doc.Get("StringList").Split(new[] { "," }, StringSplitOptions.None).ToList();

            obj.IntList = doc.Get("IntList").Split(new[] {","}, StringSplitOptions.None)
                .Select(int.Parse)
                .ToList();

            obj.PropIntList = doc.Get("PropIntList").Split(new[] { "," }, StringSplitOptions.None)
                .Select(int.Parse)
                .ToList();

            return obj;
        }
 public void DisplayResult(TopDocs topDocs)
 {
     for (int i = 0; i < topDocs.ScoreDocs.Length; i++)
     {
         Lucene.Net.Documents.Document document = searcher.Doc(topDocs.ScoreDocs[i].Doc);
         string fieldValue = document.Get(TEXT_FN).ToString();
         Console.WriteLine("Rank " + (i + 1) + " text : " + fieldValue);
     }
 }
        /// <summary>
        /// Searches the index for the querytext
        /// </summary>
        /// <param name="querytext">The text to search the index</param>
        //public string SearchIndext(string querytext)
        public List <Dictionary <string, string> > SearchIndext(string querytext)
        {
            List <Dictionary <string, string> > resultListDict = new List <Dictionary <string, string> >();      // Initiate a result list

            Query query = DisplayQueries(querytext);

            Console.WriteLine("query is " + query);
            TopDocs results = searcher.Search(query, 100);

            System.Console.WriteLine("Number of results is " + results.TotalHits);
            int rank = 0;

            // ScoreDocs : a array stores pointers of a query
            // scoreDoc : a pointer of a query points to doc_ID and score (of the doc for the query)
            //string output = "";
            if (results.TotalHits != 0)   // Check if there are results
            {
                foreach (ScoreDoc scoreDoc in results.ScoreDocs)
                {
                    rank++;
                    Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                    string myFieldValue = doc.Get(TEXT_FN_PASS_TEXT);
                    string myURL        = doc.Get(TEXT_FN_URL);
                    string passId       = doc.Get(TEXT_FN_PASS_ID);
                    string score        = scoreDoc.Score.ToString();
                    string queryId      = doc.Get(TEXT_FN_QUERY_ID);

                    Explanation e = searcher.Explain(query, scoreDoc.Doc);

                    char     delimiters = '/';
                    string[] urlSeg     = myURL.Split(delimiters);

                    resultListDict.Add(new Dictionary <string, string> {
                        { "rank", rank.ToString() }, { "passId", passId },
                        { "score", score }, { "title", urlSeg[2] }, { "url", myURL }, { "text", myFieldValue }, { "queryId", queryId }
                    });

                    //Console.WriteLine("Rank " + rank + " text " + myFieldValue + " URL " + myURL);
                    //Console.WriteLine(e);
                }
            }

            return(resultListDict);
        }
Example #38
0
        public List <Result> DisplayResults(TopDocs results, Lucene.Net.Search.Query query)
        {
            int           rank             = 0;
            List <Result> retrievedResults = new List <Result>();

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string url       = doc.Get(URL_FN).ToString();
                string title     = GetTitle(url);
                int    passageID = Int32.Parse(doc.Get(PASSAGEID_FN));

                retrievedResults.Add(new Result(rank, scoreDoc.Score, title, url, null, null, passageID));
            }

            return(retrievedResults);
        }
Example #39
0
 public static IDictionary<string, string> DocToDict(Document doc, float score = 0)
 {
     var dict = new Dictionary<string, string>();
     foreach (var field in doc.GetFields()) {
         if (field.IsStored)
             dict[field.Name] = doc.Get(field.Name);
     }
     dict["rank"] = score.ToString(CultureInfo.InvariantCulture);
     return dict;
 }
 public BasicAuditEntry(Document doc, int luceneId)
 {
     User = doc.Get("user");
     Role = doc.GetValues("role").ToList();
     Id = new ID(doc.Get("id"));
     Path = doc.Get("path");
     DateTime tmp;
     DateTime.TryParse(doc.Get("timestamp"), out tmp);
     TimeStamp = tmp;
     EventId = doc.Get("event");
     Note = doc.Get("note");
     Label = doc.Get("label");
     Color = doc.Get("color");
     Database = doc.Get("database");
     Uid = luceneId.ToString();
 }
        private void btnFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            DialogResult res = dia.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            FSDirectory dir = FSDirectory.GetDirectory(Environment.CurrentDirectory + "\\LuceneIndex");
            //Lucene.Net.Store.RAMDirectory dir = new RAMDirectory();
            Lucene.Net.Analysis.Standard.StandardAnalyzer an = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
            IndexWriter wr = new IndexWriter(dir, an,true);
            IStemmer stemmer = new EnglishStemmer();
            DirectoryInfo diMain = new DirectoryInfo(dia.SelectedPath);
            foreach(FileInfo fi in diMain.GetFiles()){
                Document doc = new Document();
                doc.Add(new Field("title", fi.Name,Field.Store.YES, Field.Index.NO));
                //doc.Add(new Field("text", File.ReadAllText(fi.FullName),Field.Store.YES, Field.Index.TOKENIZED,Field.TermVector.YES));
                doc.Add(new Field("text", PerformStemming(stemmer,NLPToolkit.Tokenizer.TokenizeNow(File.ReadAllText(fi.FullName)).ToArray()), Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
                wr.AddDocument(doc);
            }
            wr.Optimize();
            wr.Flush();
            wr.Close();
            dir.Close();

            IndexReader reader = IndexReader.Open(dir);
            for (int i = 0; i < reader.MaxDoc(); i++)
            {
                if (reader.IsDeleted(i))
                    continue;

                Document doc = reader.Document(i);
                String docId = doc.Get("docId");
                foreach (TermFreqVector vector in reader.GetTermFreqVectors(i))
                {
                    foreach(string term in vector.GetTerms()){
                        Console.WriteLine(term);
                    }
                }
                // do something with docId here...
            }
            //IndexSearcher search = new IndexSearcher(wr.GetReader());

            //MoreLikeThis mlt = new MoreLikeThis(wr.GetReader());
            //FileInfo fitarget = new FileInfo(@"C:\Users\peacemaker\Desktop\TestNoBitcoin\test.txt");
            //Query query = mlt.Like(fitarget);

            //var hits = search.Search(query, int.MaxValue);
            //foreach (ScoreDoc doc in hits.ScoreDocs)
            //{
            //    textBox1.Text += doc.Score + Environment.NewLine;
            //}
        }
Example #42
0
        /// <summary>
        /// Searches the index for the querytext
        /// </summary>
        /// <param name="querytext">The text to search the index</param>
        public List <Dictionary <string, string> > SearchText(string querytext, int DocNum, bool check, out string processedQuery)
        {
            Query query;

            if (check)
            {
                query = MultiAsIsparser.Parse("\"" + querytext + "\"");
            }
            else
            {
                string temp       = Program.UrlPreprocessor2(querytext);
                string querytext2 = temp.ToLower();
                query = MultiParser.Parse(querytext2);
            }

            List <Dictionary <string, string> > resultList = new List <Dictionary <string, string> >();

            processedQuery = query.ToString();
            TopDocs results = searcher.Search(query, DocNum);

            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                Dictionary <string, string> dict;
                dict = new Dictionary <string, string>();
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string urlValue   = doc.Get(pureURL).ToString();
                string myPassID   = doc.Get(passID).ToString();
                string myPassText = doc.Get(passText).ToString();
                string queryID    = doc.Get(qID).ToString();
                dict.Add("rank", rank.ToString());
                dict.Add("score", scoreDoc.Score.ToString());
                dict.Add("result", myPassText.ToString());
                dict.Add("passID", myPassID.ToString());
                dict.Add("url", urlValue.ToString());
                dict.Add("qID", queryID.ToString());
                resultList.Add(dict);
            }
            return(resultList);
        }
 public SearchResult(Document luceneDocument) {
     Url = luceneDocument.Get("Url");
     Title = luceneDocument.Get("Title");
     Body = luceneDocument.Get("Body");
     Description = luceneDocument.Get("Description");
     PublishDate = luceneDocument.Get("PublishDate");
     LastUpdatedDate = luceneDocument.Get("LastUpdatedDate");
     Author = luceneDocument.Get("Author");
 }
Example #44
0
 private void CheckHits(ScoreDoc[] hits, int expectedCount, IndexSearcher searcher)
 {
     Assert.AreEqual(expectedCount, hits.Length, "total results");
     for (int i = 0; i < hits.Length; i++)
     {
         if (i < 10 || (i > 94 && i < 105))
         {
             Documents.Document d = searcher.Doc(hits[i].Doc);
             Assert.AreEqual(Convert.ToString(i), d.Get(ID_FIELD), "check " + i);
         }
     }
 }
Example #45
0
 private void PrintHits(TextWriter @out, ScoreDoc[] hits, IndexSearcher searcher)
 {
     @out.WriteLine(hits.Length + " total results\n");
     for (int i = 0; i < hits.Length; i++)
     {
         if (i < 10 || (i > 94 && i < 105))
         {
             Documents.Document d = searcher.Doc(hits[i].Doc);
             @out.WriteLine(i + " " + d.Get(ID_FIELD));
         }
     }
 }
        public void SaveResultForSimulation(string query, string filePath, string query_ID)
        {
            CreateSearcher();
            StreamWriter sw      = new StreamWriter(filePath, true, Encoding.Default); //instantiate StreamWriter
            TopDocs      results = SearchText(query, 10);

            int rank = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank += 1;

                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string myFieldValue = doc.Get(TEXT_FN).ToString();
                string passage_ID   = doc.Get(ID_FN_PASSAGEID).ToString();

                sw.WriteLine(query_ID + "\t" + "Q0" + "\t" + passage_ID + "\t" + rank + "\t" + scoreDoc.Score + "\t" + "n9916113_n10290320_n10381112_Climbers"); //for simulation
            }
            sw.Close();
            CleanUpSearcher();
        }
        protected virtual SearchEngineResult CreateSearchResult(Document doc, float score)
        {
            var result = new SearchEngineResult
            {
                ID = doc.Get("Key"),
                Title = doc.Get("Title"),
                Url = doc.Get("Url"),
                TypeID = byte.Parse(doc.Get("Type")),
                Excerpt = doc.Get("Excerpt"),
                Score = score
            };

            //-- This is out here incase later we accommodate areas that belong to more than one country
            byte countryID = 0;
            if (byte.TryParse(doc.Get("Country"), out countryID))
            {
                result.CountryID = countryID;
                result.Flag = cf.Caching.AppLookups.CountryFlag(result.CountryID);
            }

            return result;
        }
Example #48
0
		public static void  Main(System.String[] args)
		{
			try
			{
				Directory directory = new RAMDirectory();
				Analyzer analyzer = new SimpleAnalyzer();
				IndexWriter writer = new IndexWriter(directory, analyzer, true);
				
				System.String[] docs = new System.String[]{"a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c"};
				for (int j = 0; j < docs.Length; j++)
				{
					Document d = new Document();
					d.Add(Field.Text("contents", docs[j]));
					writer.AddDocument(d);
				}
				writer.Close();
				
				Searcher searcher = new IndexSearcher(directory);
				
				System.String[] queries = new System.String[]{"\"a c e\""};
				Hits hits = null;
				
				QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer);
				parser.SetPhraseSlop(4);
				for (int j = 0; j < queries.Length; j++)
				{
					Query query = parser.Parse(queries[j]);
					System.Console.Out.WriteLine("Query: " + query.ToString("contents"));
					
					//DateFilter filter =
					//  new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
					//DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
					//System.out.println(filter);
					
					hits = searcher.Search(query);
					
					System.Console.Out.WriteLine(hits.Length() + " total results");
					for (int i = 0; i < hits.Length() && i < 10; i++)
					{
						Document d = hits.Doc(i);
						System.Console.Out.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents"));
					}
				}
				searcher.Close();
			}
			catch (System.Exception e)
			{
				System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message);
			}
		}
Example #49
0
        private static string HighlightText(int id, string field, Highlighter highlighter, IndexSearcher searcher, Analyzer analyzer)
        {
            Lucene.Net.Documents.Document doc = searcher.Doc(id);
            var highlightedText = "";
            var text            = doc.Get(field);

            if (text == null)
            {
                return(String.Empty);
            }
            TokenStream tokenStream = TokenSources.GetAnyTokenStream(searcher.IndexReader, id, field, analyzer);

            highlightedText = highlighter.GetBestFragments(tokenStream, text, 2, "...");

            return(highlightedText);
        }
        // Generates top documents by ranking results
        public List <string> SaveResults(TopDocs topDocs)
        {
            List <string> searchResultList = new List <string>();
            int           rank             = 0;

            foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
            {
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                rank++;
                Lucene.Net.Documents.Document doc1 = searcher.Doc(scoreDoc.Doc);
                string myFieldValue = doc.Get(TEXT_FN).ToString();
                string result       = "Q0" + "\t" + scoreDoc.Doc + "\t" + rank + "\t" + scoreDoc.Score + "\t";
                searchResultList.Add(result);
            }
            return(searchResultList);
        }
        // Activity 7
        /// <summary>
        /// Outputs results to the screen
        /// </summary>
        /// <param name="results">Search results</param>
        public void DisplayResults(TopDocs results)
        {
            var form1 = new Form1();
            int rank  = 0;

            foreach (ScoreDoc scoreDoc in results.ScoreDocs)
            {
                rank++;
                // retrieve the document from the 'ScoreDoc' object
                Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
                string       myFieldValue         = doc.Get(TEXT_FN).ToString();
                ListViewItem item = new ListViewItem(rank.ToString());
                item.SubItems.Add(myFieldValue);
                form1.listView.Items.Add(item);
            }
        }
        public static List <int> SeartchUser(string keyword)
        {
            FSDirectory   directory = FSDirectory.Open(new DirectoryInfo(UserInfoIndexPath), new NoLockFactory());
            IndexReader   reader    = IndexReader.Open(directory, true);
            IndexSearcher searcher  = new IndexSearcher(reader);

            IEnumerable <string> keyList = SplitHelper.SplitWords(keyword);

            PhraseQuery queryUserInfo = new PhraseQuery();

            foreach (var key in keyList)
            {
                queryUserInfo.Add(new Term("userInfo", key));
            }
            queryUserInfo.SetSlop(100);

            BooleanQuery query = new BooleanQuery();

            query.Add(queryUserInfo, BooleanClause.Occur.SHOULD);


            // TopScoreDocCollector:盛放查询结果的容器
            TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);

            // 使用query这个查询条件进行搜索,搜索结果放入collector
            searcher.Search(query, null, collector);
            // 首先获取总条数
            int totalCount = collector.GetTotalHits();

            //这里取所有的数据 以方便后续的查找。
            ScoreDoc[] docs = collector.TopDocs(0, totalCount).scoreDocs;
            // 遍历查询结果
            List <int> resultList = new List <int>();

            for (int i = 0; i < docs.Length; i++)
            {
                // 拿到文档的id,因为Document可能非常占内存(DataSet和DataReader的区别)
                int docId = docs[i].doc;
                // 所以查询结果中只有id,具体内容需要二次查询
                // 根据id查询内容:放进去的是Document,查出来的还是Document
                Lucene.Net.Documents.Document doc = searcher.Doc(docId);
                int uid = Convert.ToInt32(doc.Get("id"));
                resultList.Add(uid);
            }

            return(resultList);
        }
Example #53
0
        private void DoTestSearch(Random random, StringWriter @out, bool useCompoundFile)
        {
            Store.Directory   directory = NewDirectory();
            Analyzer          analyzer  = new MockAnalyzer(random);
            IndexWriterConfig conf      = NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
            MergePolicy       mp        = conf.MergePolicy;

            mp.NoCFSRatio = useCompoundFile ? 1.0 : 0.0;
            IndexWriter writer = new IndexWriter(directory, conf);

            string[] docs = new string[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" };
            for (int j = 0; j < docs.Length; j++)
            {
                Documents.Document d = new Documents.Document();
                d.Add(NewTextField("contents", docs[j], Field.Store.YES));
                d.Add(NewStringField("id", "" + j, Field.Store.NO));
                writer.AddDocument(d);
            }
            writer.Dispose();

            IndexReader   reader   = DirectoryReader.Open(directory);
            IndexSearcher searcher = NewSearcher(reader);

            ScoreDoc[] hits = null;

            Sort sort = new Sort(SortField.FIELD_SCORE, new SortField("id", SortFieldType.INT32));

            foreach (Query query in BuildQueries())
            {
                @out.WriteLine("Query: " + query.ToString("contents"));
                if (Verbose)
                {
                    Console.WriteLine("TEST: query=" + query);
                }

                hits = searcher.Search(query, null, 1000, sort).ScoreDocs;

                @out.WriteLine(hits.Length + " total results");
                for (int i = 0; i < hits.Length && i < 10; i++)
                {
                    Documents.Document d = searcher.Doc(hits[i].Doc);
                    @out.WriteLine(i + " " + hits[i].Score + " " + d.Get("contents"));
                }
            }
            reader.Dispose();
            directory.Dispose();
        }
Example #54
0
 public Entry(Document doc, float score, int positionByScore)
 {
     this.Hash = doc.Get("Hash");
     this.Path = doc.Get("Path");
     this.Name = doc.Get("Name");
     this.Text = doc.Get("Text");
     this.All = doc.Get("All");
     this.ModifiedOn = DateTime.ParseExact(doc.Get("ModifiedOn"), DateFormat, null);
     this.Score = score;
     this.PositionByScore = positionByScore;
 }
Example #55
0
		public virtual void  TestBinaryField()
		{
			Document doc = new Document();
			Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
			Fieldable binaryFld = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal), Field.Store.YES);
			Fieldable binaryFld2 = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal2), Field.Store.YES);
			
			doc.Add(stringFld);
			doc.Add(binaryFld);
			
			Assert.AreEqual(2, doc.fields_ForNUnit.Count);
			
			Assert.IsTrue(binaryFld.IsBinary());
			Assert.IsTrue(binaryFld.IsStored());
			Assert.IsFalse(binaryFld.IsIndexed());
			Assert.IsFalse(binaryFld.IsTokenized());
			
			System.String binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(doc.GetBinaryValue("binary")));
			Assert.IsTrue(binaryTest.Equals(binaryVal));
			
			System.String stringTest = doc.Get("string");
			Assert.IsTrue(binaryTest.Equals(stringTest));
			
			doc.Add(binaryFld2);
			
			Assert.AreEqual(3, doc.fields_ForNUnit.Count);
			
			byte[][] binaryTests = doc.GetBinaryValues("binary");
			
			Assert.AreEqual(2, binaryTests.Length);
			
			binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[0]));
			System.String binaryTest2 = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[1]));
			
			Assert.IsFalse(binaryTest.Equals(binaryTest2));
			
			Assert.IsTrue(binaryTest.Equals(binaryVal));
			Assert.IsTrue(binaryTest2.Equals(binaryVal2));
			
			doc.RemoveField("string");
			Assert.AreEqual(2, doc.fields_ForNUnit.Count);
			
			doc.RemoveFields("binary");
			Assert.AreEqual(0, doc.fields_ForNUnit.Count);
		}
Example #56
0
		private NameInfo DocumentToNameInfo (Document doc)
		{
			NameInfo info;
			info = new NameInfo ();

			string str;
			str = doc.Get ("Uri");
			info.Id = GuidFu.FromUriString (str);

			bool have_name = false;
			bool have_parent_id = false;
			bool have_is_dir = false;

			foreach (Field f in doc.Fields ()) {
				Property prop;
				prop = GetPropertyFromDocument (f, doc, false);
				if (prop == null)
					continue;

				switch (prop.Key) {
					
				case Property.ExactFilenamePropKey:
					info.Name = prop.Value;
					have_name = true;
					break;
					
				case Property.ParentDirUriPropKey:
					info.ParentId = GuidFu.FromUriString (prop.Value);
					have_parent_id = true;
					break;

				case Property.IsDirectoryPropKey:
					info.IsDirectory = (prop.Value == "true");
					have_is_dir = true;
					break;
				}

				if (have_name && have_parent_id && have_is_dir)
					break;
			}

			return info;
		}
 private Student MapSearchResult(Document doc)
 {
     int groupId;
     int.TryParse(doc.Get(SearchingFields.Group.ToString()), out groupId);
     return new Student
     {
         Id = int.Parse(doc.Get(SearchingFields.Id.ToString())),
         FirstName = doc.Get(SearchingFields.FirstName.ToString()),
         MiddleName = doc.Get(SearchingFields.MiddleName.ToString()),
         LastName = doc.Get(SearchingFields.LastName.ToString()),
         Email = doc.Get(SearchingFields.Name.ToString()), //логин
         GroupId = groupId
     };
 }
        public ExtensionModel(Document document)
        {
            Name = document.Get(ExtensionNameField);
            FullName = document.Get(ExtensionFullNameField);
            Namespace = document.Get(ExtensionNamespaceField);
            AssemblyName = document.Get(ExtensionAssemblyNameField);
            PackageName = document.Get(ExtensionPackageNameField);
            PackageVersion = document.Get(ExtensionPackageVersionField);
            var targetFrameworksFieldValue = document.Get(ExtensionTargetFrameworksField);

            var targetFrameworks = new List<string>();
            if (!string.IsNullOrEmpty(targetFrameworksFieldValue))
            {
                targetFrameworks.AddRange(targetFrameworksFieldValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
            }

            TargetFrameworks = targetFrameworks;
        }
    // This method is printing out the message details given the index document.
    // NOTE: The field "mainText" must be stored in indexing level. Same goes for any
    // other field you want to search.
    private static void DisplayMessage(Document d, string searchTerm)
    {
        // THIS IS USED IN THE DATABASE INDEXic
            //Console.WriteLine("id: " + d.Get("id") + "\n" + "messageBox: " + d.Get("messageBox") + "\n" + "incoming: " + d.Get("incoming") + "\n" + "date: " + d.Get("date") + "\n" + "mainText: " + d.Get("mainText"));

            // THIS IS USED IN MY TEST FILES
            //Console.WriteLine("id: " + d.Get("id") + "\n" + "mainText: " + d.Get("mainText"));
            string text = d.Get("mainText");
            TermQuery query = new TermQuery(new Term("mainText", searchTerm));
            Lucene.Net.Search.Highlight.IScorer scorer = new QueryScorer(query);
            Highlighter highlighter = new Highlighter(scorer);
            System.IO.StringReader reader = new System.IO.StringReader(text);
            TokenStream tokenStream = new SimpleAnalyzer().TokenStream("mainText", reader);
            String[] toBePrinted = highlighter.GetBestFragments(tokenStream, text, 5); // 5 is the maximum number of fragments that gets tested
           foreach (var word in toBePrinted)
            {
                Console.Write(word);
            }

            Console.WriteLine("=====================");
            Console.ReadKey();
    }
Example #60
0
 private static LuceneSearchModel _mapLuceneDocumentToData(Document doc, float score = 0)
 {
     return new LuceneSearchModel
     {
         Id = Guid.Parse(doc.Get(AppConstants.LucId)),
         TopicName = doc.Get(AppConstants.LucTopicName),
         PostContent = doc.Get(AppConstants.LucPostContent),
         DateCreated = DateTools.StringToDate(doc.Get(AppConstants.LucDateCreated)),
         TopicId = Guid.Parse(doc.Get(AppConstants.LucTopicId)),
         UserId = Guid.Parse(doc.Get(AppConstants.LucUserId)),
         Username = doc.Get(AppConstants.LucUsername),
         TopicUrl = doc.Get(AppConstants.LucTopicUrl),
         Score = score
     };
 }