Search in sources :

Example 16 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testSimpleMoreLikeThisIdsMultipleTypes.

public void testSimpleMoreLikeThisIdsMultipleTypes() throws Exception {
    logger.info("Creating index test");
    int numOfTypes = randomIntBetween(2, 10);
    CreateIndexRequestBuilder createRequestBuilder = prepareCreate("test");
    for (int i = 0; i < numOfTypes; i++) {
        createRequestBuilder.addMapping("type" + i, jsonBuilder().startObject().startObject("type" + i).startObject("properties").startObject("text").field("type", "text").endObject().endObject().endObject().endObject());
    }
    assertAcked(createRequestBuilder);
    logger.info("Running Cluster Health");
    assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
    logger.info("Indexing...");
    List<IndexRequestBuilder> builders = new ArrayList<>(numOfTypes);
    for (int i = 0; i < numOfTypes; i++) {
        builders.add(client().prepareIndex("test", "type" + i).setSource("text", "lucene" + " " + i).setId(String.valueOf(i)));
    }
    indexRandom(true, builders);
    logger.info("Running MoreLikeThis");
    MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery(new String[] { "text" }, null, new Item[] { new Item("test", "type0", "0") }).include(true).minTermFreq(1).minDocFreq(1);
    String[] types = new String[numOfTypes];
    for (int i = 0; i < numOfTypes; i++) {
        types[i] = "type" + i;
    }
    SearchResponse mltResponse = client().prepareSearch().setTypes(types).setQuery(queryBuilder).execute().actionGet();
    assertHitCount(mltResponse, numOfTypes);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testMoreLikeThisIssue2197.

public void testMoreLikeThisIssue2197() throws Exception {
    Client client = client();
    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();
    client().prepareIndex("foo", "bar", "1").setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh("foo").execute().actionGet();
    assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
    SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("foo", "bar", "1") })).get();
    assertNoFailures(response);
    assertThat(response, notNullValue());
    response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("foo", "bar", "1") })).get();
    assertNoFailures(response);
    assertThat(response, notNullValue());
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) Client(org.elasticsearch.client.Client) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testMoreLikeThisArtificialDocs.

public void testMoreLikeThisArtificialDocs() throws Exception {
    int numFields = randomIntBetween(5, 10);
    createIndex("test");
    ensureGreen();
    logger.info("Indexing a single document ...");
    XContentBuilder doc = jsonBuilder().startObject();
    for (int i = 0; i < numFields; i++) {
        // make sure they are not all empty
        doc.field("field" + i, generateRandomStringArray(5, 10, false) + "a");
    }
    doc.endObject();
    indexRandom(true, client().prepareIndex("test", "type1", "0").setSource(doc));
    logger.info("Checking the document matches ...");
    MoreLikeThisQueryBuilder mltQuery = // routing to ensure we hit the shard with the doc
    moreLikeThisQuery(new Item[] { new Item("test", "type1", doc).routing("0") }).minTermFreq(0).minDocFreq(0).maxQueryTerms(100).minimumShouldMatch(// strict all terms must match!
    "100%");
    SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
    assertSearchResponse(response);
    assertHitCount(response, 1);
}
Also used : Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testMoreLikeThisUnlike.

public void testMoreLikeThisUnlike() throws ExecutionException, InterruptedException, IOException {
    createIndex("test");
    ensureGreen();
    int numFields = randomIntBetween(5, 10);
    logger.info("Create a document that has all the fields.");
    XContentBuilder doc = jsonBuilder().startObject();
    for (int i = 0; i < numFields; i++) {
        doc.field("field" + i, i + "");
    }
    doc.endObject();
    logger.info("Indexing each field value of this document as a single document.");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < numFields; i++) {
        builders.add(client().prepareIndex("test", "type1", i + "").setSource("field" + i, i + ""));
    }
    indexRandom(true, builders);
    logger.info("First check the document matches all indexed docs.");
    MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new Item[] { new Item("test", "type1", doc) }).minTermFreq(0).minDocFreq(0).maxQueryTerms(100).minimumShouldMatch("0%");
    SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
    assertSearchResponse(response);
    assertHitCount(response, numFields);
    logger.info("Now check like this doc, but ignore one doc in the index, then two and so on...");
    List<Item> docs = new ArrayList<>(numFields);
    for (int i = 0; i < numFields; i++) {
        docs.add(new Item("test", "type1", i + ""));
        mltQuery = moreLikeThisQuery(null, new Item[] { new Item("test", "type1", doc) }).unlike(docs.toArray(new Item[docs.size()])).minTermFreq(0).minDocFreq(0).maxQueryTerms(100).include(true).minimumShouldMatch("0%");
        response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
        assertSearchResponse(response);
        assertHitCount(response, numFields - (i + 1));
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) ArrayList(java.util.ArrayList) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testSelectFields.

public void testSelectFields() throws IOException, ExecutionException, InterruptedException {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=whitespace", "text1", "type=text,analyzer=whitespace"));
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("text", "hello world").field("text1", "elasticsearch").endObject()), client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("text", "goodby moon").field("text1", "elasticsearch").endObject()));
    MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new Item[] { new Item("test", "type1", "1") }).minTermFreq(0).minDocFreq(0).include(true).minimumShouldMatch("1%");
    SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
    assertSearchResponse(response);
    assertHitCount(response, 2);
    mltQuery = moreLikeThisQuery(new String[] { "text" }, null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(0).minDocFreq(0).include(true).minimumShouldMatch("1%");
    response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
    assertSearchResponse(response);
    assertHitCount(response, 1);
}
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)

Aggregations

MoreLikeThisQueryBuilder (org.elasticsearch.index.query.MoreLikeThisQueryBuilder)21 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)17 Item (org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item)16 ArrayList (java.util.ArrayList)5 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)5 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 Client (org.elasticsearch.client.Client)1