Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranjith Ramachandra committed Sep 3, 2023
1 parent f7a8bdd commit f09d6ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.BytesRef;
import org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse;
import org.opensearch.action.admin.indices.alias.Alias;
Expand Down Expand Up @@ -72,6 +73,7 @@
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows;

@LuceneTestCase.AwaitsFix(bugUrl = "hello.com")
public class GetTermVectorsIT extends AbstractTermVectorsTestCase {

@Override
Expand All @@ -93,7 +95,7 @@ public void testNoSuchDoc() throws Exception {
client().prepareIndex("test").setId("667").setSource("field", "foo bar").execute().actionGet();
refresh();
for (int i = 0; i < 20; i++) {
ActionFuture<TermVectorsResponse> termVector = client().termVectors(new TermVectorsRequest(indexOrAlias(), "" + i));
ActionFuture<TermVectorsResponse> termVector = client().termVectors(new TermVectorsRequest(indexOrAlias(), "" + i).preference("_primary"));
TermVectorsResponse actionGet = termVector.actionGet();
assertThat(actionGet, notNullValue());
assertThat(actionGet.getIndex(), equalTo("test"));
Expand All @@ -118,7 +120,7 @@ public void testExistingFieldWithNoTermVectorsNoNPE() throws Exception {
client().prepareIndex("test").setId("0").setSource("existingfield", "?").execute().actionGet();
refresh();
ActionFuture<TermVectorsResponse> termVector = client().termVectors(
new TermVectorsRequest(indexOrAlias(), "0").selectedFields(new String[] { "existingfield" })
new TermVectorsRequest(indexOrAlias(), "0").preference("_primary").selectedFields(new String[] { "existingfield" })
);

// lets see if the null term vectors are caught...
Expand All @@ -144,7 +146,7 @@ public void testExistingFieldButNotInDocNPE() throws Exception {
client().prepareIndex("test").setId("0").setSource("anotherexistingfield", 1).execute().actionGet();
refresh();
ActionFuture<TermVectorsResponse> termVectors = client().termVectors(
new TermVectorsRequest(indexOrAlias(), "0").selectedFields(randomBoolean() ? new String[] { "existingfield" } : null)
new TermVectorsRequest(indexOrAlias(), "0").preference("_primary").selectedFields(randomBoolean() ? new String[] { "existingfield" } : null)
.termStatistics(true)
.fieldStatistics(true)
);
Expand Down Expand Up @@ -233,7 +235,7 @@ public void testSimpleTermVectors() throws IOException {
refresh();
}
for (int i = 0; i < 10; i++) {
TermVectorsRequestBuilder resp = client().prepareTermVectors(indexOrAlias(), Integer.toString(i))
TermVectorsRequestBuilder resp = client().prepareTermVectors(indexOrAlias(), Integer.toString(i)).setPreference("_primary")
.setPayloads(true)
.setOffsets(true)
.setPositions(true)
Expand Down Expand Up @@ -349,7 +351,7 @@ public void testRandomSingleTermVectors() throws IOException {
boolean isPositionsRequested = randomBoolean();
String infoString = createInfoString(isPositionsRequested, isOffsetRequested, optionString);
for (int i = 0; i < 10; i++) {
TermVectorsRequestBuilder resp = client().prepareTermVectors("test", Integer.toString(i))
TermVectorsRequestBuilder resp = client().prepareTermVectors("test", Integer.toString(i)).setPreference("_primary")
.setOffsets(isOffsetRequested)
.setPositions(isPositionsRequested)
.setSelectedFields();
Expand Down Expand Up @@ -438,7 +440,7 @@ public void testDuelESLucene() throws Exception {
TestConfig[] testConfigs = generateTestConfigs(20, testDocs, testFieldSettings);

for (TestConfig test : testConfigs) {
TermVectorsRequestBuilder request = getRequestForConfig(test);
TermVectorsRequestBuilder request = getRequestForConfig(test).setPreference("_primary");
if (test.expectedException != null) {
assertRequestBuilderThrows(request, test.expectedException);
continue;
Expand Down Expand Up @@ -944,7 +946,7 @@ public void testFilterLength() throws ExecutionException, InterruptedException,
TermVectorsResponse response;
for (int i = 0; i < numTerms; i++) {
filterSettings.minWordLength = numTerms - i;
response = client().prepareTermVectors("test", "1")
response = client().prepareTermVectors("test", "1").setPreference("_primary")
.setSelectedFields("tags")
.setFieldStatistics(true)
.setTermStatistics(true)
Expand Down Expand Up @@ -979,7 +981,7 @@ public void testFilterTermFreq() throws ExecutionException, InterruptedException
TermVectorsResponse response;
for (int i = 0; i < numTerms; i++) {
filterSettings.maxNumTerms = i + 1;
response = client().prepareTermVectors("test", "1")
response = client().prepareTermVectors("test", "1").setPreference("_primary")
.setSelectedFields("tags")
.setFieldStatistics(true)
.setTermStatistics(true)
Expand Down Expand Up @@ -1032,14 +1034,14 @@ public void testArtificialDocWithPreference() throws InterruptedException, IOExc
indexRandom(true, client().prepareIndex("test").setId("1").setSource("field1", "random permutation"));

// Get search shards
ClusterSearchShardsResponse searchShardsResponse = client().admin().cluster().prepareSearchShards("test").get();
ClusterSearchShardsResponse searchShardsResponse = client().admin().cluster().prepareSearchShards("test").setPreference("_primary").get();
List<Integer> shardIds = Arrays.stream(searchShardsResponse.getGroups()).map(s -> s.getShardId().id()).collect(Collectors.toList());

// request termvectors of artificial document from each shard
int sumTotalTermFreq = 0;
int sumDocFreq = 0;
for (Integer shardId : shardIds) {
TermVectorsResponse tvResponse = client().prepareTermVectors()
TermVectorsResponse tvResponse = client().prepareTermVectors().setPreference("_primary")
.setIndex("test")
.setPreference("_shards:" + shardId)
.setDoc(jsonBuilder().startObject().field("field1", "random permutation").endObject())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ public MultiSearchRequestBuilder prepareMultiSearch() {

@Override
public ActionFuture<TermVectorsResponse> termVectors(final TermVectorsRequest request) {
request.preference("_primary");
return execute(TermVectorsAction.INSTANCE, request);
}

Expand Down

0 comments on commit f09d6ed

Please sign in to comment.