예제 #1
0
            public override int DoHighlight(IndexReader reader, int doc, string field, Document document, Analyzer analyzer, string text)
            {
                FieldQuery fq = highlighter.GetFieldQuery(myq, reader);

                string[] fragments = highlighter.GetBestFragments(fq, reader, doc, field, outerInstance.m_fragSize, outerInstance.m_maxFrags);
                return(fragments != null ? fragments.Length : 0);
            }
예제 #2
0
        private Dictionary <string, Dictionary <string, string[]> > GetHighlighterResults(IndexQueryServerSide query, IndexSearcher searcher, ScoreDoc scoreDoc, Document document, global::Lucene.Net.Documents.Document luceneDocument, JsonOperationContext context)
        {
            Debug.Assert(_highlighter != null);
            Debug.Assert(_highlighterQuery != null);

            var results = new Dictionary <string, Dictionary <string, string[]> >();

            foreach (var highlighting in query.Metadata.Highlightings)
            {
                var fieldName      = highlighting.Field.Value;
                var indexFieldName = query.Metadata.IsDynamic
                    ? AutoIndexField.GetSearchAutoIndexFieldName(fieldName)
                    : fieldName;

                var fragments = _highlighter.GetBestFragments(
                    _highlighterQuery,
                    searcher.IndexReader,
                    scoreDoc.Doc,
                    indexFieldName,
                    highlighting.FragmentLength,
                    highlighting.FragmentCount,
                    _state);

                if (fragments == null || fragments.Length == 0)
                {
                    continue;
                }

                var options = highlighting.GetOptions(context, query.QueryParameters);

                string key;
                if (options != null && string.IsNullOrWhiteSpace(options.GroupKey) == false)
                {
                    key = luceneDocument.Get(options.GroupKey, _state);
                }
                else
                {
                    key = document.Id;
                }

                if (results.TryGetValue(fieldName, out var result) == false)
                {
                    results[fieldName] = result = new Dictionary <string, string[]>();
                }

                if (result.TryGetValue(key, out var innerResult))
                {
                    Array.Resize(ref innerResult, innerResult.Length + fragments.Length);
                    Array.Copy(fragments, 0, innerResult, innerResult.Length, fragments.Length);
                }
                else
                {
                    result[key] = fragments;
                }
            }

            return(results);
        }
예제 #3
0
        public static IEnumerable <SearchResult> Search(
            string queryString,
            out int totalResults,
            int maxResults = 100)
        {
            totalResults = 0;
            if (string.IsNullOrEmpty(queryString))
            {
                return(new List <SearchResult>());
            }

            var indexReader = DirectoryReader.Open(
                directory: Index,
                readOnly: true);
            var indexSearcher = new IndexSearcher(indexReader);
            var queryParser   = new QueryParser(
                Lucene.Net.Util.Version.LUCENE_30,
                "Body",
                Analyzer
                );

            queryParser.AllowLeadingWildcard = true;
            queryParser.DefaultOperator      = QueryParser.Operator.AND;
            var query = queryParser.Parse(queryString);

            var resultsCollector = TopScoreDocCollector.Create(
                numHits: maxResults,
                docsScoredInOrder: true
                );

            indexSearcher.Search(
                query: query,
                results: resultsCollector
                );
            totalResults = resultsCollector.TotalHits;
            var result = new List <SearchResult>();

            var fvh = new FastVectorHighlighter();
            var fq  = fvh.GetFieldQuery(query);

            foreach (var scoreDoc in resultsCollector.TopDocs().ScoreDocs)
            {
                string[] fragments    = fvh.GetBestFragments(fq, indexSearcher.IndexReader, scoreDoc.Doc, "Body", 100, 5);
                var      doc          = indexSearcher.Doc(scoreDoc.Doc);
                var      searchResult = new SearchResult(
                    type: doc.Get("Type"),
                    id: doc.Get("ID"),
                    snippets: fragments
                    );
                result.Add(searchResult);
            }
            return(result);
        }
예제 #4
0
        public IEnumerable <SearchResult> Search(
            string queryString,
            string context,
            string subContext,
            out int totalResults,
            int maxResults)
        {
            totalResults = 0;
            queryString  = (queryString ?? "").Replace(":", " ");
            if (string.IsNullOrWhiteSpace(queryString))
            {
                return(new List <SearchResult>());
            }

            queryString = AddContextCriteria(queryString, context, subContext);

            IndexSearcher indexSearcher = SearcherProvider.GetIndexSearcher();
            var           analyser      = AnalyzerProvider.GetAnalyzer();
            var           queryParser   = new QueryParser(
                Lucene.Net.Util.Version.LUCENE_30,
                Consts.FullTextFieldName,
                analyser);

            queryParser.AllowLeadingWildcard = true;
            queryParser.DefaultOperator      = QueryParser.Operator.AND;

            var query = queryParser.Parse(queryString);

            var resultsCollector = TopScoreDocCollector.Create(
                numHits: 9999,
                docsScoredInOrder: true
                );

            indexSearcher.Search(
                query: query,
                results: resultsCollector
                );
            totalResults = resultsCollector.TotalHits;
            var result = new List <SearchResult>();

            var highlighter = new FastVectorHighlighter();
            var fieldQuery  = highlighter.GetFieldQuery(query);

            foreach (var scoreDoc in resultsCollector.TopDocs().ScoreDocs.Take(maxResults))
            {
                string[] fragments = highlighter.GetBestFragments(
                    fieldQuery: fieldQuery,
                    reader: indexSearcher.IndexReader,
                    docId: scoreDoc.Doc,
                    fieldName: Consts.FullTextFieldName,
                    fragCharSize: 100,
                    maxNumFragments: 5);
                var doc          = indexSearcher.Doc(scoreDoc.Doc);
                var searchResult = new SearchResult(
                    type: doc.Get(Consts.SerializedObjectTypeFieldName),
                    document: doc,
                    snippets: fragments
                    );
                result.Add(searchResult);
            }
            return(result);
        }