From 3e6a6ffd3520e104195007f4c6ed29c3d55db970 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Wed, 25 May 2011 19:23:30 -0400 Subject: [PATCH] Add support for filtering aliases to MoreLikeThis --- .../mlt/TransportMoreLikeThisAction.java | 8 ++-- .../document/MoreLikeThisActionTests.java | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java index 38d0217a46b..2b154694fef 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java @@ -84,7 +84,7 @@ public class TransportMoreLikeThisAction extends BaseAction getFields = newHashSet(); if (request.fields() != null) { @@ -93,7 +93,7 @@ public class TransportMoreLikeThisAction extends BaseAction fields = newHashSet(); if (request.fields() != null) { for (String field : request.fields()) { @@ -124,7 +124,7 @@ public class TransportMoreLikeThisAction extends BaseAction it = fields.iterator(); it.hasNext();) { + for (Iterator it = fields.iterator(); it.hasNext(); ) { String field = it.next(); GetField getField = getResponse.field(field); if (getField != null) { diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/MoreLikeThisActionTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/MoreLikeThisActionTests.java index 8b0159f87db..47c418e70fb 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/MoreLikeThisActionTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/MoreLikeThisActionTests.java @@ -30,6 +30,7 @@ import org.testng.annotations.Test; import static org.elasticsearch.client.Requests.*; import static org.elasticsearch.common.xcontent.XContentFactory.*; +import static org.elasticsearch.index.query.xcontent.FilterBuilders.termFilter; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @@ -83,4 +84,41 @@ public class MoreLikeThisActionTests extends AbstractNodesTests { assertThat(mltResponse.failedShards(), equalTo(0)); assertThat(mltResponse.hits().totalHits(), equalTo(1l)); } + + + @Test public void testMoreLikeThisWithAliases() throws Exception { + logger.info("Creating index test"); + client1.admin().indices().create(createIndexRequest("test")).actionGet(); + + logger.info("Creating aliases alias release"); + client1.admin().indices().aliases(indexAliasesRequest().addAlias("test", "release", termFilter("text", "release"))).actionGet(); + client1.admin().indices().aliases(indexAliasesRequest().addAlias("test", "beta", termFilter("text", "beta"))).actionGet(); + + logger.info("Running Cluster Health"); + ClusterHealthResponse clusterHealth = client1.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet(); + logger.info("Done Cluster Health, status " + clusterHealth.status()); + assertThat(clusterHealth.timedOut(), equalTo(false)); + assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN)); + + logger.info("Indexing..."); + client1.index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("text", "lucene beta").endObject())).actionGet(); + client1.index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("text", "lucene release").endObject())).actionGet(); + client1.index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("text", "elasticsearch beta").endObject())).actionGet(); + client1.index(indexRequest("test").type("type1").id("4").source(jsonBuilder().startObject().field("text", "elasticsearch release").endObject())).actionGet(); + client1.admin().indices().refresh(refreshRequest()).actionGet(); + + logger.info("Running moreLikeThis on index"); + SearchResponse mltResponse = client1.moreLikeThis(moreLikeThisRequest("test").type("type1").id("1").minTermFreq(1).minDocFreq(1)).actionGet(); + assertThat(mltResponse.hits().totalHits(), equalTo(2l)); + + logger.info("Running moreLikeThis on beta shard"); + mltResponse = client1.moreLikeThis(moreLikeThisRequest("beta").type("type1").id("1").minTermFreq(1).minDocFreq(1)).actionGet(); + assertThat(mltResponse.hits().totalHits(), equalTo(1l)); + assertThat(mltResponse.hits().getAt(0).id(), equalTo("3")); + + logger.info("Running moreLikeThis on release shard"); + mltResponse = client1.moreLikeThis(moreLikeThisRequest("test").type("type1").id("1").minTermFreq(1).minDocFreq(1).searchIndices("release")).actionGet(); + assertThat(mltResponse.hits().totalHits(), equalTo(1l)); + assertThat(mltResponse.hits().getAt(0).id(), equalTo("2")); + } }