Skip to content
Open
3 changes: 2 additions & 1 deletion docs/developers/search-v3-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,8 @@ The builder rewrites the query before any clause sees it. The service computes t
| `unquoted` | `raw` with one pair of surrounding quotes removed. |
| `shortened` | `normalized` cut on a word boundary. |
| `lastSegment` | The last `.` / `_` / `-` / `/` segment of `raw`. |
| `synonyms` | The synonym list for the whole query. |
| `synonyms` | The synonyms of the whole query and of each of its words. |
| `exactSynonyms` | The synonyms of the whole query only. An exact-name clause reads it: a word's synonym is not a longer query's. |

Shape flags are computed by the service and referenced from the body:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.linkedin.metadata.search.MatchedField;
import com.linkedin.metadata.search.elasticsearch.index.entity.v3.V3SearchFields;
import com.linkedin.metadata.search.elasticsearch.query.request.SearchHitMatches;
import com.linkedin.metadata.search.elasticsearch.query.template.SearchTemplateSynonyms;
import com.linkedin.metadata.search.utils.ESUtils;
import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -15,6 +16,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -36,7 +38,8 @@
* would cost every write. A field matches when one of its words stems to the same word as a word of
* the query, as the shared fields' {@code stemmed} subfields do, or starts with the last word of
* the query, as their phrase prefix does; an identifier such as {@code customer_id} also matches by
* its parts, as the analyzers index them. This is best effort: synonyms are not applied, words are
* its parts, as the analyzers index them. A synonym of the query or of one of its words, from the
* synonym files the analyzers load, matches like the word itself. This is best effort: words are
* split on what is not a letter, a digit or an underscore even when a main tokenizer is configured,
* and as on V2 only the first matching value of a field is reported.
*
Expand Down Expand Up @@ -81,8 +84,19 @@ public MatchedFields(@Nonnull final String query, final int minWordLength) {
tokens.add(matcher.group());
}
}
this.queryStems =
queryWords.stream().flatMap(List::stream).map(this::stem).collect(Collectors.toSet());
final Set<String> stems =
queryWords.stream()
.flatMap(List::stream)
.map(this::stem)
.collect(Collectors.toCollection(HashSet::new));
// The analyzers expand synonyms, so a document that holds a synonym matched the query.
for (String synonym : SearchTemplateSynonyms.expansions(query)) {
final Matcher synonymWords = WORD.matcher(synonym);
while (synonymWords.find()) {
forms(synonymWords.group()).forEach(form -> stems.add(stem(form)));
}
}
this.queryStems = Set.copyOf(stems);
this.lastQueryForms = queryWords.isEmpty() ? List.of() : queryWords.get(queryWords.size() - 1);
this.queryWordForms = List.copyOf(queryWords);
this.queryTokens = List.copyOf(tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public final class SearchTemplateContextVariables {
*/
public static final String SYNONYM = "synonym";

/**
* Synonyms of the whole query only, as {@link #SYNONYM} elements. {@link #SYNONYMS} also holds
* each word's synonyms, which are not synonyms of a longer query, so an exact-name clause reads
* this list.
*/
public static final String EXACT_SYNONYMS = "exactSynonyms";

public static final String MULTI_WORD = "multiWord";
public static final String NAME_ONLY = "nameOnly";
public static final String LONG_QUERY = "longQuery";
Expand Down Expand Up @@ -203,6 +210,7 @@ public final class SearchTemplateContextVariables {
STRUCTURED_QUERY,
FLAGS,
SYNONYMS,
EXACT_SYNONYMS,
MULTI_WORD,
NAME_ONLY,
LONG_QUERY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public static Map<String, Object> validationContext(@Nonnull String source) {
Collections.nCopies(SearchTemplateNormalizationPolicy.MAX_SYNONYM_CAP, "production");
context.put(SearchTemplateContextVariables.SYNONYM_QUERIES, synonyms);
context.put(SearchTemplateContextVariables.SYNONYMS, synonymElements(synonyms));
context.put(SearchTemplateContextVariables.EXACT_SYNONYMS, synonymElements(synonyms));
context.put(SearchTemplateContextVariables.SYNONYM_PRIORITY, Boolean.TRUE);
context.put(SearchTemplateContextVariables.MULTI_WORD, Boolean.TRUE);
context.put(SearchTemplateContextVariables.LONG_QUERY, Boolean.TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public final class SearchTemplateQueryShape {
public final String fqnQuery;
public final List<String> synonyms;

/** Synonyms of the whole query only, without each word's. */
public final List<String> exactSynonyms;

/**
* A dotted query with no whitespace, at least two non-empty segments, and not a URN or storage
* path. Cranfield sentences that end in a period are not this shape.
Expand Down Expand Up @@ -83,6 +86,7 @@ private SearchTemplateQueryShape(
String letterDigitQuery,
String fqnQuery,
List<String> synonyms,
List<String> exactSynonyms,
boolean spacelessDotted,
String terminalSegment,
List<String> qualifierSegments) {
Expand All @@ -108,6 +112,7 @@ private SearchTemplateQueryShape(
this.letterDigitQuery = letterDigitQuery;
this.fqnQuery = fqnQuery;
this.synonyms = synonyms;
this.exactSynonyms = exactSynonyms;
this.spacelessDotted = spacelessDotted;
this.terminalSegment = terminalSegment;
this.qualifierSegments = qualifierSegments;
Expand Down Expand Up @@ -170,6 +175,7 @@ public static SearchTemplateQueryShape of(@Nullable String query, int wildcardMi
letterDigit ? normalized : null,
fqnQuery,
SearchTemplateSynonyms.expansions(raw),
SearchTemplateSynonyms.wholeQuery(raw),
spacelessDotted,
spacelessDotted ? dottedSegments.get(dottedSegments.size() - 1) : null,
spacelessDotted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public static List<String> expansions(@Nonnull String query) {
return List.copyOf(found);
}

/**
* Other members of the equivalence class for the whole query only. A word's synonym is not a
* synonym of a longer query, so an exact-name clause reads this list rather than {@link
* #expansions}.
*/
@Nonnull
public static List<String> wholeQuery(@Nonnull String query) {
String text = query.toLowerCase(Locale.ROOT).trim();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wholeQuery("data platform") returns dataplatform, while wholeQuery("data platform") returns an empty list. The matched-field scan recognizes the phrase with either spacing. Could we normalize internal whitespace before looking up a whole phrase and add a test for repeated spaces?

if (text.isEmpty()) {
return List.of();
}
Set<String> found = new LinkedHashSet<>();
addOthers(found, text);
found.remove(text);
return List.copyOf(found);
}

private static void addOthers(Set<String> found, String term) {
List<String> others = current().get(term);
if (others != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,15 @@ private static void putQueryShape(
SearchTemplateContextVariables.SYNONYMS,
SearchTemplateMustache.synonymElements(synonyms));
}
List<String> exactSynonyms = shape.exactSynonyms;
if (synonymCap < exactSynonyms.size()) {
exactSynonyms = synonymCap <= 0 ? List.of() : exactSynonyms.subList(0, synonymCap);
}
if (!exactSynonyms.isEmpty()) {
context.put(
SearchTemplateContextVariables.EXACT_SYNONYMS,
SearchTemplateMustache.synonymElements(exactSynonyms));
}
Comment thread
alexjst marked this conversation as resolved.
Resolved
if (shape.wildcardTerm != null) {
context.put(SearchTemplateContextVariables.WILDCARD_TERM, shape.wildcardTerm);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public void testQueryWordFindsWordsWithTheSameStem() {
new MatchedFields("card", 0).find(Map.of("name", "car rental"), List.of("name")).isEmpty());
}

/** A synonym from the analyzers' synonym files matches a field, as it matched the document. */
@Test
public void testSynonymsOfTheQueryMatch() {
Map<String, Object> source = Map.of("name", "stg_orders");
// stg and staging are one equivalence line in the synonym file; neither is a prefix or stem
// of the other
assertEquals(
names(new MatchedFields("staging", 0).find(source, List.of("name"))), List.of("name"));
assertEquals(
names(new MatchedFields("staging events", 0).find(source, List.of("name"))),
List.of("name"));
assertTrue(new MatchedFields("development", 0).find(source, List.of("name")).isEmpty());
}

/**
* Only the last query word matches as a prefix, as the phrase prefix does; the others match whole
* words or their stems. Words shorter than the minimum length are dropped, as the analyzers drop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ public void testEquivalenceExpansionsSkipExplicitRules() {
assertEquals(SearchTemplateSynonyms.expansions(""), List.of());
}

/** A word's synonym is not a synonym of a longer query, so the whole-query list leaves it out. */
@Test
public void testWholeQuerySynonymsLeaveOutWordSynonyms() {
assertEquals(
SearchTemplateSynonyms.wholeQuery("prod"), SearchTemplateSynonyms.expansions("prod"));
assertFalse(SearchTemplateSynonyms.expansions("prod orders").isEmpty());
assertTrue(SearchTemplateSynonyms.wholeQuery("prod orders").isEmpty());
assertTrue(SearchTemplateSynonyms.wholeQuery("data platform").contains("dataplatform"));
assertEquals(SearchTemplateSynonyms.wholeQuery(" "), List.of());

SearchTemplateQueryShape phrase = SearchTemplateQueryShape.of("prod orders");
assertFalse(phrase.synonyms.isEmpty());
assertTrue(phrase.exactSynonyms.isEmpty());
assertEquals(
SearchTemplateQueryShape.of("Data Platform").exactSynonyms, List.of("dataplatform"));
// A body may branch on the list at write time.
SearchTemplateMustache.validationContext(
"{{#exactSynonyms}}{ \"term\": { \"name\": \"{{synonym}}\" } }{{/exactSynonyms}}");
}

@Test
public void testEmptySynonymListRendersNoClauses() {
String template = "{{#synonymQueries}}{{.}}{{/synonymQueries}}";
Expand Down
Loading