Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code samples play2-elasticsearch
object Application extends Controller {
def async = Action {
IndexTestManager.index(IndexTest("1", "Here is the first name", "First category"))
IndexTestManager.index(IndexTest("2", "Then comes the second name", "First category"))
IndexTestManager.index(IndexTest("3", "Here is the third name", "Second category"))
IndexTestManager.index(IndexTest("4", "Finnaly is the fourth name", "Second category"))
IndexTestManager.refresh()
val indexQuery = IndexTestManager.query
.withBuilder(QueryBuilders.matchQuery("name", "Here"))
val indexQuery2 = IndexTestManager.query
.withBuilder(QueryBuilders.matchQuery("name", "third"))
// Combining futures
val l: Future[(IndexResults[IndexTest], IndexResults[IndexTest])] = for {
result1 <- IndexTestManager.searchAsync(indexQuery)
result2 <- IndexTestManager.searchAsync(indexQuery2)
} yield (result1, result2)
Async {
l.map { case (r1, r2) =>
Ok(r1.totalCount + " - " + r2.totalCount)
}
}
}
}
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "elasticsearch-sample"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
"com.clever-age" % "play2-elasticsearch" % "0.5.4"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
resolvers += Resolver.url("play-plugin-releases", new URL("http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/"))(Resolver.ivyStylePatterns),
resolvers += Resolver.url("play-plugin-snapshots", new URL("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots/"))(Resolver.ivyStylePatterns)
)
}
elasticsearch {
# permet d'utiliser elasticsearch de manière "embarquée" à l'application
local: true
# liste des Node elasticsearch auxquels se connecter
client: "127.0.0.1:9300,127.0.0.1:9301"
# cluster.name elasticsearch auquel se connecter
cluster.name: "elasticsearch"
index {
# le(s) nom(s) d'index qui sera(ont) utilisé(s) dans l'application
name: play2-elasticsearch,log
# pattern définissant les classes "indexables"
clazzs: "indexing.*"
# mapping elasticsearch qui seront appliqué au démarrage de l'application
mappings: {
# la clé est le "type" elasticsearch et la valeur est le mapping
"indexTest": "{\"indexTest\":{\"properties\":{\"category\":{\"type\":\"string\",\"analyzer\":\"keyword\"}}}}"
}
# activation du log des requêtes (logs effectués au niveau "DEBUG")
show_request: true,
}
# paramètres additionnels qui seront appliqués sur l'index au démarrage
play2-elasticsearch.settings: "{ analysis: { analyzer: { team_name_analyzer: { type: \"custom\", tokenizer: \"standard\" } } } }"
}
case class IndexableTest(id: String, name: String, category: String) extends Indexable
object IndexableTestManager extends IndexableManager[IndexableTest] {
import play.api.libs.json._
// Obligatoire : le type Elasticsearch à utiliser
val indexType = "indexableTest"
// Optionnel : le nom de l'index si on ne veut pas utiliser le premier déclarer dans la conf
// val index = "log"
val reads: Reads[IndexableTest] = Json.reads[IndexableTest]
val writes: Writes[IndexableTest] = Json.writes[IndexableTest]
}
IndexableTestManager.index(IndexableTest("1", "first name", "cateogory A"))
assert(IndexableTestManager.get("1") == IndexableTest("1", "first name", "cateogory A")
val indexQuery = IndexQuery[IndexableTest]().withBuilder(QueryBuilders.matchQuery("name", "first"))
//.withSize(...)
//.withFrom(...)
//.addFacet(...)
//.addSort(...)
val queryResults: IndexResults[IndexableTest] = IndexableTestManager.search(indexQuery)
println(queryResults.results.map(_.name).mkString(",")) val count = queryResults.totalCount
IndexableTestManager.delete("1")
@IndexType(name = "indexTest")
public class IndexTest extends Index {
public String name;
// Finder used to request ElasticSearch
public static Finder<IndexTest> find = new Finder<IndexTest>(IndexTest.class);
@Override
public Map toIndex() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
return map;
}
@Override
public Indexable fromIndex(Map map) {
this.name = (String) map.get("name");
return this;
}
}
IndexTest indexTest = new IndexTest();
indexTest.name = "hello World";
indexTest.index();
IndexTest byId = IndexTest.find.byId("1");
IndexResults<IndexTest> all = IndexTest.find.all();
IndexQuery<IndexTest> indexQuery = IndexTest.find.query();
indexQuery.setBuilder(QueryBuilders.queryString("hello"));
IndexResults<IndexTest> results = IndexTest.find.search(indexQuery);
11000:com.github.cleverage.elasticsearch.plugin.IndexPlugin
@IndexType(name = "team")
@IndexMapping(value = "{ players : { properties : { players : { type : \"nested\" }, name : { type : \"string\", analyzer : \"team_name_analyzer\" } } } }")
public class Team extends Index {
public String name;
public Date dateCreate;
public String level;
public Country country;
public List<Player> players = new ArrayList<Player>();
// Finder used to request ElasticSearch
public static Finder<Team> find = new Finder<Team>(Team.class);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment