Ejemplo n.º 1
0
        // inherit javadoc
        public override TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort)
        {
            TopFieldDocCollector collector = new TopFieldDocCollector(reader, sort, nDocs);

            Search(weight, filter, collector);
            return((TopFieldDocs)collector.TopDocs());
        }
Ejemplo n.º 2
0
        /// <summary> Just like {@link #Search(Weight, Filter, int, Sort)}, but you choose
        /// whether or not the fields in the returned {@link FieldDoc} instances
        /// should be set by specifying fillFields.<br/>
        ///
        /// <p/>
        /// NOTE: this does not compute scores by default. If you need scores, create
        /// a {@link TopFieldCollector} instance by calling
        /// {@link TopFieldCollector#create} and then pass that to
        /// {@link #Search(Weight, Filter, Collector)}.
        /// <p/>
        /// </summary>
        public virtual TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort, bool fillFields)
        {
            nDocs = Math.Min(nDocs, reader.MaxDoc());

            SortField[] fields = sort.fields;
            bool        legacy = false;

            for (int i = 0; i < fields.Length; i++)
            {
                SortField     field     = fields[i];
                System.String fieldname = field.GetField();
                int           type      = field.GetType();
                // Resolve AUTO into its true type
                if (type == SortField.AUTO)
                {
                    int autotype = SortField.DetectFieldType(reader, fieldname);
                    if (autotype == SortField.STRING)
                    {
                        fields[i] = new SortField(fieldname, field.GetLocale(), field.GetReverse());
                    }
                    else
                    {
                        fields[i] = new SortField(fieldname, autotype, field.GetReverse());
                    }
                }

                if (field.GetUseLegacySearch())
                {
                    legacy = true;
                }
            }

            if (legacy)
            {
                // Search the single top-level reader
                TopDocCollector     collector = new TopFieldDocCollector(reader, sort, nDocs);
                HitCollectorWrapper hcw       = new HitCollectorWrapper(collector);
                hcw.SetNextReader(reader, 0);
                if (filter == null)
                {
                    Scorer scorer = weight.Scorer(reader, true, true);
                    if (scorer != null)
                    {
                        scorer.Score(hcw);
                    }
                }
                else
                {
                    SearchWithFilter(reader, weight, filter, hcw);
                }
                return((TopFieldDocs)collector.TopDocs());
            }

            TopFieldCollector collector2 = TopFieldCollector.Create(sort, nDocs, fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.ScoresDocsOutOfOrder());

            Search(weight, filter, collector2);
            return((TopFieldDocs)collector2.TopDocs());
        }
Ejemplo n.º 3
0
        /// <summary> Just like <see cref="Search(Weight, Filter, int, Sort)" />, but you choose
        /// whether or not the fields in the returned <see cref="FieldDoc" /> instances
        /// should be set by specifying fillFields.<br/>
        /// 
        /// <p/>
        /// NOTE: this does not compute scores by default. If you need scores, create
        /// a <see cref="TopFieldCollector" /> instance by calling
        /// <see cref="TopFieldCollector.create" /> and then pass that to
        /// <see cref="Search(Weight, Filter, Collector)" />.
        /// <p/>
        /// </summary>
        public virtual TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort, bool fillFields)
        {
            nDocs = Math.Min(nDocs, reader.MaxDoc());

            SortField[] fields = sort.fields;
            bool legacy = false;
            for (int i = 0; i < fields.Length; i++)
            {
                SortField field = fields[i];
                System.String fieldname = field.GetField();
                int type = field.GetType();
                // Resolve AUTO into its true type
                if (type == SortField.AUTO)
                {
                    int autotype = SortField.DetectFieldType(reader, fieldname);
                    if (autotype == SortField.STRING)
                    {
                        fields[i] = new SortField(fieldname, field.GetLocale(), field.GetReverse());
                    }
                    else
                    {
                        fields[i] = new SortField(fieldname, autotype, field.GetReverse());
                    }
                }

                if (field.GetUseLegacySearch())
                {
                    legacy = true;
                }
            }

            if (legacy)
            {
                // Search the single top-level reader
                TopDocCollector collector = new TopFieldDocCollector(reader, sort, nDocs);
                HitCollectorWrapper hcw = new HitCollectorWrapper(collector);
                hcw.SetNextReader(reader, 0);
                if (filter == null)
                {
                    Scorer scorer = weight.Scorer(reader, true, true);
                    if (scorer != null)
                    {
                        scorer.Score(hcw);
                    }
                }
                else
                {
                    SearchWithFilter(reader, weight, filter, hcw);
                }
                return (TopFieldDocs) collector.TopDocs();
            }

            TopFieldCollector collector2 = TopFieldCollector.create(sort, nDocs, fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.ScoresDocsOutOfOrder());
            Search(weight, filter, collector2);
            return (TopFieldDocs) collector2.TopDocs();
        }
Ejemplo n.º 4
0
 // inherit javadoc
 public override TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort)
 {
     TopFieldDocCollector collector = new TopFieldDocCollector(reader, sort, nDocs);
     Search(weight, filter, collector);
     return (TopFieldDocs) collector.TopDocs();
 }
Ejemplo n.º 5
0
    ////string[] querys = { "dominos pizza" };
    //string[] fields = { "title" };
    //Lucene.Net.Search.BooleanClause.Occur[] clauses = { Lucene.Net.Search.BooleanClause.Occur.MUST };
    //string[] sortby = { "id" };
    //XmlDocument xDoc = GetSearchResult(@"C:\luceneindexpath\", querys, fields, clauses, sortby);
    //return xDoc;
    public static XmlDocument GetSearchResult(string Indexpath, string[] querys, string[] fields, BooleanClause.Occur[] clauses, string[] sortingfields)
    {
        //XMLDocument is created to return output records
        XmlDocument xResDoc = new XmlDocument();
        xResDoc.LoadXml("<SEARCHRESULTS></SEARCHRESULTS>");
        try
        {
            // This block will parse the given queries
            // (i.e) would remove characters other than a-z and 0-9
            for (int i = 0; i < querys.Length; i++)
            {
                querys[i] = querys[i].ToLower();
                querys[i] = System.Text.RegularExpressions.Regex.Replace(querys[i], "[^a-z0-9]", " ");
                while (querys[i].Contains("  "))
                    querys[i] = querys[i].Replace("  ", " ");
            }
            //

            if (System.IO.Directory.Exists(Indexpath))
            {
                // Open's Already created Lucene Index for searching
                Lucene.Net.Index.IndexReader idxReader = Lucene.Net.Index.IndexReader.Open(Indexpath);
                Searcher searcher = new IndexSearcher(idxReader);

                //Creates a Lucene Query with given search inputs
                Query qry = MultiFieldQueryParser.Parse(querys, fields, clauses, new StandardAnalyzer());
                ScoreDoc[] hits;

                //Assigns No. of Records to be fetched
                //here idxReader max records is used
                //can be changed to get specific top records
                int top = idxReader.MaxDoc();

                //To get output with specific sort
                if (sortingfields.Length > 0)
                {
                    TopFieldDocCollector collector = new TopFieldDocCollector(idxReader, new Sort(sortingfields), top);
                    searcher.Search(qry, collector);
                    hits = collector.TopDocs().scoreDocs;
                }
                else //To get output without specific sort
                {
                    TopDocCollector collector = new TopDocCollector(top);
                    searcher.Search(qry, collector);
                    hits = collector.TopDocs().scoreDocs;
                }

                //Loops through the output records and creates an XMLElement for XML Output
                for (int i = 0; i < hits.Length; i++)
                {
                    int docId = hits[i].doc;
                    Document doc = searcher.Doc(docId);
                    XmlElement xEle = xResDoc.CreateElement("SEARCHRESULT");
                    foreach (Field fl in doc.Fields())
                    {
                        xEle.SetAttribute(fl.Name(), doc.Get(fl.Name()));
                    }
                    xResDoc.DocumentElement.AppendChild(xEle);
                }
                searcher.Close();
                idxReader.Close();
            }
        }
        catch (Exception ex)
        {
            //Print Error Message
        }
        return xResDoc;
    }
Ejemplo n.º 6
0
        protected override IEnumerable<LucObject> DoExecute(Query query, bool allVersions, IndexReader idxReader, out int totalCount)
        {
            Searcher searcher = null;

            BeginFullExecutingTime();
            try
            {
                IEnumerable<LucObject> result;
                searcher = new IndexSearcher(idxReader);
                TopDocs topDocs;
                ScoreDoc[] hits;
                var top = 100000; // 501; //idxReader.MaxDoc();
                if (this.LucQuery.SortFields.Length > 0)
                {
                    BeginKernelTime();
                    TopFieldDocCollector collector = new TopFieldDocCollector(idxReader, new Sort(this.LucQuery.SortFields), top);
                    searcher.Search(query, collector);
                    FinishKernelTime();
                    BeginCollectingTime();
                    topDocs = collector.TopDocs();
                    totalCount = topDocs.TotalHits;
                    hits = topDocs.ScoreDocs;
                    FinishCollectingTime();
                }
                else
                {
                    BeginKernelTime();
                    TopDocCollector collector = new TopDocCollector(top);
                    searcher.Search(query, collector);
                    FinishKernelTime();
                    BeginCollectingTime();
                    topDocs = collector.TopDocs();
                    totalCount = topDocs.TotalHits;
                    hits = topDocs.ScoreDocs;
                    FinishCollectingTime();
                }
                BeginPagingTime();
                result = GetResultPage(hits, searcher, allVersions);
                FinishPagingTime();


                return result;
            }
            catch
            {
                FinishKernelTime();
                FinishCollectingTime();
                FinishPagingTime();
                throw;
            }
            finally
            {
                FinishFullExecutingTime();
                if (searcher != null)
                {
                    searcher.Close();
                    searcher = null;
                }
            }
        }