use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item in project elasticsearch by elastic.
the class MoreLikeThisIT method testMoreLikeWithCustomRouting.
// Issue #2489
public void testMoreLikeWithCustomRouting() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar").startObject("properties").endObject().endObject().endObject().string();
client().admin().indices().prepareCreate("foo").addMapping("bar", mapping, XContentType.JSON).execute().actionGet();
ensureGreen();
client().prepareIndex("foo", "bar", "1").setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject()).setRouting("2").execute().actionGet();
client().admin().indices().prepareRefresh("foo").execute().actionGet();
SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("foo", "bar", "1").routing("2") })).get();
assertNoFailures(response);
assertThat(response, notNullValue());
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item in project elasticsearch by elastic.
the class MoreLikeThisIT method testSimpleMoreLikeThis.
public void testSimpleMoreLikeThis() throws Exception {
logger.info("Creating index test");
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("text").field("type", "text").endObject().endObject().endObject().endObject()));
logger.info("Running Cluster Health");
assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
logger.info("Indexing...");
client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("text", "lucene").endObject())).actionGet();
client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("text", "lucene release").endObject())).actionGet();
client().admin().indices().refresh(refreshRequest()).actionGet();
logger.info("Running moreLikeThis");
SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
assertHitCount(response, 1L);
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilderTests method generateRandomItem.
private Item generateRandomItem() {
String index = randomBoolean() ? getIndex().getName() : null;
// set to one type to avoid ambiguous types
String type = getRandomType();
// indexed item or artificial document
Item item;
if (randomBoolean()) {
item = new Item(index, type, randomAsciiOfLength(10));
} else {
item = new Item(index, type, randomArtificialDoc());
}
// if no field is specified MLT uses all mapped fields for this item
if (randomBoolean()) {
item.fields(randomFrom(randomFields));
}
// per field analyzer
if (randomBoolean()) {
item.perFieldAnalyzer(randomPerFieldAnalyzer());
}
if (randomBoolean()) {
item.routing(randomAsciiOfLength(10));
}
if (randomBoolean()) {
item.version(randomInt(5));
}
if (randomBoolean()) {
item.versionType(randomFrom(VersionType.values()));
}
return item;
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilderTests method testItemFromXContent.
public void testItemFromXContent() throws IOException {
Item expectedItem = generateRandomItem();
String json = expectedItem.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).string();
XContentParser parser = createParser(JsonXContent.jsonXContent, json);
Item newItem = Item.parse(parser, new Item());
assertEquals(expectedItem, newItem);
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilderTests method testItemSerialization.
public void testItemSerialization() throws IOException {
Item expectedItem = generateRandomItem();
BytesStreamOutput output = new BytesStreamOutput();
expectedItem.writeTo(output);
Item newItem = new Item(output.bytes().streamInput());
assertEquals(expectedItem, newItem);
}
Aggregations