Last active
December 31, 2015 06:09
-
-
Save Yegoroff/7945379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using PlainElastic.Net; | |
using PlainElastic.Net.Queries; | |
using PlainElastic.Net.Serialization; | |
using PlainElastic.Net.Utils; | |
namespace RandomSortSample | |
{ | |
internal class Tweet | |
{ | |
public string Name { get; set; } | |
public string Text { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connection = new ElasticConnection("localhost"); | |
var serializer = new JsonNetSerializer(); | |
for (int i = 0; i < 10; i++) | |
{ | |
var tweet = new Tweet | |
{ | |
Name = "User" + i, | |
Text = "some text from user" + i | |
}; | |
connection.Put(Commands.Index("twitter", "tweet", i.AsString()).Refresh(), | |
serializer.Serialize(tweet)); | |
} | |
// use .FunctionScore query to genrater random score | |
var query = new QueryBuilder<Tweet>() | |
.Query(q => q | |
.FunctionScore(fs => fs | |
.Query(qfs => qfs.MatchAll()) | |
.Function(f => f | |
.RandomScore(rs => rs | |
.Seed(1379333621000) | |
) | |
) | |
) | |
) | |
.Sort(srt => srt | |
.Field("_score", SortDirection.asc) | |
) | |
.BuildBeautified(); | |
var result = connection.Post(Commands.Search("twitter", "tweet"), query); | |
var randomlyOrderedTweets = serializer.ToSearchResult<Tweet>(result).Documents; | |
Console.WriteLine(randomlyOrderedTweets.Select(t => t.Name + ": " + t.Text).JoinWithSeparator("\r\n")); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using PlainElastic.Net; | |
using PlainElastic.Net.Queries; | |
using PlainElastic.Net.Serialization; | |
using PlainElastic.Net.Utils; | |
namespace RandomSortSample | |
{ | |
internal class Tweet | |
{ | |
public string Name { get; set; } | |
public string Text { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connection = new ElasticConnection("localhost"); | |
var serializer = new JsonNetSerializer(); | |
for (int i = 0; i < 10; i++) | |
{ | |
var tweet = new Tweet | |
{ | |
Name = "User" + i, | |
Text = "some text from user" + i | |
}; | |
connection.Put(Commands.Index("twitter", "tweet", i.AsString()).Refresh(), | |
serializer.Serialize(tweet)); | |
} | |
var query = new QueryBuilder<Tweet>() | |
.Query(q => q | |
.Custom( | |
@"{ | |
'function_score': { | |
'query': { 'match_all': {}}, | |
'random_score': { | |
'seed': 1379333621000 | |
} | |
} | |
}".AltQuote() | |
) | |
) | |
.Sort( srt => srt | |
.Field("_score",SortDirection.asc) | |
) | |
.BuildBeautified(); | |
var result = connection.Post(Commands.Search("twitter", "tweet"), query); | |
var randomlyOrderedTweets = serializer.ToSearchResult<Tweet>(result).Documents; | |
Console.WriteLine(randomlyOrderedTweets.Select(t => t.Name + ": " + t.Text).JoinWithSeparator("\r\n") ); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment