Search in sources :

Example 6 with Item

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());
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with Item

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);
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with Item

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;
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 9 with 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);
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 10 with Item

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);
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

Item (org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)16 MoreLikeThisQueryBuilder (org.elasticsearch.index.query.MoreLikeThisQueryBuilder)16 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)15 ArrayList (java.util.ArrayList)3 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Version (org.elasticsearch.Version)1 Client (org.elasticsearch.client.Client)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1