List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.hibernate.search.test.dsl.BoostDSLTest.java
License:LGPL
@Test public void testBoostOnNumericQuery() { QueryBuilder qb = helper.queryBuilder(Coffee.class); Query query = qb.bool().should(qb.keyword().onField("name").boostedTo(40f).matching("Kazaar").createQuery()) .should(qb.keyword().onField("intensity").boostedTo(1f).matching(11).createQuery()).createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Kazaar", "Dharkan"); query = qb.bool().should(qb.keyword().onField("name").boostedTo(1f).matching("Kazaar").createQuery()) .should(qb.keyword().onField("intensity").boostedTo(40f).matching(11).createQuery()).createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Dharkan", "Kazaar"); }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2678") public void testSimpleQueryString() { QueryBuilder qb = getCoffeeQueryBuilder(); Query query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("balanced arabica").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesExactlyIds("Dulso do Brasil", "Kazaar", "Livanto"); query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("-balanced arabica").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesExactlyIds("Bukeela ka Ethiopia", "Linizio Lungo", "Volluto"); query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("powerful \"fruity note\"").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesExactlyIds("Ristretto"); query = qb.simpleQueryString().onFields("name", "summary", "description").matching("sweet robust") .createQuery();//from w w w . ja va 2s .co m helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesExactlyIds("Caramelito", "Dulso do Brasil", "Roma", "Volluto"); }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2678") public void testBoost() { QueryBuilder qb = getCoffeeQueryBuilder(); Query query = qb.simpleQueryString().onFields("name", "summary").boostedTo(5f).andField("description") .withAndAsDefaultOperator().matching("fruity arabicas south american").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Decaffeinato", "Ristretto"); query = qb.simpleQueryString().onFields("name", "summary").andField("description").boostedTo(10f) .withAndAsDefaultOperator().matching("fruity arabicas south american").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Ristretto", "Decaffeinato"); }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2678") public void testFuzzy() { QueryBuilder qb = getCoffeeQueryBuilder(); Query query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("fruity arabica~2").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesExactlyIds("Decaffeinato", "Ristretto", "Rosabaya de Colombia", "Volluto"); }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2678") public void testAnalyzer() { QueryBuilder qb = getBookQueryBuilder(); Query query = qb.simpleQueryString().onFields("title", "author").withAndAsDefaultOperator() .matching("Molire").createQuery(); helper.assertThat(query).from(Book.class).sort(new Sort(new SortField("title_sort", SortField.Type.STRING))) .matchesExactlyIds("Le Grand Molire illustr", "Tartuffe"); query = qb.simpleQueryString().onFields("title", "author").withAndAsDefaultOperator().matching("deplacait") .createQuery();/*from www.ja va2 s .c om*/ helper.assertThat(query).from(Book.class).sort(new Sort(new SortField("title_sort", SortField.Type.STRING))) .matchesExactlyIds("Le chat qui dplaait des montagnes"); qb = sfHolder.getSearchFactory().buildQueryBuilder().forEntity(Book.class) .overridesForField("author", "titleAnalyzer").get(); query = qb.simpleQueryString().onFields("title", "author").withAndAsDefaultOperator().matching("Molire") .createQuery(); helper.assertThat(query).from(Book.class).sort(new Sort(new SortField("title_sort", SortField.Type.STRING))) .matchesExactlyIds("Dom Garcie de Navarre", "Le Grand Molire illustr"); }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2700") public void testEmptyQueryString() { QueryBuilder qb = getCoffeeQueryBuilder(); Query query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesNone();//from w w w .j ava2 s. co m }
From source file:org.hibernate.search.test.dsl.SimpleQueryStringDSLTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2700") public void testBlankQueryString() { QueryBuilder qb = getCoffeeQueryBuilder(); Query query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching(" ").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesNone();/*from w w w .j a v a 2 s . c o m*/ query = qb.simpleQueryString().onFields("name", "summary", "description").withAndAsDefaultOperator() .matching("() (())").createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(new SortField(Coffee.NAME_SORT, Type.STRING))) .matchesNone(); }
From source file:org.hibernate.search.test.embedded.sorting.EmbeddedSortableIdFieldTest.java
License:LGPL
@Test public void testEntityCanSortOnId() { try (Session session = openSession()) { FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction transaction = fullTextSession.beginTransaction(); Sort sort = new Sort(new SortField("id", SortField.Type.STRING)); QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder() .forEntity(Villain.class).get(); Query q = queryBuilder.keyword().onField("name").matching(LEX).createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(q, Villain.class); fullTextQuery.setSort(sort);/* www .j a v a 2 s .co m*/ List list = fullTextQuery.list(); assertThat(list).hasSize(1); Villain actual = (Villain) list.get(0); assertThat(actual.getName()).isEqualTo(LEX); transaction.commit(); } }
From source file:org.hibernate.search.test.embedded.sorting.EmbeddedSortableIdFieldTest.java
License:LGPL
@Test public void testSortingOnSortableFieldIncludedByIndexEmbedded() { try (Session session = openSession()) { FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction transaction = fullTextSession.beginTransaction(); // This should be of type Integer Sort sort = new Sort(new SortField("sortableVillain.id", SortField.Type.STRING)); QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Hero.class) .get();// w w w . ja v a2s.c o m Query q = queryBuilder.keyword().onField("villain.name").matching(LEX).createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(q, Hero.class); fullTextQuery.setSort(sort); List list = fullTextQuery.list(); assertThat(list).hasSize(1); Hero actual = (Hero) list.get(0); assertThat(actual.getSecretIdentity()).isEqualTo(CLARK); transaction.commit(); } }
From source file:org.hibernate.search.test.embedded.sorting.EmbeddedSortableIdFieldTest.java
License:LGPL
@Test @Category(ElasticsearchSupportInProgress.class) // HSEARCH-2398 Improve field name/type validation when querying the Elasticsearch backend public void testSortingOnSortableFieldNotIncludedByIndexEmbeddedException() { thrown.expect(SearchException.class); thrown.expectMessage("HSEARCH000301"); try (Session session = openSession()) { FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction transaction = fullTextSession.beginTransaction(); Sort sort = new Sort(new SortField("villain.id", SortField.Type.STRING)); QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Hero.class) .get();/*from ww w . j a va 2 s.c om*/ Query q = queryBuilder.keyword().onField("villain.name").matching(LEX).createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(q, Hero.class); fullTextQuery.setSort(sort); fullTextQuery.list(); transaction.commit(); } }