Permalink
Browse files

UI tidy up. Initial query work (WIP)

  • Loading branch information...
1 parent 63406cf commit fc19b5266643f414e832495d07c169769801cfae @AdaTheDev committed Feb 18, 2013
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using ElasticTweets.Library.Data;
+using ElasticTweets.Library.Providers;
+using ElasticTweets.Library.Queries;
+using Nest;
+using Nest.FactoryDsl;
+
+namespace ElasticTweets.Library
+{
+ public sealed class ElasticSearcher
+ {
+ private readonly IClientProvider _clientProvider;
+ private readonly IElasticConnectionSettings _elasticConnectionSettings;
+ private readonly IElasticClient _client;
+
+ public ElasticSearcher(IClientProvider clientProvider, IElasticConnectionSettings elasticConnectionSettings)
+ {
+ if (clientProvider == null) throw new ArgumentNullException("clientProvider");
+ if (elasticConnectionSettings == null) throw new ArgumentNullException("elasticConnectionSettings");
+
+ _clientProvider = clientProvider;
+ _elasticConnectionSettings = elasticConnectionSettings;
+ _client = _clientProvider.GetClient(_elasticConnectionSettings);
+ }
+
+ public IEnumerable<Tweet> SearchRaw(IElasticQuery query)
+ {
+ var response = ExecuteRaw(query.RawQueryText);
+ return Results(response);
+ }
+
+ public IEnumerable<Tweet> SearchClientApi(IElasticQuery query)
+ {
+ var response = ExecuteClient(query.Builder);
+ return Results(response);
+ }
+
+ private IEnumerable<Tweet> Results(IQueryResponse<Tweet> response)
+ {
+ return response.Documents;
+ }
+ private IQueryResponse<Tweet> ExecuteRaw(string query)
+ {
+ return _client.SearchRaw<Tweet>(query);
+ }
+
+ private IQueryResponse<Tweet> ExecuteClient(SearchBuilder searchBuilder)
+ {
+ return _client.Search<Tweet>(searchBuilder);
+ }
+ }
+}
@@ -0,0 +1,11 @@
+using Nest.FactoryDsl;
+
+namespace ElasticTweets.Library.Queries
+{
+ public interface IElasticQuery
+ {
+ string Description { get; }
+ string RawQueryText { get; }
+ SearchBuilder Builder { get; }
+ }
+}
@@ -0,0 +1,41 @@
+using Nest.FactoryDsl;
+using Nest.FactoryDsl.Filter;
+using Nest.FactoryDsl.Query;
+
+namespace ElasticTweets.Library.Queries
+{
+ public class TweetsContainingLinksQuery : IElasticQuery
+ {
+ /*
+ * {
+ * "query" : {
+ * "constant_score" : {
+ * "filter" : {
+ * "exists" : { "field" : "url"}
+ * }
+ * }
+ * }
+ * }
+ */
+ private const string RawQuery =
+ "{ \"query\" : {\"constant_score\" : {\"filter\" : {\"exists\" : { \"field\" : \"url\"}}}}}";
+
+
+ public string Description
+ {
+ get { return "Finds tweets that contain links"; }
+ }
+
+ public string RawQueryText { get { return RawQuery; } }
+
+ public SearchBuilder Builder
+ {
+ get
+ {
+ var searchBuilder = new SearchBuilder();
+ searchBuilder.Query(new ConstantScoreQueryBuilder(new ExistsFilterBuilder("url")));
+ return searchBuilder;
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit fc19b52

Please sign in to comment.