Search in sources :

Example 1 with Option

use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method testToXContent.

public void testToXContent() throws IOException {
    Map<String, Set<CharSequence>> contexts = Collections.singletonMap("key", Collections.singleton("value"));
    CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(1, new Text("someText"), 1.3f, contexts);
    BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
    assertEquals("{\"text\":\"someText\",\"score\":1.3,\"contexts\":{\"key\":[\"value\"]}}", xContent.utf8ToString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Set(java.util.Set) HashSet(java.util.HashSet) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Example 2 with Option

use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method createTestItem.

public static Option createTestItem() {
    Text text = new Text(randomAsciiOfLengthBetween(5, 15));
    int docId = randomInt();
    int numberOfContexts = randomIntBetween(0, 3);
    Map<String, Set<CharSequence>> contexts = new HashMap<>();
    for (int i = 0; i < numberOfContexts; i++) {
        int numberOfValues = randomIntBetween(0, 3);
        Set<CharSequence> values = new HashSet<>();
        for (int v = 0; v < numberOfValues; v++) {
            values.add(randomAsciiOfLengthBetween(5, 15));
        }
        contexts.put(randomAsciiOfLengthBetween(5, 15), values);
    }
    SearchHit hit = null;
    float score = randomFloat();
    if (randomBoolean()) {
        hit = SearchHitTests.createTestItem(false);
        score = hit.getScore();
    }
    Option option = new CompletionSuggestion.Entry.Option(docId, text, score, contexts);
    option.setHit(hit);
    return option;
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Set(java.util.Set) HashSet(java.util.HashSet) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) HashSet(java.util.HashSet)

Example 3 with Option

use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsed = Option.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getHighlighted(), parsed.getHighlighted());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.collateMatch(), parsed.collateMatch());
    assertEquals(option.getContexts(), parsed.getContexts());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

Option (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 Text (org.elasticsearch.common.text.Text)2 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)2 HashMap (java.util.HashMap)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 SearchHit (org.elasticsearch.search.SearchHit)1