Example #1
0
        private void Create(CodeIndex m)
        {
            try
            {
                if (!this._codeIndexLucene.ExistsDoc(m.Id, IndexField.Id))
                {
                    Document doc = new Document();
                    doc.Add(this._codeIndexLucene.CreateStoreField(IndexField.Id, m.Id));
                    doc.Add(this._codeIndexLucene.CreateSearchField(IndexField.SearchText, m.SearchText));
                    doc.Add(this._codeIndexLucene.CreateStoreField(IndexField.Language, m.Language));
                    doc.Add(this._codeIndexLucene.CreateStoreField(IndexField.CodeBody, m.CodeBody));
                    doc.Add(this._codeIndexLucene.CreateStoreField(IndexField.UserId, m.UserId));
                    doc.Add(this._codeIndexLucene.CreateStoreField(IndexField.FileId, m.FileId));
                    this._codeIndexLucene.AddDoc(doc);
                }

                if (!m.FileId.IsNullOrEmpty() &&
                    !this._codeFileLucene.ExistsDoc(m.FileId))
                {
                    Document codeFileDoc = new Document();
                    codeFileDoc.Add(this._codeIndexLucene.CreateStoreField(nameof(CodeFile.Id), m.FileId));
                    codeFileDoc.Add(this._codeIndexLucene.CreateStoreField(nameof(CodeFile.FileContent), m.FileContent));
                    this._codeFileLucene.AddDoc(codeFileDoc);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
            }
        }
Example #2
0
 private void Delete(CodeIndex m)
 {
     try
     {
         this._codeIndexLucene.DeleteDoc(m.Id, IndexField.Id);
         this._codeFileLucene.DeleteDoc(m.FileId);
     }
     catch (Exception ex)
     {
         LogHelper.WriteError(this.GetType(), ex);
     }
 }
Example #3
0
        public CodeIndex Get(string id)
        {
            try
            {
                var doc = this._codeIndexLucene.GetDoc(id, IndexField.Id);
                if (doc != null)
                {
                    var m = new CodeIndex
                    {
                        Id         = doc.Get(IndexField.Id),
                        SearchText = doc.Get(IndexField.SearchText),
                        CodeBody   = doc.Get(IndexField.CodeBody),
                        UserId     = doc.Get(IndexField.UserId),
                        FileId     = doc.Get(IndexField.FileId),
                    };

                    if (!m.FileId.IsNullOrEmpty())
                    {
                        var codeDoc = this._codeFileLucene.GetDoc(m.FileId);
                        if (codeDoc != null)
                        {
                            m.FileContent = codeDoc.Get(nameof(CodeFile.FileContent));
                        }
                    }

                    return(m);
                }

                return(null);
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
                return(null);
            }
        }
Example #4
0
        public List <CodeIndex> Search(string text, string language, int size = 300)
        {
            List <CodeIndex> result = new List <CodeIndex>();

            try
            {
                bool isExistIndex = this._codeIndexLucene.IndexExists();

                if (isExistIndex && !text.IsNullOrEmpty())
                {
                    IndexSearcher searcher = new IndexSearcher(this._codeIndexLucene.OpenReader());

                    BooleanQuery allQuery = new BooleanQuery();
                    allQuery.Add(new TermQuery(new Term(IndexField.Language, language)), BooleanClause.Occur.MUST);

                    //搜索条件
                    BooleanQuery shouldQuery = new BooleanQuery();

                    //把用户输入的关键字进行分词
                    foreach (string word in SplitContent.SplitWords(text))
                    {
                        PhraseQuery query1 = new PhraseQuery();
                        query1.Add(new Term(IndexField.SearchText, word));
                        shouldQuery.Add(query1, BooleanClause.Occur.SHOULD);
                    }

                    allQuery.Add(shouldQuery, BooleanClause.Occur.MUST);

                    MultiSearcher        multiSearch = new MultiSearcher(new[] { searcher });
                    TopScoreDocCollector collector   = TopScoreDocCollector.create(size, true);
                    multiSearch.Search(allQuery, collector);

                    ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs.OrderByDescending(t => t.score).ToArray();
                    for (int i = 0; i < docs.Length; i++)
                    {
                        int      docId = docs[i].doc;         //得到查询结果文档的id(Lucene内部分配的id)
                        Document doc   = searcher.Doc(docId); //根据文档id来获得文档对象Document
                        var      m     = new CodeIndex
                        {
                            Id         = doc.Get(IndexField.Id),
                            SearchText = doc.Get(IndexField.SearchText),
                            CodeBody   = doc.Get(IndexField.CodeBody),
                            UserId     = doc.Get(IndexField.UserId),
                            Language   = doc.Get(IndexField.Language)
                        };

                        var codeDoc = this._codeFileLucene.GetDoc(m.FileId);
                        if (codeDoc != null)
                        {
                            m.FileContent = codeDoc.Get(nameof(CodeFile.FileContent));
                        }

                        result.Add(m);

                        // 当完全匹配时,只返回此条
                        if (text != null && m.SearchText != null && m.SearchText.Trim().ToLower() == text.Trim().ToLower())
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
            }

            return(result);
        }
Example #5
0
 public void Enqueue(CodeIndex m)
 {
     this._docqueue.Enqueue(m);
 }