Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added defaultIndex processing for userInput items parsing #23581

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private Item parse(String queryToParse, String filterToParse, Language parsingLa
}
setState(parsingLanguage, indexFacts);

Item root = parseItems();
Item root = parseItems(defaultIndexName);
if (filterToParse != null) {
AnyParser filterParser = new AnyParser(environment);
if (root == null) {
Expand Down Expand Up @@ -222,8 +222,8 @@ private boolean is(Token.Kind kind, Token tokenOrNull) {
if (tokenOrNull == null) return false;
return kind.equals(tokenOrNull.kind);
}
protected abstract Item parseItems();

protected abstract Item parseItems(String defaultIndexName);

/**
* Assigns the default index to query terms having no default index. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AdvancedParser(ParserEnvironment environment) {
super(environment);
}

protected Item parseItems() {
protected Item parseItems(String defaultIndexName) {
return advancedItems(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public AllParser(ParserEnvironment environment, boolean weakAnd) {
}

@Override
protected Item parseItems() {
protected Item parseItems(String defaultIndexName) {
int position = tokens.getPosition();
try {
return parseItemsBody();
return parseItemsBody(defaultIndexName);
} finally {
tokens.setPosition(position);
}
}

protected Item parseItemsBody() {
protected Item parseItemsBody(String defaultIndexName) {
// Algorithm: Collect positive, negative, and and'ed items, then combine.
CompositeItem and = null;
NotItem not = null; // Store negatives here as we go
Expand All @@ -65,7 +65,7 @@ protected Item parseItemsBody() {

current = positiveItem();
if (current == null)
current = indexableItem();
current = indexableItem(defaultIndexName);
if (current == null)
current = compositeItem();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public AnyParser(ParserEnvironment environment) {
super(environment);
}

protected Item parseItems() {
return anyItems(true);
protected Item parseItems(String defaultIndexName) {
return anyItems(true, defaultIndexName);
}

Item parseFilter(String filter, Language queryLanguage, IndexFacts.Session indexFacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public PhraseParser(ParserEnvironment environment) {
super(environment);
}

protected Item parseItems() {
protected Item parseItems(String defaultIndex) {
return forcedPhrase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ protected Item handleComposite(boolean topLevel) {
* If there's a explicit composite and some other terms,
* a rank terms combines them
*/
protected Item anyItems(boolean topLevel) {
protected Item anyItems(boolean topLevel, String defaultIndexName) {
int position = tokens.getPosition();
Item item = null;

try {
item = anyItemsBody(topLevel);
item = anyItemsBody(topLevel, defaultIndexName);
return item;
} finally {
if (item == null) {
Expand All @@ -47,7 +47,11 @@ protected Item anyItems(boolean topLevel) {
}
}

private Item anyItemsBody(boolean topLevel) {
protected Item anyItems(boolean topLevel) {
return anyItems(topLevel, null);
}

private Item anyItemsBody(boolean topLevel, String defaultIndexName) {
Item topLevelItem = null;
NotItem not = null;
Item item = null;
Expand Down Expand Up @@ -88,7 +92,7 @@ private Item anyItemsBody(boolean topLevel) {
}

if (item == null) {
item = indexableItem();
item = indexableItem(defaultIndexName);
if (item != null) {
if (topLevelItem == null) {
topLevelItem = item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.yahoo.prelude.query.parser.Token.Kind.*;

Expand Down Expand Up @@ -52,11 +53,18 @@ protected void setSubmodeFromIndex(String indexName, IndexFacts.Session indexFac
}

protected Item indexableItem() {
return indexableItem(null);
}

protected Item indexableItem(String defaultIndexName) {
int position = tokens.getPosition();
Item item = null;

try {
String indexName = indexPrefix();
if (Objects.isNull(indexName)) {
indexName = defaultIndexName;
}
setSubmodeFromIndex(indexName, indexFacts);

item = number();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public TokenizeParser(ParserEnvironment environment) {
}

@Override
protected Item parseItems() {
protected Item parseItems(String defaultIndex) {
WeakAndItem weakAnd = new WeakAndItem();
Token token;
while (null != (token = tokens.next())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public WebParser(ParserEnvironment environment) {
}

@Override
protected Item parseItemsBody() {
protected Item parseItemsBody(String defaultIndexName) {
// Algorithm: Collect positive, negative, and'ed and or'ed elements, then combine.
CompositeItem and = null;
OrItem or = null;
Expand All @@ -45,7 +45,7 @@ protected Item parseItemsBody() {

current = positiveItem();
if (current == null)
current = indexableItem();
current = indexableItem(defaultIndexName);

if (current != null) {
if (and != null && (current instanceof WordItem) && "OR".equals(((WordItem)current).getRawWord())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import static org.junit.jupiter.api.Assertions.*;

import com.yahoo.search.query.QueryTree;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import org.apache.http.client.utils.URIBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -367,4 +370,32 @@ void testUserInputWithEmptyRangeStart() {
assertEquals("select * from sources * where text_field contains \"boom\"", query.yqlRepresentation());
}

@Test
void testUserInputWithPhraseSegmentingIndex() {
execution = new Execution(searchChain, Execution.Context.createContextStub(createIndexFacts(true)));
URIBuilder builder = searchUri();
builder.setParameter("wql", "foo&bar");
builder.setParameter("yql", "select * from sources * where ([{\"defaultIndex\": \"text_field\",\"grammar\": \"any\"}]userInput(@wql))");
Query query = searchAndAssertNoErrors(builder);
assertEquals("select * from sources * where text_field contains phrase(\"foo\", \"bar\")", query.yqlRepresentation());
}

@Test
void testUserInputWithNonPhraseSegmentingIndex() {
execution = new Execution(searchChain, Execution.Context.createContextStub(createIndexFacts(false)));
URIBuilder builder = searchUri();
builder.setParameter("wql", "foo&bar");
builder.setParameter("yql", "select * from sources * where ([{\"defaultIndex\": \"text_field\",\"grammar\": \"any\"}]userInput(@wql))");
Query query = searchAndAssertNoErrors(builder);
assertEquals("select * from sources * where (text_field contains \"foo\" AND text_field contains \"bar\")", query.yqlRepresentation());
}

private IndexFacts createIndexFacts(boolean phraseSegmenting) {
SearchDefinition sd = new SearchDefinition("sources");
Index test = new Index("text_field");
test.setPhraseSegmenting(phraseSegmenting);
sd.addIndex(test);
return new IndexFacts(new IndexModel(sd));
}

}