Beispiel #1
0
        private void Insert(IndexRecord indexRecord)
        {
            IndexWriter writer = Writer;

            if (writer == null)
            {
                return;
            }

            try
            {
                Document doc = new Document();
                doc.Add(new Field(IndexRecord.URIFIELD, indexRecord.Uri, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field(IndexRecord.ENTITYFIELD, indexRecord.Entity, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field(IndexRecord.CONTENTFIELD, new StringReader(IndexRecord.ProcessContent(indexRecord.Content))));
#pragma warning disable CS0618 // Type or member is obsolete
                doc.Add(new Field(IndexRecord.TIMESTAMPFIELD, DateField.DateToString(DateTime.Now), Field.Store.YES, Field.Index.NO));
#pragma warning restore CS0618 // Type or member is obsolete

                doc.Add(new Field(IndexRecord.VIEWERFIELD, indexRecord.Viewer, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field(IndexRecord.TITLEFIELD, indexRecord.Title, Field.Store.YES, Field.Index.NOT_ANALYZED));

                int i = 1;
                foreach (string key in indexRecord.Keys)
                {
                    doc.Add(new Field(string.Format("{0}{1}", IndexRecord.KEYFIELDPREFIX, (i++).ToString()), key, Field.Store.YES, Field.Index.NO));
                }

                GXLogging.Debug(log, "AddDocument:" + indexRecord.Uri + " content:" + indexRecord.Content);
                writer.AddDocument(doc, m_analyzer);
                if (m_counter++ > Settings.Instance.OptimizeThreshold)
                {
                    m_counter = 0;
                    GXLogging.Warn(log, "Optimizing index");
                    writer.Optimize();
                }
            }
            catch (Exception e)
            {
                GXLogging.Error(log, "Insert error", e);
            }
            finally
            {
                try {
                    writer.Dispose();

                    Searcher.Instance.Close();
                }
                catch (Exception ex)
                {
                    GXLogging.Error(log, "Close writer error", ex);
                }
            }
        }
Beispiel #2
0
        public SearchResult Search(string query, int itemsPerPage, int pageNumber, IGxContext context)
        {
            if (string.IsNullOrEmpty(query))
            {
                return(SearchResult.Empty);
            }
            if (context != null)
            {
                query = TranslateQuery(query, context);
            }

            IndexSearcher searcher = GetSearcher();

            if (searcher == null)
            {
                return(new EmptySearchResult());
            }

            try
            {
                QueryParser qp = new QueryParser(Indexer.LUCENE_VERSION, IndexRecord.CONTENTFIELD, m_analyzer);

                qp.AllowLeadingWildcard = true;
                qp.DefaultOperator      = QueryParser.Operator.AND;
                Query    q  = qp.Parse(IndexRecord.ProcessContent(query));
                DateTime t1 = DateTime.Now;

                TopDocs  results = searcher.Search(q, int.MaxValue);
                DateTime t2      = DateTime.Now;

                return(new LuceneSearchResult(results, itemsPerPage, pageNumber, TimeSpan.FromTicks(t2.Ticks - t1.Ticks).TotalMilliseconds));
            }
            catch (ParseException)
            {
                GXLogging.Error(log, "Search error", new SearchException(SearchException.PARSEERROR));
                return(SearchResult.Empty);
            }
            catch (Exception ex)
            {
                GXLogging.Debug(log, "Search error", ex);
                throw new SearchException(SearchException.IOEXCEPTION);
            }
        }