Created
May 13, 2013 23:45
-
-
Save Yegoroff/5572496 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 PlainElastic.Net; | |
using PlainElastic.Net.Queries; | |
using PlainElastic.Net.Serialization; | |
namespace MoreLikeThisSample | |
{ | |
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 MoreLikeThis query. | |
/* | |
{ | |
"query": { | |
"more_like_this": { | |
"fields": [ "Caption", "Text" ], | |
"like_text": "Note", | |
"min_term_freq": 1, | |
"min_doc_freq": 1 | |
} | |
} | |
} | |
*/ | |
string query = new QueryBuilder<Note>() | |
.Query(q => q | |
.MoreLikeThis(mlt => mlt | |
.Fields( note => note.Caption, 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
Hi,
With implementing DSL queries, My web request request returns with no data. I tried posting DSL query with ElasticSearch head and it only works with GET method
PlasinElastic.Net elasticconnection.cs uses POST method.