Example #1
0
        static public Query NewTokenQuery(string token)
        {
            Query query;

            query = new Query();

            QueryPart_Text part;

            part      = new QueryPart_Text();
            part.Text = token;
            query.AddPart(part);

            return(query);
        }
Example #2
0
        static private QueryPart StringToQueryPart(string text, bool is_prohibited)
        {
            QueryPart part;

            if (text.IndexOf('*') != -1)
            {
                part = new QueryPart_Wildcard();
                ((QueryPart_Wildcard)part).QueryString = text;
            }
            else
            {
                part = new QueryPart_Text();
                ((QueryPart_Text)part).Text = text;
            }

            part.Logic = (is_prohibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
            return(part);
        }
		///////// RDF fu ///////////////////////////////////////////////

		// Returns a collection of Uris
		// HitFilter and UriFilter are ignored for now
		// They will come into play in the final FetchDocument part
		// FIXME: Should RDFQuery do any query mapping using backend_query_part_hook ?
		// I think it should not. QueryPart hooks are for human beings, RDF is for softwares.
		public ICollection DoRDFQuery (Query _query, TextCache text_cache)
		{
			RDFQuery query = (RDFQuery) _query;

			string subject, predicate, _object;
			PropertyType pred_type;

			subject = query.SubjectString;
			predicate = query.Predicate;
			pred_type = query.PredicateType;
			_object = query.Object;

			if (Debug)
				Logger.Log.Debug ("###### {0}: Starting low-level queries '{1}' : '{4}:{2}' = '{3}'", IndexName, subject, predicate, _object, pred_type);

			// ******** 8 cases **********

			// Return all uris
			if (subject == String.Empty && predicate == String.Empty && _object == String.Empty) {
				ICollection hits = GetAllHitsByUri ().Values;
				foreach (Hit hit in hits)
					foreach (Property text_link_property in GetTextLinks (hit.Uri, text_cache))
						hit.AddProperty (text_link_property);
				return hits;
			}

			// Normal query
			if (subject == String.Empty && predicate == String.Empty && _object != String.Empty) {
				QueryPart_Text part = new QueryPart_Text ();
				part.Text = _object;
				part.SearchFullText = false; // We only search properties in RDF query
				query.AddPart (part);
				return DoLowLevelRDFQuery (query, pred_type, predicate, _object, text_cache);
			}

			// Return uris for all documents with this property
			if (subject == String.Empty && predicate != String.Empty && _object == String.Empty) {
				string field_name = PropertyToFieldName (pred_type, predicate);

				QueryPart_Property part = new QueryPart_Property ();
				part.Type = PropertyType.Internal;
				part.Key = "Properties";
				part.Value = field_name;
				query.AddPart (part);

				return DoLowLevelRDFQuery (query, pred_type, predicate, null, text_cache);
			}

			// Property query
			if (subject == String.Empty && predicate != String.Empty && _object != String.Empty) {
				QueryPart_Property part = new QueryPart_Property ();
				part.Type = pred_type;
				part.Key = predicate;
				part.Value = _object;
				query.AddPart (part);
				return DoLowLevelRDFQuery (query, pred_type, predicate, _object, text_cache);
			}

			// Return if the URI exists
			if (subject != String.Empty && predicate == String.Empty && _object == String.Empty) {
				QueryPart_Uri part = new QueryPart_Uri ();
				part.Uri = new Uri (subject, true); // better be URI!
				query.AddPart (part);
				// FIXME: Which properties to return in the hit? All or none ?
				return DoLowLevelRDFQuery (query, pred_type, predicate, null, text_cache);
			}

			// Normal query in the document with this URI
			if (subject != String.Empty && predicate == String.Empty && _object != String.Empty) {
				QueryPart_Uri uri_part = new QueryPart_Uri ();
				uri_part.Uri = new Uri (subject, true); // better be URI!
				query.AddPart (uri_part);

				QueryPart_Text part = new QueryPart_Text ();
				part.Text = _object;
				part.SearchFullText = false; // We only search properties in RDF query
				query.AddPart (part);

				return DoLowLevelRDFQuery (query, pred_type, predicate, _object, text_cache);
			}

			// Return URI if the document with this URI contains this property
			if (subject != String.Empty && predicate != String.Empty && _object == String.Empty) {
				ArrayList returned_uris = new ArrayList (1);

				ArrayList uri_list = new ArrayList (1);
				uri_list.Add (new Uri (subject, true));

				string field_name = PropertyToFieldName (pred_type, predicate);
				FieldSelector fields = new MapFieldSelector (new string[] { "Uri", "Timestamp", field_name });
				ICollection hits = GetHitsForUris (uri_list, fields);
				if (predicate == "TextLinks") {
					foreach (Hit hit in hits)
						foreach (Property text_link_property in GetTextLinks (hit.Uri, text_cache))
							hit.AddProperty (text_link_property);
				}

				return hits;
			}

			// Property query in the document with this URI
			if (subject != String.Empty && predicate != String.Empty && _object != String.Empty) {
				QueryPart_Uri uri_part = new QueryPart_Uri ();
				uri_part.Uri = new Uri (subject, true); // better be URI!
				query.AddPart (uri_part);

				QueryPart_Property part = new QueryPart_Property ();
				part.Type = pred_type;
				part.Key = predicate;
				part.Value = _object;
				query.AddPart (part);

				return DoLowLevelRDFQuery (query, pred_type, predicate, _object, text_cache);
			}

			throw new Exception ("Never reaches");
		}
Example #4
0
        static private Query NewRandomQuery(int length,
                                            bool allow_inexpensive,
                                            bool inside_an_or)
        {
            Query query;

            query = new Query();

            // One in four queries will contain some OR terms.
            if (!inside_an_or && random.Next(4) == 0)
            {
                int N = random.Next(3) + 1;
                for (int i = 0; i < N; ++i)
                {
                    QueryPart_Or part;
                    part = new QueryPart_Or();

                    int sub_length;
                    sub_length = random.Next(length) + 1;
                    if (sub_length < 2)
                    {
                        sub_length = 2;
                    }

                    // We generate a new query at random, and stuff its QueryParts
                    // into our Or QueryPart.
                    Query or_query;
                    or_query = NewRandomQuery(sub_length, allow_inexpensive, true);
                    foreach (QueryPart sub_part in or_query.Parts)
                    {
                        part.Add(sub_part);
                    }

                    query.AddPart(part);
                }
            }

            if (allow_inexpensive && !inside_an_or)
            {
                int mime_type;
                mime_type = random.Next(3);

                QueryPart_Or       mime_type_part = new QueryPart_Or();
                QueryPart_Property part;
                part      = new QueryPart_Property();
                part.Type = PropertyType.Keyword;
                part.Key  = "beagle:MimeType";

                if (mime_type == 0)
                {
                    part.Value = "inode/directory";
                    mime_type_part.Add(part);
                    query.AddPart(mime_type_part);
                }
                else if (mime_type == 1)
                {
                    part.Value = "text/plain";
                    mime_type_part.Add(part);
                    query.AddPart(mime_type_part);
                }
            }

            // Every query must contain at least
            // one required part.
            bool contains_required;

            contains_required = false;

            for (int i = 0; i < length; ++i)
            {
                QueryPart_Text part;
                part      = new QueryPart_Text();
                part.Text = Token.GetRandom();

                // Prohibited parts are not allowed inside an or
                if (contains_required && !inside_an_or)
                {
                    if (random.Next(2) == 0)
                    {
                        part.Logic = QueryPartLogic.Prohibited;
                    }
                }
                else
                {
                    // This part will be required.
                    contains_required = true;
                }

                if (random.Next(2) == 0)
                {
                    part.SearchTextProperties = false;
                }
                else if (allow_inexpensive && random.Next(2) == 0)
                {
                    part.SearchFullText = false;
                }

                query.AddPart(part);
            }

            // Note the ! inside_an_or; date range queries don't
            // work right inside OR queries when being searched
            // within the resolution of one day.  See the FIXME
            // about hit filters in LuceneCommon.cs
            if (allow_inexpensive && !inside_an_or && random.Next(3) == 0)
            {
                DateTime a, b;
                FileSystemObject.PickTimestampRange(out a, out b);

                QueryPart_DateRange part;
                part           = new QueryPart_DateRange();
                part.StartDate = a;
                part.EndDate   = b;
                query.AddPart(part);
            }

            return(query);
        }
Example #5
0
                static private QueryPart StringToQueryPart (string text, bool is_prohibited)
                {
                        QueryPart part;

                        if (text.IndexOf ('*') != -1) {
                                part = new QueryPart_Wildcard ();
                                ((QueryPart_Wildcard) part).QueryString = text;
                        } else {
                                part = new QueryPart_Text ();
                                ((QueryPart_Text) part).Text = text;
                        }

                        part.Logic = (is_prohibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                        return part;
                }