0

I want to implement a search query that searches the database for a text. But I always get back an empty result, although the text appears in a DataSet.

  public List<Object> getSearchVorgangResult( final int mandantId, final String searchValue, final List<Integer> projektIds,
                                              final Field field,
                                              final boolean isGrossKlein )
  {
    final EntityManager em = getEntityManager();

    final FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager( em );

    // create native Lucene query unsing the query DSL
    // alternatively you can write the Lucene query using the Lucene query parser
    // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
    final QueryBuilder qb = fullTextEntityManager.getSearchFactory()
        .buildQueryBuilder().forEntity( Vorgang.class ).get();

    final List<String> fieldNames = new ArrayList<>();
    System.out.println( field );
    if ( field == null )
    {
      for ( final Field classField : Vorgang.class.getDeclaredFields() )
      {
        fieldNames.add( classField.getName() );
      }
    }
    else
    {
      fieldNames.add( field.getName() );
    }
    String[] fields = new String[fieldNames.size()];
    fields = fieldNames.toArray( fields );

    final org.apache.lucene.search.Query luceneQuery = qb
        .keyword()
        .onFields( fields ).ignoreAnalyzer()
        .matching( searchValue )
        .createQuery();

    // wrap Lucene query in a javax.persistence.Query
    final javax.persistence.Query jpaQuery =
        fullTextEntityManager.createFullTextQuery( luceneQuery, Vorgang.class );

    // execute search
    return jpaQuery.getResultList();
  }

I have no idea why the result is always empty.

Sanne
  • 6,027
  • 19
  • 34
espirio
  • 189
  • 14
  • It seems to me that you create a query, but never execute it. Directory dir = new RAMDirectory(); IndexReader idxReader = new IndexReader(dir); idxSearcher idxSearcher = new IndexSearcher(idxReader) Query q = new TermQuery(new Term(“field”, “value”)); idxSearcher.search(q); From here: [link](http://opensourceconnections.com/blog/2014/01/20/build-your-own-custom-lucene-query-and-scorer/) – Setup Jun 30 '15 at 11:00
  • Is there any reason that you are using ignoreAnalyzer()? Can you try to remove ignoreAnalyzer() and try .matching( searchValue.toUpperCase() ) or .matching( "%SOMEVALUETHATEXISTS%" ). Also can you check if index is being properly – Sujit Chaitanya Jun 30 '15 at 11:02
  • If i remove ignoreAnalyzer(), I get the following error: org.hibernate.search.exception.EmptyQueryException: HSEARCH000146: The query string ' ' applied on field 'anfrageEingangAm' has no meaningful tokens to be matched. Validate the query input against the Analyzer applied on this field. @SujitChaitanya – espirio Jun 30 '15 at 11:58
  • What do you mean by : "that searches the database for a text" ? have you indexed the text ? – Bilal BBB Jun 30 '15 at 16:00
  • Also i have added: "fullTextEntityManager.createIndexer( Vorgang.class ).startAndWait();" Now I get more than 900 results. That's too many. @BoutayaBilal – espirio Jul 01 '15 at 06:10
  • @dtr : that means that index was empty, "fullTextEntityManager.createIndexer( Vorgang.class ).startAndWait();" is used to index manually documents. Read hibernate search documentation, it will help a lot, thus, you can automate indexation (some annotations), use LUKE to know what the index contain. – Bilal BBB Jul 01 '15 at 12:26
  • @dtr : 900 results, it depends on your "searchValue" value. It means that 900 results much your query. – Bilal BBB Jul 01 '15 at 12:29
  • @BoutayaBilal : My search for string fields is now working, but the search to integer fields is not working. Do you have any idea? It does not work when I use an integer as Searchvalue. The use of a FieldBridge also not working. – espirio Jul 02 '15 at 10:52

1 Answers1

2

I answer your question about integer fields or in general numeric fields.

You have to use a hibernate search annotation that allows you to index integers or any numeric fields :

@FieldBridge(impl=IntegerNumericFieldBridge.class) 
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO) @NumericField
private Integer integerValue;

I Don't know if @NumericField annotations is optional or not. Analyze.No because you don't need to analyze numeric fields.

To construct your query, you need to use the lucene NumericRangeQuery like this :

NumericRangeQuery<Integer> integerQuery = NumericRangeQuery.newIntRange(yourIntegerFieldName, minValue, maxValue,  minInclusive, maxInclusive);

if you need to combine many queries, you have to use another object which is lucene BooleanQuery :

BooleanQuery luceneBooleanQuery = new BooleanQuery(); // creates a boolean query

luceneBooleanQuery.add(integerQuery, BooleanClause.Occur.MUST); // adds integerQuery to luceneBooleanQuery. You can add many queries

finally you can wrap lucene query in your jpa query

javax.persistence.Query jpaQuery =
        fullTextEntityManager.createFullTextQuery(luceneBooleanQuery, Vorgang.class );

I wish it can help you.

Bilal BBB
  • 1,154
  • 13
  • 20
  • Thanks for your help :) The problem I've found is that all the fields are set to null (except string). I do not know why that happens. Here are the results for the string field search: "Resultobject:Vorgang DomainObject: id=763 bezeichnung=Ich habe ein Punkt. ebene=null tableRowNr=null". In the database are "ebene" and "tableRowNr" not null. – espirio Jul 02 '15 at 13:20
  • what do you mean by fields ? Install LUKE to see what does your index contain, it is very helpful – Bilal BBB Jul 02 '15 at 13:22
  • I get in Luke an exception. ___ Invalid directory at the location, check console for more information. Last exception: org.apache.lucene.index.IndexFormatTooNewException: Format version is not supported (resource: MMapIndexInput(path="C:\workspaceLea (mars)\LeaServiceImplementation\luceneIndexes\com.isp.lea.domain.Vorgang\segments.gen")): -3 (needs to be between -2 and -2) – espirio Jul 02 '15 at 13:42
  • So eben and tableRowNrare not at present in the index. How can this happen? But it lacks a lot of fields .... – espirio Jul 02 '15 at 13:55
  • try this http://stackoverflow.com/questions/20711604/luke-says-my-lucene-index-directory-is-invalid – Bilal BBB Jul 02 '15 at 13:55
  • I have already made. Luke works. That's why I know that many fields are missing. – espirio Jul 02 '15 at 13:58
  • what is exactly the problem ? – Bilal BBB Jul 02 '15 at 14:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82212/discussion-between-boutaya-bilal-and-dtr). – Bilal BBB Jul 02 '15 at 14:06
  • The objects do not possess some fields. For example, "level" and "tableRowNr" does not appear in Luke. – espirio Jul 02 '15 at 14:08