Search in sources :

Example 1 with Suggestions

use of com.enonic.xp.suggester.Suggestions in project xp by enonic.

the class SuggestionsFactoryTest method testEmpty.

@Test
public void testEmpty() {
    final Suggest suggest = new Suggest();
    final Suggestions suggestions = SuggestionsFactory.create(suggest);
    assertNotNull(suggestions);
    assertTrue(suggestions.isEmpty());
}
Also used : Suggestions(com.enonic.xp.suggester.Suggestions) Suggest(org.elasticsearch.search.suggest.Suggest) Test(org.junit.jupiter.api.Test)

Example 2 with Suggestions

use of com.enonic.xp.suggester.Suggestions in project xp by enonic.

the class SuggestionsFactoryTest method testTermSuggestion.

@Test
public void testTermSuggestion() {
    final Option option1 = Mockito.mock(Option.class);
    Mockito.when(option1.getText()).thenReturn(new Text("option1"));
    Mockito.when(option1.getScore()).thenReturn(1.0f);
    Mockito.when(option1.getFreq()).thenReturn(3);
    final Entry entry1 = Mockito.mock(Entry.class);
    Mockito.when(entry1.getLength()).thenReturn(2);
    Mockito.when(entry1.getOffset()).thenReturn(1);
    Mockito.when(entry1.getText()).thenReturn(new Text("entry1"));
    Mockito.when(entry1.getOptions()).thenReturn(List.of(option1));
    final TermSuggestion termSuggestion1 = Mockito.mock(TermSuggestion.class);
    Mockito.when(termSuggestion1.getName()).thenReturn("suggestion1");
    Mockito.when(termSuggestion1.getEntries()).thenReturn(List.of(entry1));
    final Suggest suggest = new Suggest(List.of(termSuggestion1));
    final Suggestions suggestions = SuggestionsFactory.create(suggest);
    assertNotNull(suggestions);
    assertFalse(suggestions.isEmpty());
    final com.enonic.xp.suggester.TermSuggestion resultSuggestion = (com.enonic.xp.suggester.TermSuggestion) suggestions.get("suggestion1");
    assertFalse(resultSuggestion.getEntries().isEmpty());
    final TermSuggestionEntry resultEntry = resultSuggestion.getEntries().get(0);
    assertEquals("entry1", resultEntry.getText());
    assertEquals(2L, resultEntry.getLength().longValue());
    assertEquals(1L, resultEntry.getOffset().longValue());
    final TermSuggestionOption resultOption = resultEntry.getOptions().get(0);
    assertEquals("option1", resultOption.getText());
    assertEquals(3L, resultOption.getFreq().longValue());
    assertEquals(1.0f, resultOption.getScore(), 1e-14);
}
Also used : TermSuggestionEntry(com.enonic.xp.suggester.TermSuggestionEntry) Text(org.elasticsearch.common.text.Text) TermSuggestionOption(com.enonic.xp.suggester.TermSuggestionOption) Suggest(org.elasticsearch.search.suggest.Suggest) Suggestions(com.enonic.xp.suggester.Suggestions) Entry(org.elasticsearch.search.suggest.term.TermSuggestion.Entry) TermSuggestionEntry(com.enonic.xp.suggester.TermSuggestionEntry) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option) TermSuggestionOption(com.enonic.xp.suggester.TermSuggestionOption) Test(org.junit.jupiter.api.Test)

Example 3 with Suggestions

use of com.enonic.xp.suggester.Suggestions in project xp by enonic.

the class FindNodesByQueryHandlerTest method testExample.

@Test
public void testExample() {
    final BucketAggregation duration = Aggregation.bucketAggregation("duration").buckets(Buckets.create().add(Bucket.create().key("1600").docCount(2).build()).add(Bucket.create().key("1400").docCount(1).build()).add(Bucket.create().key("1300").docCount(5).build()).build()).build();
    final Aggregations agg = Aggregations.create().add(Aggregation.bucketAggregation("urls").buckets(Buckets.create().add(Bucket.create().key("/site/draft/superhero/search").docCount(6762L).addAggregations(Aggregations.from(duration)).build()).add(Bucket.create().key("/site/draft/superhero").docCount(1245).addAggregations(Aggregations.from(duration)).build()).build()).build()).build();
    final Suggestions suggestions = Suggestions.create().add(TermSuggestion.create("termSuggestion").addSuggestionEntry(TermSuggestionEntry.create().text("text1").length(2).offset(1).addSuggestionOption(TermSuggestionOption.create().text("text1-1").score(1.0f).freq(2).build()).addSuggestionOption(TermSuggestionOption.create().text("text1-2").score(4.0f).freq(5).build()).build()).addSuggestionEntry(TermSuggestionEntry.create().text("text2").length(2).offset(2).addSuggestionOption(TermSuggestionOption.create().text("text2-1").score(2.3f).freq(4).build()).addSuggestionOption(TermSuggestionOption.create().text("text2-2").score(1.0f).freq(2).build()).build()).build()).build();
    Mockito.doReturn(FindNodesByQueryResult.create().totalHits(12902).addNodeHit(NodeHit.create().nodeId(NodeId.from("b186d24f-ac38-42ca-a6db-1c1bda6c6c26")).score(1.23f).highlight(HighlightedProperties.create().add(HighlightedProperty.create().name("property1").addFragment("fragment1").addFragment("fragment2").build()).build()).build()).addNodeHit(NodeHit.create().nodeId(NodeId.from("350ba4a6-589c-498b-8af0-f183850e1120")).score(1.40f).build()).aggregations(agg).suggestions(suggestions).build()).when(this.nodeService).findByQuery(Mockito.isA(NodeQuery.class));
    runScript("/lib/xp/examples/node/query.js");
}
Also used : Suggestions(com.enonic.xp.suggester.Suggestions) Aggregations(com.enonic.xp.aggregation.Aggregations) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Test(org.junit.jupiter.api.Test)

Aggregations

Suggestions (com.enonic.xp.suggester.Suggestions)3 Test (org.junit.jupiter.api.Test)3 Suggest (org.elasticsearch.search.suggest.Suggest)2 Aggregations (com.enonic.xp.aggregation.Aggregations)1 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 TermSuggestionEntry (com.enonic.xp.suggester.TermSuggestionEntry)1 TermSuggestionOption (com.enonic.xp.suggester.TermSuggestionOption)1 Text (org.elasticsearch.common.text.Text)1 TermSuggestion (org.elasticsearch.search.suggest.term.TermSuggestion)1 Entry (org.elasticsearch.search.suggest.term.TermSuggestion.Entry)1 Option (org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option)1