Case insensitive with Hibernate-Search?

Is there an easy way to make Hibernate Search index all of its lowercase values? Instead of the default mixed case.

I am using @Field annotation. But I can't seem to customize some set of application level

+3


source to share


3 answers


Fool that I am! The StandardAnalyzer class is already lowercase indexed. It's just a matter of defining your search terms in lowercase. I assumed the request would do this.



However, if a different analyzer was used for analysis, it can be set using the hibernate.search.analyzer property .

+3


source


The following definition, term separation, common term removal, and many other advanced language processing functions are applied by the parser.

Typically, you have to process user input designed to match indexed strings with the same Parser used when indexing; setting hibernate.search.analyzer sets a standard (global) analyzer, but you can configure it for each index, for each entity type, for each field, and even for different entity instances.

For example, it is useful to have language specific parsing, so handle Chinese descriptions with Chinese specific routines, Italian descriptions with Italian tokenizers.



The default parser is suitable for most use cases and the lower and separator terms are in spaces.

Consider also that when using the Lucene Queryparser API is asking you for the appropriate parser.

When using the Hibernate Search QueryBuilder, it tries to apply the correct parser for each field; see also http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html_single/#search-query-querydsl .

0


source


There are several ways to make sorting insensitive on a string type field.

1.First Way adds @Fields annotation to the field / property of the object. how

@fields ({@Field (index = Index.YES, parse = Analyze.YES, store = Store.YES), @Field (index = Index.YES, name = "nameSort", parser = @Analyzer (impl = KeywordAnalyzer. class), store = Store.YES)})

personal string name;

suppose you have a name property with a custom parser and sort by it. so this is not possible, then you can add a new index to a field called Sort apply sort on that field. you have to apply the Keyword Analyzer class because it is not a tokenized field and by default apply the string factory class on the field.

2. The second way is to implement a comparison class when sorting, for example

@Override
public FieldComparator newComparator(String field, int numHits, int sortPos, boolean reversed) throws IOException {
    return new StringValComparator(numHits, field);
}

      

Make one class extending the FieldComparatorSource class and implement the above method.

Created a new class name using StringValComparator and implements FieldComparator and implement the following method

StringValComparator class extends FieldComparator {

private String[] values;
private String[] currentReaderValues;
private final String field;
private String bottom;

StringValComparator(int numHits, String field) {
  values = new String[numHits];
  this.field = field;
}

@Override
public int compare(int slot1, int slot2) {
  final String val1 = values[slot1];
  final String val2 = values[slot2];
  if (val1 == null) {
    if (val2 == null) {
      return 0;
    }
    return -1;
  } else if (val2 == null) {
    return 1;
  }

  return val1.toLowerCase().compareTo(val2.toLowerCase());
}

@Override
public int compareBottom(int doc) {
  final String val2 = currentReaderValues[doc];
  if (bottom == null) {
    if (val2 == null) {
      return 0;
    }
    return -1;
  } else if (val2 == null) {
    return 1;
  }
  return bottom.toLowerCase().compareTo(val2.toLowerCase());
}

@Override
public void copy(int slot, int doc) {
  values[slot] = currentReaderValues[doc];
}

@Override
public void setNextReader(IndexReader reader, int docBase) throws IOException {
  currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}

@Override
public void setBottom(final int bottom) {
  this.bottom = values[bottom];
}

@Override
public String value(int slot) {
    return values[slot];
}

      

}

Apply sort by fields like

new SortField ("name", new StringCaseInsensitiveComparator (), true);

0


source







All Articles