Search in sources :

Example 6 with MoreLikeThisQueryBuilder

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

the class MoreLikeThisIT method testMoreLikeThisWithAliasesInLikeDocuments.

// Issue #14944
public void testMoreLikeThisWithAliasesInLikeDocuments() throws Exception {
    String indexName = "foo";
    String aliasName = "foo_name";
    String typeName = "bar";
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar").startObject("properties").endObject().endObject().endObject().string();
    client().admin().indices().prepareCreate(indexName).addMapping(typeName, mapping, XContentType.JSON).get();
    client().admin().indices().prepareAliases().addAlias(indexName, aliasName).get();
    assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
    client().index(indexRequest(indexName).type(typeName).id("1").source(jsonBuilder().startObject().field("text", "elasticsearch index").endObject())).actionGet();
    client().index(indexRequest(indexName).type(typeName).id("2").source(jsonBuilder().startObject().field("text", "lucene index").endObject())).actionGet();
    client().index(indexRequest(indexName).type(typeName).id("3").source(jsonBuilder().startObject().field("text", "elasticsearch index").endObject())).actionGet();
    refresh(indexName);
    SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item(aliasName, typeName, "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(response, 2L);
    assertThat(response.getHits().getAt(0).getId(), equalTo("3"));
}
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 MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder 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 8 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder 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 9 with MoreLikeThisQueryBuilder

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

the class MoreLikeThisIT method testSimpleMoreLikeThisIds.

public void testSimpleMoreLikeThisIds() 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...");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene").setId("1"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene release").setId("2"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "apache lucene").setId("3"));
    indexRandom(true, builders);
    logger.info("Running MoreLikeThis");
    MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery(new String[] { "text" }, null, ids("1")).include(true).minTermFreq(1).minDocFreq(1);
    SearchResponse mltResponse = client().prepareSearch().setTypes("type1").setQuery(queryBuilder).execute().actionGet();
    assertHitCount(mltResponse, 3L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) 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 10 with MoreLikeThisQueryBuilder

use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project fess by codelibs.

the class EsAbstractConditionQuery method regMoreLikeThisQueryQ.

protected MoreLikeThisQueryBuilder regMoreLikeThisQueryQ(String name, String[] likeTexts) {
    MoreLikeThisQueryBuilder moreLikeThisQuery = QueryBuilders.moreLikeThisQuery(new String[] { name }, likeTexts, null);
    regQ(moreLikeThisQuery);
    return moreLikeThisQuery;
}
Also used : MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder)

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