.sentence(terms.iterator().next().toLowerCase()).createQuery();
private void addDisplayFilterExact(QueryBuilder qb, BooleanJunction<?> bool, ValueSet.ConceptSetFilterComponent nextFilter) { bool.must(qb.phrase().onField("myDisplay").sentence(nextFilter.getValue()).createQuery()); }
private void addDisplayFilterInexact(QueryBuilder qb, BooleanJunction<?> bool, ValueSet.ConceptSetFilterComponent nextFilter) { Query textQuery = qb .phrase() .withSlop(2) .onField("myDisplay").boostedTo(4.0f) .andField("myDisplayEdgeNGram").boostedTo(2.0f) // .andField("myDisplayNGram").boostedTo(1.0f) // .andField("myDisplayPhonetic").boostedTo(0.5f) .sentence(nextFilter.getValue().toLowerCase()).createQuery(); bool.must(textQuery); }
.andField("myContentTextNGram").boostedTo(1.0f) .andField("myContentTextPhonetic").boostedTo(0.5f) .sentence(theText.toLowerCase()).createQuery();
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldPhraseWithAttributesOnFields(Map<String, String> eqSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } return combinedQuery; }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addMustPhraseWithAttributesOnFields(Map<String, String> eqSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.must(luceneQuery); } return combinedQuery; }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addMustPhraseWithMultiFieldOnAttribute(Map<String, Set<String>> attributeForMultipleFieldsMap, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { PhraseContext queryParse = qb.phrase(); for(String value :attributeForMultipleFieldsMap.keySet()){ Set<String> attributes = attributeForMultipleFieldsMap.get(value); PhraseMatchingContext pharseMatchingContext = null; for(String attribute : attributes){ if(!attribute.isEmpty()){ if(pharseMatchingContext == null){ pharseMatchingContext = queryParse.onField(attribute); } else{ pharseMatchingContext.andField(attribute); } } } if(pharseMatchingContext != null){ Query luceneQuery = pharseMatchingContext.sentence(value).createQuery(); combinedQuery.must(luceneQuery); } } return combinedQuery; }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldWMustPhraseWithAttributesOnFields(Map<String, String> eqSentenceOnField,Map<String, String> eqMustSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } combinedQuery.must(combinedQuery.createQuery()); //combinedQuery.setMinimumShouldMatch(2); for(String field : eqMustSentenceOnField.keySet()){ String value = eqMustSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.must(luceneQuery); //must(luceneQuery); } return combinedQuery; }
private void addDisplayFilterExact(QueryBuilder qb, BooleanJunction<?> bool, ValueSet.ConceptSetFilterComponent nextFilter) { bool.must(qb.phrase().onField("myDisplay").sentence(nextFilter.getValue()).createQuery()); }
private void addDisplayFilterInexact(QueryBuilder qb, BooleanJunction<?> bool, ValueSet.ConceptSetFilterComponent nextFilter) { Query textQuery = qb .phrase() .withSlop(2) .onField("myDisplay").boostedTo(4.0f) .andField("myDisplayEdgeNGram").boostedTo(2.0f) // .andField("myDisplayNGram").boostedTo(1.0f) // .andField("myDisplayPhonetic").boostedTo(0.5f) .sentence(nextFilter.getValue().toLowerCase()).createQuery(); bool.must(textQuery); }
// New DSL based query composition //org.hibernate.search.query.dsl SearchFactory searchFactory = fullTextSession.getSearchFactory(); QueryBuilder buildQuery = searchFactory.buildQueryBuilder().forEntity(MasterDiagnosis.class).get(); PhraseContext keyword = buildQuery.phrase(); keyword.withSlop(3); //WildcardContext wildcard = keyword.wildcard(); String[] searchfields = MasterDiagnosis.getSearchfields(); PhraseMatchingContext onFields = keyword.onField(searchfields[0]); for (int i = 1; i < searchfields.length; i++) onFields.andField(searchfields[i]); PhraseTermination matching = onFields.sentence(lowerCasedSearchTerm); Query query = matching.createQuery(); // Convert the Search Query into something that provides results: Specify Compound again to be future proof
public void testPhraseSentence() { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().phrase() .onField("blurb").sentence("Eats grass").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(person2); person4 = new Person(); person4.setName("Another goat"); person4.setBlurb("Eats grass and drinks water."); cache.put("anotherKey", person4); found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(person2); assert found.contains(person4); }
public void testPhraseSentenceForNonAnalyzedEntries() { loadNumericTypes(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().phrase() .onField("name").sentence("Some string").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(0, found.size()); NumericType type4 = new NumericType(45,50); type4.setName("Some string"); cache.put("otherKey", type4); found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(type4); }
@Test public void testPhraseQueryWithStopWords() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .phrase() .onField( "mythology" ) .sentence( "colder and whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).hasResultSize( 1 ); }
public void testPhraseWithSlopWithoutAnalyzer() { loadNumericTypes(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(NumericType.class).get().phrase().withSlop(1) .onField("name").sentence("Some string").createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(0, found.size()); NumericType type = new NumericType(10, 60); type.setName("Some string"); cache.put("otherKey", type); found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(type); NumericType type1 = new NumericType(20, 60); type1.setName("Some other string"); cache.put("otherKey1", type1); found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(type); }
@Test @TestForIssue(jiraKey = "HSEARCH-2479") public void testPhraseQueryTermCreation() throws Exception { String testCaseText = "Test the Test test of your test test to test test test of test and Test budgeting."; final QueryBuilder monthQb = helper.queryBuilder( Month.class ); Query query = monthQb .phrase() .onField( "mythology" ) .sentence( testCaseText ) .createQuery(); helper.assertThat( query ).from( Month.class ).as( "test term ordering" ).hasResultSize( 0 ); }
@Test public void searchOnEmptyIndex() { cache.put("1", new Book("Seam in Action", "Dan Allen", "Manning")); cache.put("2", new Book("Hibernate Search in Action", "Emmanuel Bernard and John Griffin", "Manning")); cache.put("3", new Book("Megaprogramming Ruby", "Paolo Perrotta", "The Pragmatic Programmers")); SearchManager qf = Search.getSearchManager(cache); Query luceneQuery = qf.buildQueryBuilderForClass(Book.class) .get() .phrase() .onField("title") .sentence("in action") .createQuery(); List<?> list = qf.getQuery(luceneQuery).list(); assert list.size() == 2; }
@Override @SuppressWarnings("unchecked") protected void execute(FullTextSession fts) { long bookCount = ctx.bookIdCounter.get(); String phrase = PHRASES[(int) ( bookCount % PHRASES.length )]; Query q = fts.getSearchFactory() .buildQueryBuilder() .forEntity( Book.class ) .get() .phrase() .withSlop( 3 ) .onField( "summary" ) .sentence( phrase ) .createQuery(); List<Book> result = fts.createFullTextQuery( q, Book.class ).list(); if ( ctx.testContext.assertQueryResults ) { assertResult( result, phrase ); assertResultSize( result, phrase, bookCount ); } }
@Test public void testDSLPhraseQueryWithoutAnalyzer() { try ( Session session = openSession() ) { FullTextSession fullTextSession = Search.getFullTextSession( session ); final QueryBuilder queryBuilder = queryBuilder( fullTextSession ); Query query = queryBuilder .phrase() .onField( "signature" ) .sentence( "Gunnar Morling" ) .createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class ); String queryString = fullTextQuery.getQueryString(); assertJsonEquals( "{'query':{'match_phrase':{'signature':{'query':'Gunnar Morling'}}}}", queryString ); } }
@Test @TestForIssue(jiraKey = "HSEARCH-1074") public void testPhraseQueryWithNoTermsAfterAnalyzerApplication() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); // we use mythology_stem here as the default analyzer for Elasticsearch does not include a stopwords filter Query query = monthQb. phrase() .onField( "mythology_stem" ) .sentence( "and" ) .createQuery(); helper.assertThat( query ).from( Month.class ) .as( "there should be no results, since all terms are stop words" ) .matchesNone(); }