-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- include_fields works the same way as exclude_fields. - These 2 settings are mutually exclusive (exclude_fields has the priority).
- Loading branch information
1 parent
1eb8f55
commit 2611238
Showing
7 changed files
with
285 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...test/java/test/elasticsearch/plugin/river/mongodb/simple/RiverMongoIncludeFieldsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package test.elasticsearch.plugin.river.mongodb.simple; | ||
|
||
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.util.Map; | ||
|
||
import org.bson.types.ObjectId; | ||
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import test.elasticsearch.plugin.river.mongodb.RiverMongoDBTestAsbtract; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.DB; | ||
import com.mongodb.DBCollection; | ||
import com.mongodb.DBObject; | ||
import com.mongodb.WriteConcern; | ||
|
||
@Test | ||
public class RiverMongoIncludeFieldsTest extends RiverMongoDBTestAsbtract { | ||
|
||
private DB mongoDB; | ||
private DBCollection mongoCollection; | ||
protected boolean dropCollectionOption = true; | ||
|
||
protected RiverMongoIncludeFieldsTest() { | ||
super("include-fields-river-" + System.currentTimeMillis(), | ||
"include-fields-db-" + System.currentTimeMillis(), | ||
"include-fields-collection-" + System.currentTimeMillis(), | ||
"include-fields-index-" + System.currentTimeMillis()); | ||
} | ||
|
||
protected RiverMongoIncludeFieldsTest(String river, String database, | ||
String collection, String index) { | ||
super(river, database, collection, index); | ||
} | ||
|
||
@BeforeClass | ||
public void createDatabase() { | ||
logger.debug("createDatabase {}", getDatabase()); | ||
try { | ||
mongoDB = getMongo().getDB(getDatabase()); | ||
mongoDB.setWriteConcern(WriteConcern.REPLICAS_SAFE); | ||
super.createRiver(TEST_MONGODB_RIVER_INCLUDE_FIELDS_JSON, | ||
getRiver(), (Object) String.valueOf(getMongoPort1()), | ||
(Object) String.valueOf(getMongoPort2()), | ||
(Object) String.valueOf(getMongoPort3()), | ||
(Object) "[\"include-field-1\", \"include-field-2\"]", | ||
(Object) getDatabase(), (Object) getCollection(), | ||
(Object) getIndex(), (Object) getDatabase()); | ||
logger.info("Start createCollection"); | ||
mongoCollection = mongoDB.createCollection(getCollection(), null); | ||
Assert.assertNotNull(mongoCollection); | ||
} catch (Throwable t) { | ||
logger.error("createDatabase failed.", t); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public void cleanUp() { | ||
super.deleteRiver(); | ||
logger.info("Drop database " + mongoDB.getName()); | ||
mongoDB.dropDatabase(); | ||
} | ||
|
||
@Test | ||
public void testIncludeFields() throws Throwable { | ||
logger.debug("Start testIncludeFields"); | ||
try { | ||
DBObject dbObject = new BasicDBObject(); | ||
dbObject.put("include-field-1", System.currentTimeMillis()); | ||
dbObject.put("include-field-2", System.currentTimeMillis()); | ||
dbObject.put("field-3", System.currentTimeMillis()); | ||
mongoCollection.insert(dbObject); | ||
Thread.sleep(wait); | ||
String id = dbObject.get("_id").toString(); | ||
assertThat( | ||
getNode().client().admin().indices() | ||
.exists(new IndicesExistsRequest(getIndex())) | ||
.actionGet().isExists(), equalTo(true)); | ||
refreshIndex(); | ||
|
||
SearchResponse sr = getNode().client().prepareSearch(getIndex()) | ||
.setQuery(fieldQuery("_id", id)).execute().actionGet(); | ||
logger.debug("SearchResponse {}", sr.toString()); | ||
long totalHits = sr.getHits().getTotalHits(); | ||
logger.debug("TotalHits: {}", totalHits); | ||
assertThat(totalHits, equalTo(1l)); | ||
|
||
Map<String, Object> object = sr.getHits().getHits()[0] | ||
.sourceAsMap(); | ||
assertThat(object.containsKey("include-field-1"), equalTo(true)); | ||
assertThat(object.containsKey("include-field-2"), equalTo(true)); | ||
assertThat(object.containsKey("field-3"), equalTo(false)); | ||
|
||
// Update Mongo object | ||
dbObject = mongoCollection.findOne(new BasicDBObject("_id", new ObjectId(id))); | ||
dbObject.put("field-4", System.currentTimeMillis()); | ||
mongoCollection.save(dbObject); | ||
Thread.sleep(wait); | ||
|
||
sr = getNode().client().prepareSearch(getIndex()) | ||
.setQuery(fieldQuery("_id", id)).execute().actionGet(); | ||
logger.debug("SearchResponse {}", sr.toString()); | ||
totalHits = sr.getHits().getTotalHits(); | ||
logger.debug("TotalHits: {}", totalHits); | ||
assertThat(totalHits, equalTo(1l)); | ||
|
||
object = sr.getHits().getHits()[0].sourceAsMap(); | ||
assertThat(object.containsKey("include-field-1"), equalTo(true)); | ||
assertThat(object.containsKey("include-field-2"), equalTo(true)); | ||
assertThat(object.containsKey("field-3"), equalTo(false)); | ||
assertThat(object.containsKey("field-4"), equalTo(false)); | ||
} catch (Throwable t) { | ||
logger.error("testIncludeFields failed.", t); | ||
t.printStackTrace(); | ||
throw t; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.