Permalink
Browse files

1st query added to find all tweets with links in. UI to show results.

  • Loading branch information...
1 parent fc19b52 commit 9cf6957f0d4f4e391deae07b126babbd7413ccf5 @AdaTheDev committed Feb 27, 2013
@@ -24,30 +24,33 @@ public ElasticSearcher(IClientProvider clientProvider, IElasticConnectionSetting
_client = _clientProvider.GetClient(_elasticConnectionSettings);
}
- public IEnumerable<Tweet> SearchRaw(IElasticQuery query)
+ public IEnumerable<Tweet> SearchRaw(IElasticQuery query, int maxDocuments)
{
- var response = ExecuteRaw(query.RawQueryText);
+ var response = ExecuteRaw(query.RawQueryText, maxDocuments);
return Results(response);
}
- public IEnumerable<Tweet> SearchClientApi(IElasticQuery query)
+ public IEnumerable<Tweet> Search(IElasticQuery query, int maxDocuments)
{
- var response = ExecuteClient(query.Builder);
+ var response = ExecuteDsl(query.Query, maxDocuments);
return Results(response);
}
private IEnumerable<Tweet> Results(IQueryResponse<Tweet> response)
{
return response.Documents;
}
- private IQueryResponse<Tweet> ExecuteRaw(string query)
+
+ private IQueryResponse<Tweet> ExecuteRaw(string query, int maxDocuments)
{
- return _client.SearchRaw<Tweet>(query);
+ var searchBuilder = new SearchBuilder();
+ searchBuilder.Query(query).Size(maxDocuments);
+ return _client.Search<Tweet>(searchBuilder);
}
- private IQueryResponse<Tweet> ExecuteClient(SearchBuilder searchBuilder)
+ private IQueryResponse<Tweet> ExecuteDsl(QueryDescriptor<Tweet> query, int maxDocuments)
{
- return _client.Search<Tweet>(searchBuilder);
+ return _client.Search<Tweet>(s => s.Query(query).Size(maxDocuments));
}
}
}
@@ -76,6 +76,7 @@
<Compile Include="Providers\ClientProvider.cs" />
<Compile Include="Providers\IClientProvider.cs" />
<Compile Include="Queries\IElasticQuery.cs" />
+ <Compile Include="Queries\QueryDiscoverer.cs" />
<Compile Include="Queries\TweetsContainingLinksQuery.cs" />
</ItemGroup>
<ItemGroup>
@@ -1,11 +1,12 @@
-using Nest.FactoryDsl;
+using ElasticTweets.Library.Data;
+using Nest;
namespace ElasticTweets.Library.Queries
{
public interface IElasticQuery
{
string Description { get; }
string RawQueryText { get; }
- SearchBuilder Builder { get; }
+ QueryDescriptor<Tweet> Query { get; }
}
}
@@ -0,0 +1,19 @@
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace ElasticTweets.Library.Queries
+{
+ public class QueryDiscoverer
+ {
+ public IElasticQuery[] FindAll()
+ {
+ return
+ Assembly
+ .GetExecutingAssembly()
+ .GetTypes()
+ .Where(t => typeof(IElasticQuery).IsAssignableFrom(t) && t.IsClass)
+ .Select(query => (IElasticQuery) Activator.CreateInstance(query)).ToArray();
+ }
+ }
+}
@@ -1,6 +1,6 @@
-using Nest.FactoryDsl;
-using Nest.FactoryDsl.Filter;
-using Nest.FactoryDsl.Query;
+using System.Linq;
+using ElasticTweets.Library.Data;
+using Nest;
namespace ElasticTweets.Library.Queries
{
@@ -11,30 +11,30 @@ public class TweetsContainingLinksQuery : IElasticQuery
* "query" : {
* "constant_score" : {
* "filter" : {
- * "exists" : { "field" : "url"}
+ * "exists" : { "field" : "entities.urls.url"}
* }
* }
* }
* }
*/
private const string RawQuery =
- "{ \"query\" : {\"constant_score\" : {\"filter\" : {\"exists\" : { \"field\" : \"url\"}}}}}";
+ "{ \"query\" : {\"constant_score\" : {\"filter\" : {\"exists\" : { \"field\" : \"entities.urls.url\"}}}}}";
public string Description
{
- get { return "Finds tweets that contain links"; }
+ get { return "Tweets that contain links"; }
}
public string RawQueryText { get { return RawQuery; } }
- public SearchBuilder Builder
+ public QueryDescriptor<Tweet> Query
{
get
{
- var searchBuilder = new SearchBuilder();
- searchBuilder.Query(new ConstantScoreQueryBuilder(new ExistsFilterBuilder("url")));
- return searchBuilder;
+ var query = new QueryDescriptor<Tweet>();
+ query.ConstantScore(cs => cs.Filter(fd => fd.Exists(t =>t.entities.urls.First().url)));
+ return query;
}
}
}
View
@@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElasticTweets.Library", "El
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElasticTweets.Library.UnitTests", "ElasticTweets.Library.UnitTests\ElasticTweets.Library.UnitTests.csproj", "{5300D2DF-1462-4C4D-B3AF-D7BEE82846F6}"
EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9ED9F41C-4F7C-4761-BDCB-964CE29AEDC6}"
+ ProjectSection(SolutionItems) = preProject
+ readme.txt = readme.txt
+ EndProjectSection
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Oops, something went wrong.

0 comments on commit 9cf6957

Please sign in to comment.