Ejemplo n.º 1
0
        private void btnExecuteSearch_Click(object sender, EventArgs e)
        {
            Directory indexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(tempPath));
            IndexSearcher searcher = new IndexSearcher(indexDirectory, true); // read-only=true

            // TODO: QueryParser support for Hebrew terms (most concerning issue is with acronyms - mid-word quotes)
            QueryParser qp = new QueryParser("content", analyzer);
            qp.SetDefaultOperator(QueryParser.Operator.AND);
            Query query = qp.Parse(txbSearchQuery.Text);

            ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;

            // Iterate through the results:
            BindingList<SearchResult> l = new BindingList<SearchResult>();
            for (int i = 0; i < hits.Length; i++)
            {
                Document hitDoc = searcher.Doc(hits[i].doc);
                SearchResult sr = new SearchResult(hitDoc.GetField("title").StringValue(),
                    hitDoc.GetField("path").StringValue(), hits[i].score);
                l.Add(sr);
            }

            searcher.Close();
            indexDirectory.Close();

            dgvResults.DataSource = l;
        }
Ejemplo n.º 2
0
        public void TestSingleCharTokenAnalyzer()
        {
            Analyzer analyzer = new SingleCharTokenAnalyzer();
            IndexSearcher src = CreateIndex("[email protected] 1234567890 abcdefgh", analyzer);

            QueryParser p = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "field", analyzer);
            p.SetDefaultOperator(QueryParser.Operator.AND);
            p.SetEnablePositionIncrements(true);

            TopDocs td = null;

            td = src.Search(p.Parse("usergmail"), 10);
            Assert.AreEqual(0, td.totalHits);

            td = src.Search(p.Parse("gmailcom"), 10);
            Assert.AreEqual(0, td.totalHits);

            td = src.Search(p.Parse("678"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("someuser"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("omeuse"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("omeuse 6789"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("user gmail"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("\"user gmail\""), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("user@gmail"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("gmail.com"), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("\"gmail.com 1234\""), 10);
            Assert.AreEqual(1, td.totalHits);

            td = src.Search(p.Parse("\"gmail.com defg\""), 10);
            Assert.AreEqual(0, td.totalHits);

            td = src.Search(p.Parse("gmail.com defg"), 10);
            Assert.AreEqual(1, td.totalHits);
        }
Ejemplo n.º 3
0
        public TextSearchResult Search(string query, Tenant tenant)
        {
            var result = new TextSearchResult(module);

            if (string.IsNullOrEmpty(query) || !Directory.Exists(path))
            {
                return result;
            }

            var dir = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo(path));
            var searcher = new IndexSearcher(dir, false);
            try
            {
                var analyzer = new AnalyzersProvider().GetAnalyzer(tenant.GetCulture().TwoLetterISOLanguageName);
                var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Text", analyzer);
                parser.SetDefaultOperator(QueryParser.Operator.AND);
                if (TextIndexCfg.MaxQueryLength < query.Length)
                {
                    query = query.Substring(0, TextIndexCfg.MaxQueryLength);
                }
                Query q = null;
                try
                {
                    q = parser.Parse(query);
                }
                catch (Lucene.Net.QueryParsers.ParseException) { }
                if (q == null)
                {
                    q = parser.Parse(QueryParser.Escape(query));
                }

#pragma warning disable 618
                var hits = searcher.Search(q);
#pragma warning restore 618
                for (int i = 0; i < hits.Length(); i++)
                {
                    var doc = hits.Doc(i);
                    result.AddIdentifier(doc.Get("Id"));
                }
            }
            finally
            {
                searcher.Close();
                dir.Close();
            }
            return result;
        }
Ejemplo n.º 4
0
        public SearchService()
        {
            var tmpluceneIndexDir = LuceneStore.FSDirectory.Open(INDEX_DIR);

            luceneIndexDir = new LuceneStore.RAMDirectory(tmpluceneIndexDir);
            tmpluceneIndexDir.Close();

            analyzer = new StandardAnalyzer(LuceneUtil.Version.LUCENE_29);

            reader = IndexReader.Open(luceneIndexDir, true); // only searching, so read-only=true

            searcher = new IndexSearcher(reader);

            // parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "analized_path", analyzer);
             parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "name", analyzer);

             parser.SetDefaultOperator(QueryParser.Operator.AND);
             parser.SetAllowLeadingWildcard(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 提供搜索的方法
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="fileType">文档类型</param>
        public static TopDocs PanguQuery(string keyword, string fileType)
        {
            ParallelMultiSearcher multiSearch = IndexManager.GenerateMultiSearcher(fileType);

            if (!multiSearch.GetSearchables().Any())
            {
                return null;
            }
            #region 生成Query语句

            var field = new string[2];
            field[0] = "fileName";
            field[1] = "fileContent";

            var boolQuery = new BooleanQuery();

            if (!string.IsNullOrEmpty(keyword))
            {

                var keywordQuery = new BooleanQuery();
                string queryKeyword = GetKeyWordsSplitBySpace(keyword, new PanGuTokenizer());//对关键字进行分词处理
                #region 查询fileName

                var fileNameParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, field[0], IndexManager.Analyzer);
                fileNameParser.SetDefaultOperator(QueryParser.Operator.AND);
                var fileNameQuery = fileNameParser.Parse(queryKeyword + "~");  // ~ 提供模糊查询
                keywordQuery.Add(fileNameQuery, BooleanClause.Occur.SHOULD);

                #endregion
                #region 查询fileContent

                var fileContentParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, field[1], IndexManager.Analyzer);
                fileContentParser.SetDefaultOperator(QueryParser.Operator.AND);
                var fileContentQuery = fileContentParser.Parse(queryKeyword + "~");
                keywordQuery.Add(fileContentQuery, BooleanClause.Occur.SHOULD);

                #endregion
                boolQuery.Add(keywordQuery, BooleanClause.Occur.MUST);
            }
            #endregion

            return multiSearch.Search(boolQuery, null, 1000);
        }
Ejemplo n.º 6
0
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(SearchTextBox.Text))
        {
            Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.RAMDirectory(luceneDBPath);

            String srch = SearchTextBox.Text;
            Lucene.Net.Search.IndexSearcher idx = new Lucene.Net.Search.IndexSearcher(ramDir);
            Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser("_searchtxt", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
            qp.SetDefaultOperator(Lucene.Net.QueryParsers.QueryParser.Operator.AND);
            Lucene.Net.Search.BooleanQuery.SetMaxClauseCount(100);

            Lucene.Net.Search.Hits hits = idx.Search(qp.Parse(srch));

            List<int> aIds = new List<int>();
            for (int i = 0; i < hits.Length(); i++)
            {
                Lucene.Net.Documents.Document doc = hits.Doc(i);
                int aid = 0;
                if (int.TryParse(doc.Get("id"), out aid))
                {
                    aIds.Add(aid);
                }
            }

            using (DataClassesDataContext dtx = new DataClassesDataContext())
            {
                var arts = from a in dtx.Articles
                           where aIds.Contains(a.Id)
                           select a;

                DataList1.DataSource = arts;
                DataList1.DataBind();
            }

            idx.Close();

            GC.Collect();
        }
    }
Ejemplo n.º 7
0
        public static Lucene.Net.Search.Query Translate(Query query)
        {
            BooleanQuery bq = new BooleanQuery();

            if (query.Text != null && query.Text.Trim().Length > 0)
            {
                // TODO: Clean up default field using schema, if available
                QueryParser parser = new QueryParser("Text", new StandardAnalyzer());
                parser.SetDefaultOperator(QueryParser.Operator.AND);
                bq.Add(
                    parser.Parse(query.Text),
                    LBooleanClause.Occur.MUST
                    );
            }

            foreach (QueryClause term in query.Children)
            {
                ProcessClause(bq, term);
            }

            return bq;
        }
Ejemplo n.º 8
0
		public virtual QueryParser GetParser(Analyzer a)
		{
			if (a == null)
				a = new SimpleAnalyzer();
			QueryParser qp = new QueryParser("field", a);
			qp.SetDefaultOperator(QueryParser.OR_OPERATOR);
			return qp;
		}
Ejemplo n.º 9
0
        public List<TextSearched> SearchHistoryByUserPattern(string pattern)
        {
            IndexSearcher searcher = new IndexSearcher(m_HistoryPath);
            List<TextSearched> list = null;
            try
            {
                list = new List<TextSearched>();

                QueryParser parserUser = new QueryParser(Constants.VisitUser, new WhitespaceAnalyzer());
                parserUser.SetDefaultOperator(QueryParser.AND_OPERATOR);
                QueryParser parserPattern = new QueryParser(Constants.SearchedText, new WhitespaceAnalyzer());
                parserPattern.SetDefaultOperator(QueryParser.AND_OPERATOR);

                Query queryUser = parserUser.Parse(System.Threading.Thread.CurrentPrincipal.Identity.Name);
                Query queryPattern = parserPattern.Parse(pattern);

                BooleanQuery query = new BooleanQuery();
                query.Add(queryUser, BooleanClause.Occur.MUST);
                query.Add(queryPattern, BooleanClause.Occur.MUST);

                Hits hits = searcher.Search(query);
                for (int i = 0; i < hits.Length(); i++)
                {
                    TextSearched st = new TextSearched();

                    Document doc = hits.Doc(i);
                    st.DateTicks = doc.Get(Constants.DateTicks);
                    st.VisitUser = doc.Get(Constants.VisitUser);
                    st.SearchedText = doc.Get(Constants.SearchedText);

                    list.Add(st);
                }
                list.Sort();
                return list;
            }
            finally
            {
                searcher.Close();
            }
        }
Ejemplo n.º 10
0
        public List<IndexedFile> Search(string pattern)
        {
            IndexSearcher searcher = new IndexSearcher(m_SearchPath);
            List<IndexedFile> list = null;
            try
            {
                list = new List<IndexedFile>();

                QueryParser parser = new QueryParser(Constants.Content, new StandardAnalyzer());
                parser.SetDefaultOperator(QueryParser.AND_OPERATOR);
                string tokenizedPattern = Tokenizer.Tokenize(pattern, false);
                if (pattern != tokenizedPattern)
                    pattern = pattern + " OR \"" + tokenizedPattern + "\"";

                Query query = parser.Parse(pattern);

                Hits hits = searcher.Search(query);
                for (int i = 0; i < hits.Length(); i++)
                {
                    IndexedFile fi = new IndexedFile();

                    Document doc = hits.Doc(i);
                    fi.FileFullName = doc.Get(Constants.Full);
                    fi.FileName = doc.Get(Constants.File);
                    fi.FileFolderName = doc.Get(Constants.Folder);
                    fi.FileExtension = doc.Get(Constants.Extension);
                    fi.FileSizeinBytes = long.Parse(doc.Get(Constants.Size));

                    list.Add(fi);
                }
                return list;
            }
            finally
            {
                searcher.Close();
            }
        }
        public ActionResult Index(Models.Search model)
        {
            if (!string.IsNullOrEmpty(model.Phrase))
            {

                //// Use this style query to search for documents - it returns all results
                //// including those matching only some of the terms
                //Query query = new QueryParser(
                //        Lucene.Net.Util.Version.LUCENE_29,
                //        "title",
                //        new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                //    ).Parse(model.Phrase);

                // Use this style query for products - all of the terms entered must match.
                QueryParser parser = new QueryParser(
                        Lucene.Net.Util.Version.LUCENE_29,
                        "title",
                        new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                    );

                //ISynonymEngine engine = new SpecialSynonymEngine();

                //// Same as above, but modified to handle synonyms
                //QueryParser parser = new QueryParser(
                //        Lucene.Net.Util.Version.LUCENE_29,
                //        "title",
                //        new SynonymAnalyzer(Lucene.Net.Util.Version.LUCENE_29, engine)
                //    );

                // This ensures all words must match in the phrase
                parser.SetDefaultOperator(QueryParser.Operator.AND);

                // This ensures similar words will match
                //parser.SetPhraseSlop(3);

                // Sets the current culture
                parser.SetLocale(System.Threading.Thread.CurrentThread.CurrentCulture);
                Query query = parser.Parse(model.Phrase);

                // Use query.Combine to merge this query with individual facets ??

                //// Get the terms from the query
                //string[] terms = model.Phrase.Split(" ".ToCharArray());

                //PhraseQuery query = new PhraseQuery();
                //query.SetSlop(4);

                //foreach (var term in terms)
                //{
                //    query.Add(new Term("title", term));
                //}

                BrowseResult result = this.PerformSearch(query, this.IndexDirectory, null);

                //// Build results for display
                //int totalHits = result.NumHits;
                //BrowseHit[] hits = result.Hits;

                //model.Hits = result.Hits;
                //model.FacetMap = result.FacetMap;
                ////model.TotalHitCount = totalHits;

                PopulateModelResult(model, result);
            }

            return View(model);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Creates the basis query that all QueryTypes use
        /// </summary>
        /// <param name="queryTerm"></param>
        /// <param name="fields"></param>
        /// <param name="weighting"></param>
        /// <param name="analyzer"></param>
        /// <returns></returns>
        private Query MultiFieldQuery(string queryTerm, IList<string> fields, IList<float> weighting, Lucene.Net.Analysis.Analyzer analyzer)
        {
            BooleanQuery combinedQuery = new BooleanQuery();

            for (int i = 0; i < fields.Count; i++)
            {
                QueryParser qp = new QueryParser(fields[i], analyzer);
                qp.SetDefaultOperator(QueryParser.Operator.AND);
                Query fieldQuery;

                try
                {
                    fieldQuery = qp.Parse(queryTerm);
                }
                catch(ParseException)
                {
                    fieldQuery = qp.Parse(QueryParser.Escape(queryTerm));
                }

                fieldQuery.SetBoost(weighting[i]);

                combinedQuery.Add(fieldQuery, BooleanClause.Occur.SHOULD);
            }

            return combinedQuery;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Add a restriction to an existing BooleanQuery
        /// </summary>
        /// <param name="boolQuery"></param>
        /// <param name="field"></param>
        /// <param name="term"></param>
        /// <param name="analyzer"></param>
        /// <returns></returns>
        private Query RestrictSearch(BooleanQuery boolQuery, string field, string term, Lucene.Net.Analysis.Analyzer analyzer)
        {
            QueryParser qp = new QueryParser(field, analyzer);
            qp.SetDefaultOperator(QueryParser.Operator.AND);
            Query q = qp.Parse(term);

            BooleanQuery restrictedQuery = new BooleanQuery();
            restrictedQuery.Add(boolQuery, BooleanClause.Occur.MUST);
            restrictedQuery.Add(q, BooleanClause.Occur.MUST);

            return restrictedQuery;
        }
Ejemplo n.º 14
0
		public virtual Query GetQueryDOA(System.String query, Analyzer a)
		{
			if (a == null)
				a = new SimpleAnalyzer();
			QueryParser qp = new QueryParser("field", a);
			qp.SetDefaultOperator(QueryParser.AND_OPERATOR);
			return qp.Parse(query);
		}
Ejemplo n.º 15
0
        public Dictionary<MsDocEntryPoint, Dictionary<MsDocEntryPointMethod, string>> Search(string query, List<MsDocEntryPoint> points)
        {
            var parser = new QueryParser("summary", new StandardAnalyzer());
            parser.SetDefaultOperator(QueryParser.Operator.AND);
            Query q = null;
            try
            {
                q = parser.Parse(MakeQuery(query));
            }
            catch (Lucene.Net.QueryParsers.ParseException)
            {
                q = parser.Parse(QueryParser.Escape(query));
            }

            var result = new Dictionary<MsDocEntryPoint, ICollection<MsDocEntryPointMethod>>();
            var hits = searcher.Search(q, Sort.RELEVANCE);
            for (int i = 0; i < hits.Length(); i++)
            {
                var doc = hits.Doc(i);
                var point = doc.Get("point");
                var method = doc.Get("path");
                if (!string.IsNullOrEmpty(method))
                {
                    var entryPoint = points.Single(x => x.Name == point);
                    var pointMethod = entryPoint.Methods.Single(x => x.ToString() == method);
                    ICollection<MsDocEntryPointMethod> foundPoints;
                    if (result.TryGetValue(entryPoint, out foundPoints))
                    {
                        foundPoints.Add(pointMethod);
                    }
                    else
                    {
                        foundPoints = new List<MsDocEntryPointMethod> { pointMethod };
                        result.Add(entryPoint, foundPoints);
                    }
                }
            }
            return result.ToDictionary(x => x.Key, y => y.Value.ToDictionary(key => key, value => string.Empty));
        }
Ejemplo n.º 16
0
        private Query CreateQuery(string field, string term)
        {
            QueryParser parser = new QueryParser(Version.LUCENE_29, field, Analyzer);

            parser.SetFuzzyMinSim(0.70f);
            parser.SetFuzzyPrefixLength(3);
            parser.SetDefaultOperator(QueryParser.Operator.OR);
            parser.SetAllowLeadingWildcard(true);

            return parser.Parse(term);
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            LuceneStore.Directory indexFSDir = LuceneStore.FSDirectory.Open(INDEX_DIR);

            LuceneStore.RAMDirectory indexDir = new LuceneStore.RAMDirectory(indexFSDir);

            Analyzer analyzer = new StandardAnalyzer(LuceneUtil.Version.LUCENE_29);

            var lastAccess = INDEX_DIR.LastAccessTime;

            //if (lastAccess.AddMinutes(10) < DateTime.Now)
            //{
            //    IndexPrograms(indexDir, analyzer);
            //}

            IndexReader reader = IndexReader.Open(indexDir, true); // only searching, so read-only=true

            Searcher searcher = new IndexSearcher(reader);
            QueryParser parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "analized_path", analyzer);
            string[] queryFields = new[] { "analized_path", "name", "orig_path" };
            Stopwatch stwQuery = new Stopwatch();

            var endkey = new ConsoleKeyInfo('q', ConsoleKey.Q,false,false,false);

            Console.WriteLine("Press q-key for end, another key for searching");

            while (Console.ReadKey() != endkey)
            {

                Console.Clear();

                Console.WriteLine("Searching, type a query:");
                string userquery = Console.ReadLine();

                string[] queries = new[] { userquery, userquery, userquery };

                //QueryParser parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "analized_path", analyzer);
                //Query query = parser.Parse(userquery);

                stwQuery.Start();

               // Query query = MultiFieldQueryParser.Parse(LuceneUtil.Version.LUCENE_29, queries, queryFields, analyzer);
                parser.SetDefaultOperator(QueryParser.Operator.AND);
                Query query = parser.Parse(userquery);
                var tophits = searcher.Search(query, 15);
                stwQuery.Stop();

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("total number of hits {0} in {1} ms {2}", tophits.totalHits,stwQuery.ElapsedMilliseconds,stwQuery.Elapsed);
                Console.ForegroundColor = ConsoleColor.White;

                for (int i = 0; i < tophits.scoreDocs.Length; i++)
                {
                    int docId = tophits.scoreDocs[i].doc;

                    Document doc = searcher.Doc(docId);

                    Console.ForegroundColor = ConsoleColor.Green;

                    Console.WriteLine("\n{0} {1}", i + 1, doc.GetField("name"));
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("------------------------------------------");
                    Console.WriteLine("Path= {0} ", doc.GetField("path"));
                    Console.WriteLine("Analized Path= {0} ", doc.GetField("analized_path"));
                    //Console.WriteLine("Orig Path= {0} ", doc.GetField("orig_path"));

                }

                Console.WriteLine("\n Press q-key for end, another key for searching");

            }

            searcher.Close();
            reader.Close();

            Console.ReadKey();
        }
Ejemplo n.º 18
0
		public virtual void  TestSimple()
		{
			AssertQueryEquals("term term term", null, "term term term");
			AssertQueryEquals("türm term term", new WhitespaceAnalyzer(), "türm term term");
			AssertQueryEquals("ümlaut", new WhitespaceAnalyzer(), "ümlaut");
			
			AssertQueryEquals("\"\"", new KeywordAnalyzer(), "");
			AssertQueryEquals("foo:\"\"", new KeywordAnalyzer(), "foo:");
			
			AssertQueryEquals("a AND b", null, "+a +b");
			AssertQueryEquals("(a AND b)", null, "+a +b");
			AssertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
			AssertQueryEquals("a AND NOT b", null, "+a -b");
			AssertQueryEquals("a AND -b", null, "+a -b");
			AssertQueryEquals("a AND !b", null, "+a -b");
			AssertQueryEquals("a && b", null, "+a +b");
			AssertQueryEquals("a && ! b", null, "+a -b");
			
			AssertQueryEquals("a OR b", null, "a b");
			AssertQueryEquals("a || b", null, "a b");
			AssertQueryEquals("a OR !b", null, "a -b");
			AssertQueryEquals("a OR ! b", null, "a -b");
			AssertQueryEquals("a OR -b", null, "a -b");
			
			AssertQueryEquals("+term -term term", null, "+term -term term");
			AssertQueryEquals("foo:term AND field:anotherTerm", null, "+foo:term +anotherterm");
			AssertQueryEquals("term AND \"phrase phrase\"", null, "+term +\"phrase phrase\"");
			AssertQueryEquals("\"hello there\"", null, "\"hello there\"");
			Assert.IsTrue(GetQuery("a AND b", null) is BooleanQuery);
			Assert.IsTrue(GetQuery("hello", null) is TermQuery);
			Assert.IsTrue(GetQuery("\"hello there\"", null) is PhraseQuery);
			
			AssertQueryEquals("germ term^2.0", null, "germ term^2.0");
			AssertQueryEquals("(term)^2.0", null, "term^2.0");
			AssertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
			AssertQueryEquals("term^2.0", null, "term^2.0");
			AssertQueryEquals("term^2", null, "term^2.0");
			AssertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
			AssertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");
			
			AssertQueryEquals("(foo OR bar) AND (baz OR boo)", null, "+(foo bar) +(baz boo)");
			AssertQueryEquals("((a OR b) AND NOT c) OR d", null, "(+(a b) -c) d");
			AssertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null, "+(apple \"steve jobs\") -(foo bar baz)");
			AssertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null, "+(title:dog title:cat) -author:\"bob dole\"");
			
			QueryParser qp = new QueryParser("field", new StandardAnalyzer());
			// make sure OR is the default:
			Assert.AreEqual(QueryParser.OR_OPERATOR, qp.GetDefaultOperator());
			qp.SetDefaultOperator(QueryParser.AND_OPERATOR);
			Assert.AreEqual(QueryParser.AND_OPERATOR, qp.GetDefaultOperator());
			qp.SetDefaultOperator(QueryParser.OR_OPERATOR);
			Assert.AreEqual(QueryParser.OR_OPERATOR, qp.GetDefaultOperator());
		}
 private QueryParser BuildQueryParser()
 {
     var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Name", _analyzer);
     parser.SetDefaultOperator(QueryParser.Operator.AND);
     return parser;
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Indexer"/> class.
        /// </summary>
        /// <param name="path">The path to the .xml.bz2 dump of wikipedia</param>
        public Indexer(string path)
        {
            filePath = path;

            indexPath = Path.ChangeExtension(path, ".idx");

            if (Directory.Exists(indexPath) &&
                IndexReader.IndexExists(indexPath))
            {
                indexExists = true;
            }

            if (indexExists)
            {
                searcher = new IndexSearcher(indexPath);
            }

            textAnalyzer = GuessAnalyzer(filePath);

            queryParser = new QueryParser("title", textAnalyzer);

            queryParser.SetDefaultOperator(QueryParser.Operator.AND);

            abortIndexing = false;

            multithreadedIndexing = (Environment.ProcessorCount > 1);
        }
        public ActionResult SubmitSearchSelections(Models.Search model)
        {
            // Use this style query for products - all of the terms entered must match.
            QueryParser parser = new QueryParser(
                    Lucene.Net.Util.Version.LUCENE_29,
                    "title",
                    new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                );

            // This ensures all words must match in the phrase
            parser.SetDefaultOperator(QueryParser.Operator.AND);

            // This ensures similar words will match
            //parser.SetPhraseSlop(3);

            // Sets the current culture
            parser.SetLocale(System.Threading.Thread.CurrentThread.CurrentCulture);
            Query query = parser.Parse(model.Phrase);

            // Use query.Combine to merge this query with individual facets

            BrowseResult result = this.PerformSearch(query, this.IndexDirectory, model.SelectionGroups);

            //// Build results for display
            //int totalHits = result.NumHits;
            //BrowseHit[] hits = result.Hits;

            //model.Hits = result.Hits;
            //model.FacetMap = result.FacetMap;
            ////model.TotalHitCount = totalHits;

            PopulateModelResult(model, result);

            return Json(model, "application/json");

            //return View("Index", new Models.Search());
        }
Ejemplo n.º 22
0
		public virtual void  TestMultiAnalyzer_Rename()
		{
			
			QueryParser qp = new QueryParser("", new MultiAnalyzer(this));
			
			// trivial, no multiple tokens:
			Assert.AreEqual("foo", qp.Parse("foo").ToString());
			Assert.AreEqual("foo", qp.Parse("\"foo\"").ToString());
			Assert.AreEqual("foo foobar", qp.Parse("foo foobar").ToString());
			Assert.AreEqual("\"foo foobar\"", qp.Parse("\"foo foobar\"").ToString());
			Assert.AreEqual("\"foo foobar blah\"", qp.Parse("\"foo foobar blah\"").ToString());
			
			// two tokens at the same position:
			Assert.AreEqual("(multi multi2) foo", qp.Parse("multi foo").ToString());
			Assert.AreEqual("foo (multi multi2)", qp.Parse("foo multi").ToString());
			Assert.AreEqual("(multi multi2) (multi multi2)", qp.Parse("multi multi").ToString());
			Assert.AreEqual("+(foo (multi multi2)) +(bar (multi multi2))", qp.Parse("+(foo multi) +(bar multi)").ToString());
			Assert.AreEqual("+(foo (multi multi2)) field:\"bar (multi multi2)\"", qp.Parse("+(foo multi) field:\"bar multi\"").ToString());
			
			// phrases:
			Assert.AreEqual("\"(multi multi2) foo\"", qp.Parse("\"multi foo\"").ToString());
			Assert.AreEqual("\"foo (multi multi2)\"", qp.Parse("\"foo multi\"").ToString());
			Assert.AreEqual("\"foo (multi multi2) foobar (multi multi2)\"", qp.Parse("\"foo multi foobar multi\"").ToString());
			
			// fields:
			Assert.AreEqual("(field:multi field:multi2) field:foo", qp.Parse("field:multi field:foo").ToString());
			Assert.AreEqual("field:\"(multi multi2) foo\"", qp.Parse("field:\"multi foo\"").ToString());
			
			// three tokens at one position:
			Assert.AreEqual("triplemulti multi3 multi2", qp.Parse("triplemulti").ToString());
			Assert.AreEqual("foo (triplemulti multi3 multi2) foobar", qp.Parse("foo triplemulti foobar").ToString());
			
			// phrase with non-default slop:
			Assert.AreEqual("\"(multi multi2) foo\"~10", qp.Parse("\"multi foo\"~10").ToString());
			
			// phrase with non-default boost:
			Assert.AreEqual("\"(multi multi2) foo\"^2.0", qp.Parse("\"multi foo\"^2").ToString());
			
			// phrase after changing default slop
			qp.SetPhraseSlop(99);
			Assert.AreEqual("\"(multi multi2) foo\"~99 bar", qp.Parse("\"multi foo\" bar").ToString());
			Assert.AreEqual("\"(multi multi2) foo\"~99 \"foo bar\"~2", qp.Parse("\"multi foo\" \"foo bar\"~2").ToString());
			qp.SetPhraseSlop(0);
			
			// non-default operator:
			qp.SetDefaultOperator(QueryParser.AND_OPERATOR);
			Assert.AreEqual("+(multi multi2) +foo", qp.Parse("multi foo").ToString());
		}
Ejemplo n.º 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Indexer"/> class.
        /// </summary>
        /// <param name="path">The path to the .xml.bz2 dump of wikipedia</param>
        public Indexer(string path)
        {
            filePath = path;

            indexPath = Path.ChangeExtension(path, ".idx");
            Lucene.Net.Store.Directory idxDir = FSDirectory.Open(new DirectoryInfo(indexPath));

            if (Directory.Exists(indexPath) &&
                IndexReader.IndexExists(idxDir))
            {
                indexExists = true;
            }

            if (indexExists)
            {
                searcher = new IndexSearcher(idxDir, true);
            }

            textAnalyzer = GuessAnalyzer(filePath, out _IsRTL);

            queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", textAnalyzer);

            queryParser.SetDefaultOperator(QueryParser.Operator.AND);

            abortIndexing = false;

            multithreadedIndexing = (Environment.ProcessorCount > 1);
        }