Skip to content

Completion types with multi-fields support #34081

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

Merged
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
@@ -0,0 +1,299 @@

---
"Search by suggestion and by keyword sub-field should work":

- skip:
version: " - 6.99.99"
reason: "Search by suggestion with multi-fields was introduced 7.0.0"

- do:
indices.create:
index: completion_with_sub_keyword
body:
mappings:
test:
"properties":
"suggest_1":
"type" : "completion"
"fields":
"text_raw":
"type" : "keyword"

- do:
index:
index: completion_with_sub_keyword
type: test
id: 1
body:
suggest_1: "bar"

- do:
index:
index: completion_with_sub_keyword
type: test
id: 2
body:
suggest_1: "baz"

- do:
indices.refresh: {}

- do:
search:
index: completion_with_sub_keyword
body:
suggest:
result:
text: "b"
completion:
field: suggest_1

- length: { suggest.result: 1 }
- length: { suggest.result.0.options: 2 }


- do:
search:
index: completion_with_sub_keyword
body:
query: { term: { suggest_1.text_raw: "bar" }}

- match: { hits.total: 1 }



---
"Search by suggestion on sub field should work":

- skip:
version: " - 6.99.99"
reason: "Search by suggestion with multi-fields was introduced 7.0.0"

- do:
indices.create:
index: completion_with_sub_completion
body:
mappings:
test:
"properties":
"suggest_1":
"type": "completion"
"fields":
"suggest_2":
"type": "completion"

- do:
index:
index: completion_with_sub_completion
type: test
id: 1
body:
suggest_1: "bar"

- do:
index:
index: completion_with_sub_completion
type: test
id: 2
body:
suggest_1: "baz"

- do:
indices.refresh: {}

- do:
search:
index: completion_with_sub_completion
body:
suggest:
result:
text: "b"
completion:
field: suggest_1.suggest_2

- length: { suggest.result: 1 }
- length: { suggest.result.0.options: 2 }

---
"Search by suggestion on sub field with context should work":

- skip:
version: " - 6.99.99"
reason: "Search by suggestion with multi-fields was introduced 7.0.0"

- do:
indices.create:
index: completion_with_context
body:
mappings:
test:
"properties":
"suggest_1":
"type": "completion"
"contexts":
-
"name": "color"
"type": "category"
"fields":
"suggest_2":
"type": "completion"
"contexts":
-
"name": "color"
"type": "category"


- do:
index:
index: completion_with_context
type: test
id: 1
body:
suggest_1:
input: "foo red"
contexts:
color: "red"

- do:
index:
index: completion_with_context
type: test
id: 2
body:
suggest_1:
input: "foo blue"
contexts:
color: "blue"

- do:
indices.refresh: {}

- do:
search:
index: completion_with_context
body:
suggest:
result:
prefix: "foo"
completion:
field: suggest_1.suggest_2
contexts:
color: "red"

- length: { suggest.result: 1 }
- length: { suggest.result.0.options: 1 }
- match: { suggest.result.0.options.0.text: "foo red" }


---
"Search by suggestion on sub field with weight should work":

- skip:
version: " - 6.99.99"
reason: "Search by suggestion with multi-fields was introduced 7.0.0"

- do:
indices.create:
index: completion_with_weight
body:
mappings:
test:
"properties":
"suggest_1":
"type": "completion"
"fields":
"suggest_2":
"type": "completion"

- do:
index:
index: completion_with_weight
type: test
id: 1
body:
suggest_1:
input: "bar"
weight: 2

- do:
index:
index: completion_with_weight
type: test
id: 2
body:
suggest_1:
input: "baz"
weight: 3

- do:
indices.refresh: {}

- do:
search:
index: completion_with_weight
body:
suggest:
result:
text: "b"
completion:
field: suggest_1.suggest_2

- length: { suggest.result: 1 }
- length: { suggest.result.0.options: 2 }
- match: { suggest.result.0.options.0.text: "baz" }
- match: { suggest.result.0.options.1.text: "bar" }

---
"Search by suggestion on geofield-hash on sub field should work":

- skip:
version: " - 6.99.99"
reason: "Search by suggestion with multi-fields was introduced 7.0.0"

- do:
indices.create:
index: geofield_with_completion
body:
mappings:
test:
"properties":
"geofield":
"type": "geo_point"
"fields":
"suggest_1":
"type": "completion"

- do:
index:
index: geofield_with_completion
type: test
id: 1
body:
geofield: "hgjhrwysvqw7"
#41.12,-72.34,12

- do:
index:
index: geofield_with_completion
type: test
id: 1
body:
geofield: "hgm4psywmkn7"
#41.12,-71.34,12

- do:
indices.refresh: {}

- do:
search:
index: geofield_with_completion
body:
suggest:
result:
prefix: "hgm"
completion:
field: geofield.suggest_1


- length: { suggest.result: 1 }
- length: { suggest.result.0.options: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ public void parse(ParseContext context) throws IOException {
Token token = parser.currentToken();
Map<String, CompletionInputMetaData> inputMap = new HashMap<>(1);

// ignore null values
if (token == Token.VALUE_NULL) {
if (context.externalValueSet()) {
inputMap = getInputMapFromExternalValue(context);
} else if (token == Token.VALUE_NULL) { // ignore null values
return;
} else if (token == Token.START_ARRAY) {
while ((token = parser.nextToken()) != Token.END_ARRAY) {
Expand Down Expand Up @@ -471,12 +472,33 @@ public void parse(ParseContext context) throws IOException {
context.doc().add(new SuggestField(fieldType().name(), input, metaData.weight));
}
}

List<IndexableField> fields = new ArrayList<>(1);
createFieldNamesField(context, fields);
for (IndexableField field : fields) {
context.doc().add(field);
}
multiFields.parse(this, context);

for (CompletionInputMetaData metaData: inputMap.values()) {
ParseContext externalValueContext = context.createExternalValueContext(metaData);
multiFields.parse(this, externalValueContext);
}
}

private Map<String, CompletionInputMetaData> getInputMapFromExternalValue(ParseContext context) {
Map<String, CompletionInputMetaData> inputMap;
if (isExternalValueOfClass(context, CompletionInputMetaData.class)) {
CompletionInputMetaData inputAndMeta = (CompletionInputMetaData) context.externalValue();
inputMap = Collections.singletonMap(inputAndMeta.input, inputAndMeta);
} else {
String fieldName = context.externalValue().toString();
inputMap = Collections.singletonMap(fieldName, new CompletionInputMetaData(fieldName, Collections.emptyMap(), 1));
}
return inputMap;
}

private boolean isExternalValueOfClass(ParseContext context, Class<?> clazz) {
return context.externalValue().getClass().equals(clazz);
}

/**
Expand All @@ -487,7 +509,7 @@ public void parse(ParseContext context) throws IOException {
private void parse(ParseContext parseContext, Token token, XContentParser parser, Map<String, CompletionInputMetaData> inputMap) throws IOException {
String currentFieldName = null;
if (token == Token.VALUE_STRING) {
inputMap.put(parser.text(), new CompletionInputMetaData(Collections.<String, Set<CharSequence>>emptyMap(), 1));
inputMap.put(parser.text(), new CompletionInputMetaData(parser.text(), Collections.emptyMap(), 1));
} else if (token == Token.START_OBJECT) {
Set<String> inputs = new HashSet<>();
int weight = 1;
Expand Down Expand Up @@ -561,7 +583,7 @@ private void parse(ParseContext parseContext, Token token, XContentParser parser
}
for (String input : inputs) {
if (inputMap.containsKey(input) == false || inputMap.get(input).weight < weight) {
inputMap.put(input, new CompletionInputMetaData(contextsMap, weight));
inputMap.put(input, new CompletionInputMetaData(input, contextsMap, weight));
}
}
} else {
Expand All @@ -570,13 +592,20 @@ private void parse(ParseContext parseContext, Token token, XContentParser parser
}

static class CompletionInputMetaData {
public final String input;
public final Map<String, Set<CharSequence>> contexts;
public final int weight;

CompletionInputMetaData(Map<String, Set<CharSequence>> contexts, int weight) {
CompletionInputMetaData(String input, Map<String, Set<CharSequence>> contexts, int weight) {
this.input = input;
this.contexts = contexts;
this.weight = weight;
}

@Override
public String toString() {
return input;
}
}

@Override
Expand Down
Loading