Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Created May 13, 2013 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yegoroff/5572496 to your computer and use it in GitHub Desktop.
Save Yegoroff/5572496 to your computer and use it in GitHub Desktop.
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; }
}
}
@Srini2010
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment