Exemple #1
0
		        public Hit (uint id, Beagle.Hit hit, string[] fields, Query query)
			{
				this.id = id;
				bHit = hit;
				hitValue = new object[fields.Length];
				int i = 0;

				uri = hit.Uri;

				foreach (string field in fields) {
					// We add String.Empty to attributes because they may be null and we cannot
					// return null objects over DBus
					string[] bfields = Ontologies.XesamToBeagleField (field);

					switch (bfields [0]) {
					case "uri":
						hitValue [i++] = hit.Uri.ToString ();
						break;

					case "mimetype":
						hitValue [i++] = hit.MimeType + String.Empty;
						break;

					case "date":
						hitValue [i++] = hit.Timestamp.ToString ("s");
						break;

					case "snippet":
						SnippetRequest sreq = new SnippetRequest (query, hit);
						SnippetResponse sresp = (SnippetResponse) sreq.Send ();
						hitValue [i++] = sresp.Snippet != null ? sresp.Snippet : String.Empty;
						break;
					    
					default:
						// FIXME: This *will* break since we don't know what the expected
						// type here is, and we're always using strings
						
						List<string> values = new List<string> ();

						foreach (string bfield in bfields) {
							string[] prop = hit.GetProperties (bfield);

							if (prop != null)
								values.AddRange (prop);
						}

						if (values.Count == 0)
							// No values found
							hitValue [i++] = String.Empty;
						else if (values.Count == 1)
							// Only one value -- return as string
							hitValue [i++] = values [0];
						else
							// Multiple values -- returns as string[]
							hitValue [i++] = values.ToArray ();

						break;
					}
				}
			}
Exemple #2
0
	private static void OnHitsAdded (HitsAddedResponse response)
	{
		lastQueryTime = DateTime.Now;

		if (count == 0 && verbose) {
			Console.WriteLine ("First hit returned in {0:0.000}s",
					   (lastQueryTime - queryStartTime).TotalSeconds);
		}

		if (verbose) {
			if (response.NumMatches >= 0)
				Console.WriteLine ("Returned latest {0} results out of total {1} matches", response.Hits.Count, response.NumMatches);
			else
				Console.WriteLine ("Returned latest {0} results", response.Hits.Count);
		}

		if (! display_hits) {
			count += response.Hits.Count;
			return;
		}

		foreach (Hit hit in response.Hits) {
			string uri = (raw_uri ?  hit.Uri.OriginalString : UriFu.EscapedUriToString (hit.Uri));
			if (verbose)
				Console.WriteLine ("  Uri: {0}", uri);
			else
				Console.WriteLine (uri);

			if (verbose) {
				SnippetRequest sreq = new SnippetRequest (query, hit);
				if (display_cached_text)
					sreq.FullText = true;

				SnippetResponse sresp = (SnippetResponse) sreq.Send ();
				Console.WriteLine ("PaUri: {0}", hit.ParentUri != null ? hit.ParentUri.ToString () : "(null)");
				if (! display_cached_text)
					Console.WriteLine (" Snip: {0}", sresp.Snippet != null ? sresp.Snippet : "(null)");
				Console.WriteLine (" Type: {0}", hit.Type);
				Console.WriteLine ("MimeT: {0}", hit.MimeType == null ? "(null)" : hit.MimeType);
				Console.WriteLine ("  Src: {0}", hit.Source);
				Console.WriteLine ("Score: {0}", hit.Score);
				if (hit.ValidTimestamp)
					Console.WriteLine (" Time: {0}", DateTimeUtil.ToString (hit.Timestamp));
				
				foreach (Property prop in hit.Properties)
					Console.WriteLine ("    {0} = '{1}'",
						prop.Key,
						(prop.Type != PropertyType.Date ? prop.Value : DateTimeUtil.ToString (StringFu.StringToDateTime (prop.Value))));
				

				if (display_cached_text) {
					Console.WriteLine ("-- Cache -------------------------------------");
					if (sresp.SnippetList.Snippets == null)
						Console.WriteLine ("(empty)");
					else {
						foreach (SnippetLine snippet_line in sresp.SnippetList.Snippets) {
							if (snippet_line == null || snippet_line.Fragments == null)
								Console.WriteLine ("(empty)");
							else
								Console.WriteLine (((Fragment)snippet_line.Fragments [0]).Text);
						}
					}
					Console.WriteLine ("----------------------------------------------");
				}
				Console.WriteLine ();
			}

			++count;
		}
	}
	private static void OnHitsAdded (QueryResult result, ICollection hits, int num_hits)
	{
		lastQueryTime = DateTime.Now;

		if (count == 0 && verbose) {
			Console.WriteLine ("First hit returned in {0:0.000}s",
					   (lastQueryTime - queryStartTime).TotalSeconds);
		}

		if (verbose && num_hits >= 0)
			Console.WriteLine ("Returned latest {0} results out of total {1} matches", hits.Count, num_hits);

		if (! display_hits) {
			count += hits.Count;
			return;
		}

		foreach (Hit hit in hits) {
			if (verbose)
				Console.WriteLine ("  Uri: {0}", hit.Uri);
			else
				Console.WriteLine (hit.Uri);

			if (verbose) {
				SnippetRequest sreq = new SnippetRequest (query, hit);
				if (display_cached_text)
					sreq.FullText = true;

				List<SnippetLine> snippets = GetSnippet (sreq);
				Console.WriteLine ("PaUri: {0}", hit.ParentUri != null ? hit.ParentUri.ToString () : "(null)");
				if (! display_cached_text) {
					Console.Write (" Snip: ");
					if (snippets.Count == 0)
						Console.WriteLine ("(null)");
					else {
						foreach (SnippetLine snippet_line in snippets) {
							Console.Write (snippet_line);
							Console.Write (" ... ");
						}

						Console.WriteLine ();
					}
				}

				Console.WriteLine (" Type: {0}", hit.Type);
				Console.WriteLine ("MimeT: {0}", hit.MimeType == null ? "(null)" : hit.MimeType);
				Console.WriteLine ("  Src: {0}", hit.Source);
				Console.WriteLine ("Score: {0}", hit.Score);
				if (hit.ValidTimestamp)
					Console.WriteLine (" Time: {0}", DateTimeUtil.ToString (hit.Timestamp));
				
				foreach (Property prop in hit.Properties)
					Console.WriteLine ("    {0} = '{1}'",
						prop.Key,
						(prop.Type != PropertyType.Date ? prop.Value : DateTimeUtil.ToString (StringFu.StringToDateTime (prop.Value))));
				

				if (display_cached_text) {
					Console.WriteLine ("-- Cache -------------------------------------");
					if (snippets.Count == 0)
						Console.WriteLine ("(empty)");
					else {
						foreach (SnippetLine snippet_line in snippets) {
							if (snippet_line == null || snippet_line.Fragments == null)
								Console.WriteLine ("(empty)");
							else
								Console.WriteLine (((Fragment)snippet_line.Fragments [0]).Text);
						}
					}
					Console.WriteLine ("----------------------------------------------");
				}
				Console.WriteLine ();
			}

			++count;
		}
	}
	private static List<SnippetLine> GetSnippet (SnippetRequest request)
	{
		Queryable queryable = QueryDriver.GetQueryable (request.Hit.Source);
		ISnippetReader snippet_reader;
		bool full_text = request.FullText;
		int ctx_length = request.ContextLength;
		int snp_length = request.SnippetLength;

		if (queryable == null) {
			Console.WriteLine ("SnippetExecutor: No queryable object matches '{0}'", request.Hit.Source);
			snippet_reader = new SnippetReader (null, null, false, -1, -1);
			full_text = false;
		} else
			snippet_reader = queryable.GetSnippet (request.QueryTerms, request.Hit, full_text, ctx_length, snp_length);

		List<SnippetLine> snippetlines = new List<SnippetLine> ();
		if (snippet_reader == null)
			return snippetlines;

		if (! full_text) {
 			foreach (SnippetLine snippet_line in snippet_reader.GetSnippet ())
				snippetlines.Add (snippet_line);
		} else {
			SnippetLine snippet_line = new SnippetLine ();
			snippet_line.Line = 1;

			Fragment fragment = new Fragment ();
			fragment.QueryTermIndex = -1;
			StringBuilder sb = new StringBuilder ();

			string line;
			// Read data from snippet_reader and write
			while ((line = snippet_reader.ReadLine ()) != null) {
				sb.Append (StringFu.CleanupInvalidXmlCharacters (line));
				sb.Append ("\n");
			}

			fragment.Text = sb.ToString ();
			snippet_line.Fragments = new ArrayList ();
			snippet_line.Fragments.Add (fragment);
			snippetlines.Add (snippet_line);
		}

		snippet_reader.Close ();

		return snippetlines;
	}
Exemple #5
0
		protected void RequestSnippet ()
		{
			if (snippet != null) {
				EmitGotSnippet ();
			} else {
				SnippetRequest sreq = new SnippetRequest (query, hit);
				sreq.RegisterAsyncResponseHandler (typeof (SnippetResponse), SnippetResponseReceived);
				sreq.SendAsync ();
			}
		}
		public ISnippetReader GetSnippet (string[] query_terms, Hit hit, bool full_text, int ctx_length, int snp_length)
		{
			string source = hit ["beagle:Source"];
			hit ["beagle:Source"] = hit ["beagle:OrigSource"];

			string network_node = hit ["beagle:NetworkNode"];
			SnippetReader snippet_reader = null;

			// FIXME: Creating a snippet request, registering transports, all of this
			// doing everytime for hundreds of hits may become quite expensive.
			// In that case, pre generate one snippetrequest and use it over and over.

			// Form a correct snippet request
			SnippetRequest sreq = new SnippetRequest ();
			sreq.Hit = hit;
			sreq.QueryTerms = query_terms;
			sreq.FullText = full_text;
			sreq.ContextLength = ctx_length;
			sreq.SnippetLength = snp_length;

			// fake a blocking snippet retrieval
			sreq.RegisterAsyncResponseHandler (typeof (SnippetResponse),
							   delegate (ResponseMessage response) {
				if (response is ErrorResponse) {
					Log.Error ("Error retrieval snippet for {0} from network node {1}", hit.Uri, network_node);
					return;
				}

				snippet_reader = new SnippetReader ((SnippetResponse) response);
			});

			List<string[]> network_services = Conf.Networking.GetListOptionValues (Conf.Names.NetworkServices);
			foreach (string[] service in network_services) {
				if (network_node != service [0])
					continue;

				sreq.Transports.Clear ();
				sreq.RegisterTransport (new HttpTransport (service [1]));

				// fake a blocking snippet retrieval
				try {
					sreq.SendAsyncBlocking ();
				} catch (Exception e) {
					Log.Debug (e, "Error while requesting snippet from {0} for {1}", service [1], hit.Uri);
				}
				break;
			}

			hit ["beagle:Source"] = source; // reset source
			return snippet_reader;
		}