Search in sources :

Example 11 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)

Example 12 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)

Example 13 with MoreLikeThisQueryBuilder

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

the class MoreLikeThisIT method testSimpleMoreLikeInclude.

public void testSimpleMoreLikeInclude() 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", "Apache Lucene is a free/open source information retrieval software library").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("text", "Lucene has been ported to other programming languages").endObject())).actionGet();
    client().admin().indices().refresh(refreshRequest()).actionGet();
    logger.info("Running More Like This with include true");
    SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1).include(true).minimumShouldMatch("0%")).get();
    assertOrderedSearchHits(response, "1", "2");
    response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "2") }).minTermFreq(1).minDocFreq(1).include(true).minimumShouldMatch("0%")).get();
    assertOrderedSearchHits(response, "2", "1");
    logger.info("Running More Like This with include false");
    response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1).minimumShouldMatch("0%")).get();
    assertSearchHits(response, "2");
}
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 14 with MoreLikeThisQueryBuilder

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

the class MoreLikeThisIT method testMoreLikeThisWithAliases.

public void testMoreLikeThisWithAliases() 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("Creating aliases alias release");
    client().admin().indices().prepareAliases().addAlias("test", "release", termQuery("text", "release")).addAlias("test", "beta", termQuery("text", "beta")).get();
    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 beta").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("text", "lucene release").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("text", "elasticsearch beta").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("4").source(jsonBuilder().startObject().field("text", "elasticsearch release").endObject())).actionGet();
    client().admin().indices().refresh(refreshRequest()).actionGet();
    logger.info("Running moreLikeThis on index");
    SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(response, 2L);
    logger.info("Running moreLikeThis on beta shard");
    response = client().prepareSearch("beta").setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(response, 1L);
    assertThat(response.getHits().getAt(0).getId(), equalTo("3"));
    logger.info("Running moreLikeThis on release shard");
    response = client().prepareSearch("release").setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(response, 1L);
    assertThat(response.getHits().getAt(0).getId(), equalTo("2"));
    logger.info("Running moreLikeThis on alias with node client");
    response = internalCluster().coordOnlyNodeClient().prepareSearch("beta").setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(response, 1L);
    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 15 with MoreLikeThisQueryBuilder

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

the class MoreLikeThisIT method testNumericField.

// Issue #3252
public void testNumericField() throws Exception {
    final String[] numericTypes = new String[] { "byte", "short", "integer", "long" };
    prepareCreate("test").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("int_value").field("type", randomFrom(numericTypes)).endObject().startObject("string_value").field("type", "text").endObject().endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("string_value", "lucene index").field("int_value", 1).endObject()).execute().actionGet();
    client().prepareIndex("test", "type", "2").setSource(jsonBuilder().startObject().field("string_value", "elasticsearch index").field("int_value", 42).endObject()).execute().actionGet();
    refresh();
    // Implicit list of fields -> ignore numeric fields
    SearchResponse searchResponse = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type", "1") }).minTermFreq(1).minDocFreq(1)).get();
    assertHitCount(searchResponse, 1L);
    // Explicit list of fields including numeric fields -> fail
    assertThrows(client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(new String[] { "string_value", "int_value" }, null, new Item[] { new Item("test", "type", "1") }).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class);
    // mlt query with no field -> No results (because _all is not enabled)
    searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "index" }).minTermFreq(1).minDocFreq(1)).execute().actionGet();
    assertHitCount(searchResponse, 0L);
    // mlt query with string fields
    searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "string_value" }, new String[] { "index" }, null).minTermFreq(1).minDocFreq(1)).execute().actionGet();
    assertHitCount(searchResponse, 2L);
    // mlt query with at least a numeric field -> fail by default
    assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "string_value", "int_value" }, new String[] { "index" }, null)), SearchPhaseExecutionException.class);
    // mlt query with at least a numeric field -> fail by command
    assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "string_value", "int_value" }, new String[] { "index" }, null).failOnUnsupportedField(true)), SearchPhaseExecutionException.class);
    // mlt query with at least a numeric field but fail_on_unsupported_field set to false
    searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "string_value", "int_value" }, new String[] { "index" }, null).minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).get();
    assertHitCount(searchResponse, 2L);
    // mlt field query on a numeric field -> failure by default
    assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "int_value" }, new String[] { "42" }, null).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class);
    // mlt field query on a numeric field -> failure by command
    assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "int_value" }, new String[] { "42" }, null).minTermFreq(1).minDocFreq(1).failOnUnsupportedField(true)), SearchPhaseExecutionException.class);
    // mlt field query on a numeric field but fail_on_unsupported_field set to false
    searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery(new String[] { "int_value" }, new String[] { "42" }, null).minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).execute().actionGet();
    assertHitCount(searchResponse, 0L);
}
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