Created
May 14, 2013 00:06
-
-
Save Yegoroff/5572590 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 PlainElastic.Net; | |
using PlainElastic.Net.Queries; | |
using PlainElastic.Net.Serialization; | |
namespace ComplexMoreLikeThisSample | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var connection = new ElasticConnection("localhost", 9200); | |
var serializer = new JsonNetSerializer(); | |
// Add a sample document to index. | |
var noteToIndex = new Note { Caption = "Test Note", Text = "Note to test MoreLikeThis query" }; | |
string noteJson = serializer.ToJson(noteToIndex); | |
connection.Put(Commands.Index("notes", "note", "1"), noteJson); | |
// Create Complex MoreLikeThis query. | |
/* | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ "match_all": { } } | |
], | |
"should": [ | |
{ | |
"more_like_this": { | |
"fields": [ "Caption" ], | |
"like_text": "Test", | |
"min_term_freq": 1, | |
"min_doc_freq": 1 | |
} | |
}, | |
{ | |
"more_like_this": { | |
"fields": [ "Text" ], | |
"like_text": "Note", | |
"min_term_freq": 1, | |
"min_doc_freq": 1 | |
} | |
} | |
], | |
"minimum_number_should_match": 1 | |
} | |
} | |
} | |
*/ | |
string query = new QueryBuilder<Note>() | |
.Query(q => q | |
.Bool(b => b | |
.Must(m => m | |
.MatchAll() | |
) | |
.Should(s => s | |
.MoreLikeThis(mlt => mlt | |
.Fields(note => note.Caption) | |
.LikText("Test") | |
.MinTermFreq(1) | |
.MinDocFreq(1) | |
) | |
.MoreLikeThis(mlt => mlt | |
.Fields(note => note.Text) | |
.LikText("Note") | |
.MinTermFreq(1) | |
.MinDocFreq(1) | |
) | |
) | |
) | |
) | |
.BuildBeautified(); | |
Console.WriteLine("QUERY: \r\n" + query); | |
// Execute query and deserialize results. | |
string results = connection.Post(Commands.Search("notes", "note"), query); | |
var noteResults = serializer.ToSearchResult<Note>(results); | |
// Print results | |
Console.WriteLine("\r\nFOUND NOTES: \r\n"); | |
foreach (var note in noteResults.Documents) | |
{ | |
Console.WriteLine(note.Caption + ": " + note.Text); | |
} | |
Console.ReadKey(); | |
} | |
} | |
public class Note | |
{ | |
public string Caption { get; set; } | |
public string Text { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment