public QueryTermVector(System.String queryString, Analyzer analyzer)
		{
			if (analyzer != null)
			{
				TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
				if (stream != null)
				{
					System.Collections.ArrayList terms = new System.Collections.ArrayList();
					try
					{
						bool hasMoreTokens = false;
						
						stream.Reset();
						TermAttribute termAtt = (TermAttribute) stream.AddAttribute(typeof(TermAttribute));
						
						hasMoreTokens = stream.IncrementToken();
						while (hasMoreTokens)
						{
							terms.Add(termAtt.Term());
							hasMoreTokens = stream.IncrementToken();
						}
						ProcessTerms((System.String[]) terms.ToArray(typeof(System.String)));
					}
					catch (System.IO.IOException e)
					{
					}
				}
			}
		}
 public QueryTermVector(System.String queryString, Analyzer analyzer)
 {
     if (analyzer != null)
     {
         TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
         if (stream != null)
         {
             IList<string> terms = new List<string>();
             try
             {
                 bool hasMoreTokens = false;
                 
                 stream.Reset();
                 ITermAttribute termAtt = stream.AddAttribute<ITermAttribute>();
                 
                 hasMoreTokens = stream.IncrementToken();
                 while (hasMoreTokens)
                 {
                     terms.Add(termAtt.Term);
                     hasMoreTokens = stream.IncrementToken();
                 }
                 ProcessTerms(terms.ToArray());
             }
             catch (System.IO.IOException)
             {
             }
         }
     }
 }
		public override void  TearDown()
		{
			base.TearDown();
			base.TearDown();
			dir = null;
			anlzr = null;
		}
Exemple #4
0
		/// <summary>This ctor used by test code only.
		/// 
		/// </summary>
		/// <param name="directory">The directory to write the document information to
		/// </param>
		/// <param name="analyzer">The analyzer to use for the document
		/// </param>
		/// <param name="similarity">The Similarity function
		/// </param>
		/// <param name="maxFieldLength">The maximum number of tokens a field may have
		/// </param>
		public DocumentWriter(Directory directory, Analyzer analyzer, Similarity similarity, int maxFieldLength)
		{
			InitBlock();
			this.directory = directory;
			this.analyzer = analyzer;
			this.similarity = similarity;
			this.maxFieldLength = maxFieldLength;
		}
Exemple #5
0
		public DocumentWriter(Directory directory, Analyzer analyzer, IndexWriter writer)
		{
			InitBlock();
			this.directory = directory;
			this.analyzer = analyzer;
			this.similarity = writer.GetSimilarity();
			this.maxFieldLength = writer.GetMaxFieldLength();
			this.termIndexInterval = writer.GetTermIndexInterval();
		}
 private void CountHits(Analyzer analyzer, string[] docs, Query q, int expected)
 {
     Directory d = GetDirectory(analyzer, docs);
     IndexReader r = DirectoryReader.Open(d);
     IndexSearcher s = new IndexSearcher(r);
     TotalHitCountCollector c = new TotalHitCountCollector();
     s.Search(q, c);
     Assert.AreEqual(expected, c.TotalHits, q.ToString());
     r.Dispose();
     d.Dispose();
 }
Exemple #7
0
        /// <summary>
        /// ��ǰ����ִ���
        /// </summary>
        public static void Init()
        {
            /// <summary>
            /// �ִ���
            /// </summary>
             OneAnalyzer = new Lucene.Net.Analysis.XunLongX.XunLongAnalyzer();

               /// <summary>
               /// ������
               /// </summary>
            mSearch = new ClassSearch();
        }
        public static Directory GetDirectory(Analyzer analyzer, string[] vals)
        {
            Directory directory = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).SetMaxBufferedDocs(TestUtil.NextInt(Random(), 100, 1000)).SetMergePolicy(NewLogMergePolicy()));

            foreach (string s in vals)
            {
                Document d = new Document();
                d.Add(NewTextField(FIELD, s, Field.Store.YES));
                writer.AddDocument(d);
            }
            writer.Dispose();
            return directory;
        }
 public override void SetUp()
 {
     base.SetUp();
     Analyzer = new MockAnalyzer(Random());
     Dir = NewDirectory();
     IndexWriterConfig config = NewIndexWriterConfig(TEST_VERSION_CURRENT, Analyzer);
     config.SetMergePolicy(NewLogMergePolicy()); // we will use docids to validate
     RandomIndexWriter writer = new RandomIndexWriter(Random(), Dir, config);
     writer.AddDocument(Doc("lucene", "lucene is a very popular search engine library"));
     writer.AddDocument(Doc("solr", "solr is a very popular search server and is using lucene"));
     writer.AddDocument(Doc("nutch", "nutch is an internet search engine with web crawler and is using lucene and hadoop"));
     Reader = writer.Reader;
     writer.Dispose();
     Searcher = NewSearcher(Reader);
 }
        /// <summary>
        /// Cstor
        /// </summary>
        /// <param name="queryType">The type of query the factory should return</param>
        public QueryFactory(QueryType queryType)
        {
            this.queryType = queryType;

            //create the base fields to search against
            baseFieldName = new List<string>();
            baseFieldName.Add("title");
            baseFieldName.Add("description");
            baseFieldName.Add("tags");

            //create the base boost values
            baseFieldBoost = new List<float>();
            baseFieldBoost.Add(8f);
            baseFieldBoost.Add(4f);
            baseFieldBoost.Add(1f);

            analyzer = new DnkAnalyzer();
        }
Exemple #11
0
        /// <summary> A convenience method that tries a number of approaches to getting a token stream.
        /// The cost of finding there are no termVectors in the index is minimal (1000 invocations still 
        /// registers 0 ms). So this "lazy" (flexible?) approach to coding is probably acceptable
        /// </summary>
        /// <param name="">reader</param>
        /// <param name="">docId</param>
        /// <param name="">field</param>
        /// <param name="">analyzer</param>
        /// <returns> null if field not stored correctly 
        /// </returns>
        /// <throws>  IOException </throws>
        public static TokenStream GetAnyTokenStream(IndexReader reader, int docId, string field, Analyzer analyzer)
        {
            TokenStream ts = null;

            TermFreqVector tfv = (TermFreqVector) reader.GetTermFreqVector(docId, field);
            if (tfv != null)
            {
                if (tfv is TermPositionVector)
                {
                    ts = GetTokenStream((TermPositionVector) tfv);
                }
            }
            //No token info stored so fall back to analyzing raw content
            if (ts == null)
            {
                ts = GetTokenStream(reader, docId, field, analyzer);
            }
            return ts;
        }
Exemple #12
0
		public QueryTermVector(System.String queryString, Analyzer analyzer)
		{
			if (analyzer != null)
			{
				TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
				if (stream != null)
				{
					Token next = null;
					System.Collections.ArrayList terms = new System.Collections.ArrayList();
					try
					{
						while ((next = stream.Next()) != null)
						{
							terms.Add(next.TermText());
						}
						ProcessTerms((System.String[]) terms.ToArray(typeof(System.String)));
					}
					catch (System.IO.IOException)
					{
					}
				}
			}
		}
 public QueryTermVector(System.String queryString, Analyzer analyzer)
 {
     if (analyzer != null)
     {
         TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
         if (stream != null)
         {
             System.Collections.ArrayList terms = new System.Collections.ArrayList();
             try
             {
                 Token reusableToken = new Token();
                 for (Token nextToken = stream.Next(reusableToken); nextToken != null; nextToken = stream.Next(reusableToken))
                 {
                     terms.Add(nextToken.Term());
                 }
                 ProcessTerms((System.String[]) terms.ToArray(typeof(System.String)));
             }
             catch (System.IO.IOException)
             {
             }
         }
     }
 }
		public override void  SetUp()
		{
			base.SetUp();
			// prepare a small index with just a few documents.  
			dir = new RAMDirectory();
			anlzr = new StandardAnalyzer();
			IndexWriter iw = new IndexWriter(dir, anlzr);
			// add docs not exactly in natural ID order, to verify we do check the order of docs by scores
			int remaining = N_DOCS;
			bool[] done = new bool[N_DOCS];
			int i = 0;
			while (remaining > 0)
			{
				if (done[i])
				{
					throw new System.Exception("to set this test correctly N_DOCS=" + N_DOCS + " must be primary and greater than 2!");
				}
				AddDoc(iw, i);
				done[i] = true;
				i = (i + 4) % N_DOCS;
				remaining--;
			}
			iw.Close();
		}
Exemple #15
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);
        }
 /// <summary> Sets the analyzer to use. An analyzer is not required for generating a query with the
 /// {@link #Like(int)} method, all other 'like' methods require an analyzer.
 /// 
 /// </summary>
 /// <param name="analyzer">the analyzer to use to tokenize text.
 /// </param>
 public void  SetAnalyzer(Analyzer analyzer)
 {
     this.analyzer = analyzer;
 }
 /// <summary>
 /// create a new index writer config with random defaults </summary>
 public static IndexWriterConfig NewIndexWriterConfig(LuceneVersion v, Analyzer a)
 {
     return NewIndexWriterConfig(Random(), v, a);
 }
Exemple #18
0
		/// <summary> Constructs a query parser.
		/// 
		/// </summary>
		/// <param name="matchVersion">Lucene version to match. See <a href="#version">above</a>)
		/// </param>
		/// <param name="f">the default field for query terms.
		/// </param>
		/// <param name="a">used to find terms in the query text.
		/// </param>
		public QueryParser(Version matchVersion, System.String f, Analyzer a):this(new FastCharStream(new System.IO.StringReader("")))
		{
			analyzer = a;
			field = f;
			if (matchVersion.OnOrAfter(Version.LUCENE_29))
			{
				enablePositionIncrements = true;
			}
			else
			{
				enablePositionIncrements = false;
			}
		}
Exemple #19
0
 public void updateDocument(Term term, Document doc, Analyzer a)
 {
     threadPool.QueueWorkItem(new Amib.Threading.Action <Document, Term, Analyzer>(RunJob), doc, term, a);
 }
 /// <summary>
 /// Calls {@link
 ///  IndexWriter#addDocuments(Iterable,Analyzer)} and
 ///  returns the generation that reflects this change.
 /// </summary>
 public virtual long AddDocuments(IEnumerable <IEnumerable <IndexableField> > docs, Analyzer a)
 {
     Writer.AddDocuments(docs, a);
     // Return gen as of when indexing finished:
     return(IndexingGen.Get());
 }
 public override void SetUp()
 {
     base.SetUp();
     Analyzer = new MockAnalyzer(Random());
 }
Exemple #22
0
 public static Query Parse(System.String[] queries, System.String[] fields, Analyzer analyzer)
 {
     return(Parse(Version.LUCENE_24, queries, fields, analyzer));
 }
Exemple #23
0
        /// <summary> Parses a query which searches on the fields specified.
        /// <p/>
        /// If x fields are specified, this effectively constructs:
        ///
        /// <pre>
        /// &lt;code&gt;
        /// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
        /// &lt;/code&gt;
        /// </pre>
        ///
        /// </summary>
        /// <param name="matchVersion">Lucene version to match; this is passed through to
        /// QueryParser.
        /// </param>
        /// <param name="queries">Queries strings to parse
        /// </param>
        /// <param name="fields">Fields to search on
        /// </param>
        /// <param name="analyzer">Analyzer to use
        /// </param>
        /// <throws>  ParseException </throws>
        /// <summary>             if query parsing fails
        /// </summary>
        /// <throws>  IllegalArgumentException </throws>
        /// <summary>             if the length of the queries array differs from the length of
        /// the fields array
        /// </summary>
        public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, Analyzer analyzer)
        {
            if (queries.Length != fields.Length)
            {
                throw new System.ArgumentException("queries.length != fields.length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            for (int i = 0; i < fields.Length; i++)
            {
                QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
                Query       q  = qp.Parse(queries[i]);
                if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
                {
                    bQuery.Add(q, BooleanClause.Occur.SHOULD);
                }
            }
            return(bQuery);
        }
Exemple #24
0
 public MultiFieldQueryParser(System.String[] fields, Analyzer analyzer) : this(Version.LUCENE_24, fields, analyzer)
 {
 }
Exemple #25
0
 /// <summary> Creates a MultiFieldQueryParser.
 ///
 /// <p/>
 /// It will, when parse(String query) is called, construct a query like this
 /// (assuming the query consists of two terms and you specify the two fields
 /// <code>title</code> and <code>body</code>):
 /// <p/>
 ///
 /// <code>
 /// (title:term1 body:term1) (title:term2 body:term2)
 /// </code>
 ///
 /// <p/>
 /// When setDefaultOperator(AND_OPERATOR) is set, the result will be:
 /// <p/>
 ///
 /// <code>
 /// +(title:term1 body:term1) +(title:term2 body:term2)
 /// </code>
 ///
 /// <p/>
 /// In other words, all the query's terms must appear, but it doesn't matter
 /// in what fields they appear.
 /// <p/>
 /// </summary>
 public MultiFieldQueryParser(Version matchVersion, System.String[] fields, Analyzer analyzer) : base(matchVersion, null, analyzer)
 {
     this.fields = fields;
 }
Exemple #26
0
 /// <summary> Creates a MultiFieldQueryParser. Allows passing of a map with term to
 /// Boost, and the boost to apply to each term.
 ///
 /// <p/>
 /// It will, when parse(String query) is called, construct a query like this
 /// (assuming the query consists of two terms and you specify the two fields
 /// <code>title</code> and <code>body</code>):
 /// <p/>
 ///
 /// <code>
 /// (title:term1 body:term1) (title:term2 body:term2)
 /// </code>
 ///
 /// <p/>
 /// When setDefaultOperator(AND_OPERATOR) is set, the result will be:
 /// <p/>
 ///
 /// <code>
 /// +(title:term1 body:term1) +(title:term2 body:term2)
 /// </code>
 ///
 /// <p/>
 /// When you pass a boost (title=>5 body=>10) you can get
 /// <p/>
 ///
 /// <code>
 /// +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
 /// </code>
 ///
 /// <p/>
 /// In other words, all the query's terms must appear, but it doesn't matter
 /// in what fields they appear.
 /// <p/>
 /// </summary>
 public MultiFieldQueryParser(Version matchVersion, System.String[] fields, Analyzer analyzer, System.Collections.IDictionary boosts) : this(matchVersion, fields, analyzer)
 {
     this.boosts = boosts;
 }
 /// <summary> Creates a MultiFieldQueryParser.
 ///
 /// <p>It will, when parse(String query)
 /// is called, construct a query like this (assuming the query consists of
 /// two terms and you specify the two fields <code>title</code> and <code>body</code>):</p>
 ///
 /// <code>
 /// (title:term1 body:term1) (title:term2 body:term2)
 /// </code>
 ///
 /// <p>When setDefaultOperator(AND_OPERATOR) is set, the result will be:</p>
 ///
 /// <code>
 /// +(title:term1 body:term1) +(title:term2 body:term2)
 /// </code>
 ///
 /// <p>In other words, all the query's terms must appear, but it doesn't matter in
 /// what fields they appear.</p>
 /// </summary>
 public MultiFieldQueryParser(System.String[] fields, Analyzer analyzer) : base(null, analyzer)
 {
     this.fields = fields;
 }
        /// <summary> Parses a query, searching on the fields specified.
        /// Use this if you need to specify certain fields as required,
        /// and others as prohibited.
        /// <p><pre>
        /// Usage:
        /// <code>
        /// String[] query = {"query1", "query2", "query3"};
        /// String[] fields = {"filename", "contents", "description"};
        /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
        /// BooleanClause.Occur.MUST,
        /// BooleanClause.Occur.MUST_NOT};
        /// MultiFieldQueryParser.parse(query, fields, flags, analyzer);
        /// </code>
        /// </pre>
        /// <p>
        /// The code above would construct a query:
        /// <pre>
        /// <code>
        /// (filename:query1) +(contents:query2) -(description:query3)
        /// </code>
        /// </pre>
        ///
        /// </summary>
        /// <param name="queries">Queries string to parse
        /// </param>
        /// <param name="fields">Fields to search on
        /// </param>
        /// <param name="flags">Flags describing the fields
        /// </param>
        /// <param name="analyzer">Analyzer to use
        /// </param>
        /// <throws>  ParseException if query parsing fails </throws>
        /// <throws>  IllegalArgumentException if the length of the queries, fields, </throws>
        /// <summary>  and flags array differ
        /// </summary>
        public static Query Parse(System.String[] queries, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
        {
            if (!(queries.Length == fields.Length && queries.Length == flags.Length))
            {
                throw new System.ArgumentException("queries, fields, and flags array have have different length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            for (int i = 0; i < fields.Length; i++)
            {
                QueryParser qp = new QueryParser(fields[i], analyzer);
                Query       q  = qp.Parse(queries[i]);
                if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
                {
                    bQuery.Add(q, flags[i]);
                }
            }
            return(bQuery);
        }
		/// <summary> Parses a query, searching on the fields specified.
		/// Use this if you need to specify certain fields as required,
		/// and others as prohibited.
		/// <p><pre>
		/// Usage:
		/// <code>
		/// String[] query = {"query1", "query2", "query3"};
		/// String[] fields = {"filename", "contents", "description"};
		/// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
		/// BooleanClause.Occur.MUST,
		/// BooleanClause.Occur.MUST_NOT};
		/// MultiFieldQueryParser.parse(query, fields, flags, analyzer);
		/// </code>
		/// </pre>
		/// <p>
		/// The code above would construct a query:
		/// <pre>
		/// <code>
		/// (filename:query1) +(contents:query2) -(description:query3)
		/// </code>
		/// </pre>
		/// 
		/// </summary>
		/// <param name="queries">Queries string to parse
		/// </param>
		/// <param name="fields">Fields to search on
		/// </param>
		/// <param name="flags">Flags describing the fields
		/// </param>
		/// <param name="analyzer">Analyzer to use
		/// </param>
		/// <throws>  ParseException if query parsing fails </throws>
		/// <throws>  TokenMgrError if query parsing fails </throws>
		/// <throws>  IllegalArgumentException if the length of the queries, fields, </throws>
		/// <summary>  and flags array differ
		/// </summary>
		public static Query Parse(System.String[] queries, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
		{
			if (!(queries.Length == fields.Length && queries.Length == flags.Length))
				throw new System.ArgumentException("queries, fields, and flags array have have different length");
			BooleanQuery bQuery = new BooleanQuery();
			for (int i = 0; i < fields.Length; i++)
			{
				QueryParser qp = new QueryParser(fields[i], analyzer);
				Query q = qp.Parse(queries[i]);
				bQuery.Add(q, flags[i]);
			}
			return bQuery;
		}
 public override void  SetUp()
 {
     base.SetUp();
     similarityOne = new SimilarityOne(this);
     anlzr         = new StandardAnalyzer(Util.Version.LUCENE_CURRENT);
 }
Exemple #31
0
 /// <summary> Sets the analyzer to use. An analyzer is not required for generating a query with the
 /// {@link #Like(int)} method, all other 'like' methods require an analyzer.
 ///
 /// </summary>
 /// <param name="analyzer">the analyzer to use to tokenize text.
 /// </param>
 public void SetAnalyzer(Analyzer analyzer)
 {
     this.analyzer = analyzer;
 }
 /// <summary>
 /// Calls {@link
 ///  IndexWriter#updateDocuments(Term,Iterable,Analyzer)}
 ///  and returns the generation that reflects this change.
 /// </summary>
 public virtual long UpdateDocuments(Term t, IEnumerable <IEnumerable <IndexableField> > docs, Analyzer a)
 {
     Writer.UpdateDocuments(t, docs, a);
     // Return gen as of when indexing finished:
     return(IndexingGen.Get());
 }
		public override void  SetUp()
		{
			base.SetUp();
			similarityOne = new SimilarityOne(this);
			anlzr = new StandardAnalyzer();
		}
Exemple #34
0
 public SearchManager()
 {
     _directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(_indexDirectoryPath));
     _analyzer  = new Lucene.Net.Analysis.Standard.StandardAnalyzer(luceneVersion);
 }
Exemple #35
0
		public QueryParser(System.String f, Analyzer a):this(Version.LUCENE_24, f, a)
		{
		}
 public override void  TearDown()
 {
     base.TearDown();
     dir   = null;
     anlzr = null;
 }
			public DumbQueryParser(System.String f, Analyzer a):base(f, a)
			{
			}
		/// <deprecated> use {@link #GetFieldQuery(String, String)}
		/// </deprecated>
		protected internal override Query GetFieldQuery(System.String field, Analyzer analyzer, System.String queryText)
		{
			return GetFieldQuery(field, queryText);
		}
Exemple #39
0
 public void addDocument(Document doc, Analyzer a)
 {
     threadPool.QueueWorkItem(new Amib.Threading.Action <Document, Term, Analyzer>(RunJob), doc, null, a);
 }
		/// <deprecated> use {@link #MultiFieldQueryParser(String[], Analyzer)} instead
		/// </deprecated>
		public MultiFieldQueryParser(System.String f, Analyzer a):base(f, a)
		{
		}
Exemple #41
0
 private void RunJob(Document doc, Term delTerm, Analyzer analyzer)
 {
     base.UpdateDocument(delTerm, doc, analyzer);
 }
		/// <summary> Parses a query, searching on the fields specified.
		/// Use this if you need to specify certain fields as required,
		/// and others as prohibited.
		/// <p><pre>
		/// Usage:
		/// <code>
		/// String[] fields = {"filename", "contents", "description"};
		/// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
		/// BooleanClause.Occur.MUST,
		/// BooleanClause.Occur.MUST_NOT};
		/// MultiFieldQueryParser.parse("query", fields, flags, analyzer);
		/// </code>
		/// </pre>
		/// <p>
		/// The code above would construct a query:
		/// <pre>
		/// <code>
		/// (filename:query) +(contents:query) -(description:query)
		/// </code>
		/// </pre>
		/// 
		/// </summary>
		/// <param name="query">Query string to parse
		/// </param>
		/// <param name="fields">Fields to search on
		/// </param>
		/// <param name="flags">Flags describing the fields
		/// </param>
		/// <param name="analyzer">Analyzer to use
		/// </param>
		/// <throws>  ParseException if query parsing fails </throws>
		/// <throws>  TokenMgrError if query parsing fails </throws>
		/// <throws>  IllegalArgumentException if the length of the fields array differs </throws>
		/// <summary>  from the length of the flags array
		/// </summary>
		public static Query Parse(System.String query, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
		{
			if (fields.Length != flags.Length)
				throw new System.ArgumentException("fields.length != flags.length");
			BooleanQuery bQuery = new BooleanQuery();
			for (int i = 0; i < fields.Length; i++)
			{
				QueryParser qp = new QueryParser(fields[i], analyzer);
				Query q = qp.Parse(query);
				bQuery.Add(q, flags[i]);
			}
			return bQuery;
		}
        /// <summary>
        /// create a new index writer config with random defaults using the specified random </summary>
        public static IndexWriterConfig NewIndexWriterConfig(Random r, LuceneVersion v, Analyzer a)
        {
            IndexWriterConfig c = new IndexWriterConfig(v, a);
            c.SetSimilarity(ClassEnvRule.Similarity);
            if (VERBOSE)
            {
                // Even though TestRuleSetupAndRestoreClassEnv calls
                // InfoStream.setDefault, we do it again here so that
                // the PrintStreamInfoStream.messageID increments so
                // that when there are separate instances of
                // IndexWriter created we see "IW 0", "IW 1", "IW 2",
                // ... instead of just always "IW 0":
                c.InfoStream = new TestRuleSetupAndRestoreClassEnv.ThreadNameFixingPrintStreamInfoStream(Console.Out);
            }

            if (r.NextBoolean())
            {
                c.SetMergeScheduler(new SerialMergeScheduler());
            }
            else if (Rarely(r))
            {
                int maxThreadCount = TestUtil.NextInt(Random(), 1, 4);
                int maxMergeCount = TestUtil.NextInt(Random(), maxThreadCount, maxThreadCount + 4);
                ConcurrentMergeScheduler cms = new ConcurrentMergeScheduler();
                cms.SetMaxMergesAndThreads(maxMergeCount, maxThreadCount);
                c.SetMergeScheduler(cms);
            }
            if (r.NextBoolean())
            {
                if (Rarely(r))
                {
                    // crazy value
                    c.SetMaxBufferedDocs(TestUtil.NextInt(r, 2, 15));
                }
                else
                {
                    // reasonable value
                    c.SetMaxBufferedDocs(TestUtil.NextInt(r, 16, 1000));
                }
            }
            if (r.NextBoolean())
            {
                if (Rarely(r))
                {
                    // crazy value
                    c.SetTermIndexInterval(r.NextBoolean() ? TestUtil.NextInt(r, 1, 31) : TestUtil.NextInt(r, 129, 1000));
                }
                else
                {
                    // reasonable value
                    c.SetTermIndexInterval(TestUtil.NextInt(r, 32, 128));
                }
            }
            if (r.NextBoolean())
            {
                int maxNumThreadStates = Rarely(r) ? TestUtil.NextInt(r, 5, 20) : TestUtil.NextInt(r, 1, 4); // reasonable value -  crazy value

                if (Rarely(r))
                {
                    // Retrieve the package-private setIndexerThreadPool
                    // method:
                    MethodInfo setIndexerThreadPoolMethod = typeof(IndexWriterConfig).GetMethod("SetIndexerThreadPool", new Type[] { typeof(DocumentsWriterPerThreadPool) });
                    //setIndexerThreadPoolMethod.setAccessible(true);
                    Type clazz = typeof(RandomDocumentsWriterPerThreadPool);
                    ConstructorInfo ctor = clazz.GetConstructor(new[] { typeof(int), typeof(Random) });
                    //ctor.Accessible = true;
                    // random thread pool
                    setIndexerThreadPoolMethod.Invoke(c, new[] { ctor.Invoke(new object[] { maxNumThreadStates, r }) });
                }
                else
                {
                    // random thread pool
                    c.SetMaxThreadStates(maxNumThreadStates);
                }
            }

            c.SetMergePolicy(NewMergePolicy(r));

            if (Rarely(r))
            {
                c.SetMergedSegmentWarmer(new SimpleMergedSegmentWarmer(c.InfoStream));
            }
            c.SetUseCompoundFile(r.NextBoolean());
            c.SetReaderPooling(r.NextBoolean());
            c.SetReaderTermsIndexDivisor(TestUtil.NextInt(r, 1, 4));
            c.SetCheckIntegrityAtMerge(r.NextBoolean());
            return c;
        }
Exemple #44
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);
        }
		/// <throws>  ParseException </throws>
		/// <deprecated> use {@link #GetRangeQuery(String, String, String, boolean)}
		/// </deprecated>
		protected internal override Query GetRangeQuery(System.String field, Analyzer analyzer, System.String part1, System.String part2, bool inclusive)
		{
			return GetRangeQuery(field, part1, part2, inclusive);
		}
Exemple #46
0
        /// <summary> A convenience method that tries a number of approaches to getting a token stream.
        /// The cost of finding there are no termVectors in the index is minimal (1000 invocations still
        /// registers 0 ms). So this "lazy" (flexible?) approach to coding is probably acceptable
        /// </summary>
        /// <param name="">reader
        /// </param>
        /// <param name="">docId
        /// </param>
        /// <param name="">field
        /// </param>
        /// <param name="">analyzer
        /// </param>
        /// <returns> null if field not stored correctly
        /// </returns>
        /// <throws>  IOException </throws>
        public static TokenStream GetAnyTokenStream(IndexReader reader, int docId, System.String field, Analyzer analyzer)
        {
            TokenStream ts = null;

            TermFreqVector tfv = (TermFreqVector)reader.GetTermFreqVector(docId, field);

            if (tfv != null)
            {
                if (tfv is TermPositionVector)
                {
                    ts = GetTokenStream((TermPositionVector)tfv);
                }
            }
            //No token info stored so fall back to analyzing raw content
            if (ts == null)
            {
                ts = GetTokenStream(reader, docId, field, analyzer);
            }
            return(ts);
        }
		/// <summary> Parses a query which searches on the fields specified.
		/// If x fields are specified, this effectively constructs:
		/// 
		/// <code>
		/// (field1:query) (field2:query) (field3:query)...(fieldx:query)
		/// </code>
		/// 
		/// </summary>
		/// <param name="query">Query string to parse
		/// </param>
		/// <param name="fields">Fields to search on
		/// </param>
		/// <param name="analyzer">Analyzer to use
		/// </param>
		/// <throws>  ParseException if query parsing fails </throws>
		/// <throws>  TokenMgrError if query parsing fails </throws>
		/// <deprecated> use {@link #Parse(String)} instead but note that it
		/// returns a different query for queries where all terms are required:
		/// its query excepts all terms, no matter in what field they occur whereas
		/// the query built by this (deprecated) method expected all terms in all fields 
		/// at the same time.
		/// </deprecated>
		public static Query Parse(System.String query, System.String[] fields, Analyzer analyzer)
		{
			BooleanQuery bQuery = new BooleanQuery();
			for (int i = 0; i < fields.Length; i++)
			{
				Query q = Parse(query, fields[i], analyzer);
				bQuery.Add(q, BooleanClause.Occur.SHOULD);
			}
			return bQuery;
		}
Exemple #48
0
        //convenience method
        public static TokenStream GetTokenStream(IndexReader reader, int docId, System.String field, Analyzer analyzer)
        {
            Document doc = reader.Document(docId);

            System.String contents = doc.Get(field);
            if (contents == null)
            {
                throw new System.ArgumentException("Field " + field + " in document #" + docId + " is not stored and cannot be analyzed");
            }
            return(analyzer.TokenStream(field, new System.IO.StringReader(contents)));
        }
		/// <summary> Parses a query, searching on the fields specified. Use this if you need to
		/// specify certain fields as required, and others as prohibited.
		/// <p>
		/// <pre>
		/// Usage:
		/// <code>
		/// String[] fields = { &quot;filename&quot;, &quot;contents&quot;, &quot;description&quot; };
		/// int[] flags = { MultiFieldQueryParser.NORMAL_FIELD,
		/// MultiFieldQueryParser.REQUIRED_FIELD,
		/// MultiFieldQueryParser.PROHIBITED_FIELD, };
		/// parse(query, fields, flags, analyzer);
		/// </code>
		/// </pre>
		/// 
		/// <p>
		/// The code above would construct a query:
		/// <pre>
		/// <code>
		/// (filename:query1) +(contents:query2) -(description:query3)
		/// </code>
		/// </pre>
		/// 
		/// </summary>
		/// <param name="queries">Queries string to parse
		/// </param>
		/// <param name="fields">Fields to search on
		/// </param>
		/// <param name="flags">Flags describing the fields
		/// </param>
		/// <param name="analyzer">Analyzer to use
		/// </param>
		/// <throws>  ParseException if query parsing fails </throws>
		/// <throws>  TokenMgrError if query parsing fails </throws>
		/// <throws>  IllegalArgumentException if the length of the queries, fields, and flags array differ </throws>
		/// <deprecated> use {@link #Parse(String[], String[], BooleanClause.Occur[], Analyzer)} instead
		/// </deprecated>
		public static Query Parse(System.String[] queries, System.String[] fields, int[] flags, Analyzer analyzer)
		{
			if (!(queries.Length == fields.Length && queries.Length == flags.Length))
				throw new System.ArgumentException("queries, fields, and flags array have have different length");
			BooleanQuery bQuery = new BooleanQuery();
			for (int i = 0; i < fields.Length; i++)
			{
				QueryParser qp = new QueryParser(fields[i], analyzer);
				Query q = qp.Parse(queries[i]);
				int flag = flags[i];
				switch (flag)
				{
					
					case REQUIRED_FIELD: 
						bQuery.Add(q, BooleanClause.Occur.MUST);
						break;
					
					case PROHIBITED_FIELD: 
						bQuery.Add(q, BooleanClause.Occur.MUST_NOT);
						break;
					
					default: 
						bQuery.Add(q, BooleanClause.Occur.SHOULD);
						break;
					
				}
			}
			return bQuery;
		}
Exemple #50
0
 /// <summary> Open an index with write access.
 ///
 /// </summary>
 /// <param name="directory">the index directory
 /// </param>
 /// <param name="analyzer">the analyzer to use for adding new documents
 /// </param>
 /// <param name="create"><code>true</code> to create the index or overwrite the existing one;
 /// <code>false</code> to append to the existing index
 /// </param>
 /// <throws>  CorruptIndexException if the index is corrupt </throws>
 /// <throws>  LockObtainFailedException if another writer </throws>
 /// <summary>  has this index open (<code>write.lock</code> could not
 /// be obtained)
 /// </summary>
 /// <throws>  IOException if there is a low-level IO error </throws>
 public IndexModifier(Directory directory, Analyzer analyzer, bool create)
 {
     InitBlock();
     Init(directory, analyzer, create);
 }
		/// <summary> Creates a MultiFieldQueryParser.
		/// 
		/// <p>It will, when parse(String query)
		/// is called, construct a query like this (assuming the query consists of
		/// two terms and you specify the two fields <code>title</code> and <code>body</code>):</p>
		/// 
		/// <code>
		/// (title:term1 body:term1) (title:term2 body:term2)
		/// </code>
		/// 
		/// <p>When setDefaultOperator(AND_OPERATOR) is set, the result will be:</p>
		/// 
		/// <code>
		/// +(title:term1 body:term1) +(title:term2 body:term2)
		/// </code>
		/// 
		/// <p>In other words, all the query's terms must appear, but it doesn't matter in
		/// what fields they appear.</p>
		/// </summary>
		public MultiFieldQueryParser(System.String[] fields, Analyzer analyzer) : base(null, analyzer)
		{
			this.fields = fields;
		}
Exemple #52
0
 public MockIndexWriter(TestIndexWriterExceptions enclosingInstance, Directory dir, Analyzer a, bool create, MaxFieldLength mfl) : base(dir, a, create, mfl)
 {
     InitBlock(enclosingInstance);
 }