Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Remove lastChanged index
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 16, 2014
1 parent d9c1e84 commit ba0316c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 104 deletions.

This file was deleted.

Expand Up @@ -17,7 +17,6 @@ public interface IndexFieldLabels {
public static final String DOCUMENT_ID_FIELD = "documentId";
public static final String LOCALE_ID_FIELD = "locale";
public static final String CONTENT_STATE_FIELD = "state";
public static final String LAST_CHANGED_FIELD = "lastChanged";

public static final String TF_CONTENT = "textFlow.content-nocase";
public static final String CONTENT = "content-nocase";
Expand Down
Expand Up @@ -23,13 +23,11 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -44,8 +42,6 @@
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

Expand All @@ -55,7 +51,6 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NaturalId;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.AnalyzerDiscriminator;
Expand All @@ -69,7 +64,6 @@
import org.zanata.common.HasContents;
import org.zanata.common.LocaleId;
import org.zanata.hibernate.search.ContentStateBridge;
import org.zanata.hibernate.search.DateBridge;
import org.zanata.hibernate.search.IndexFieldLabels;
import org.zanata.hibernate.search.LocaleIdBridge;
import org.zanata.hibernate.search.StringListBridge;
Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
Expand All @@ -38,14 +37,9 @@
import javax.persistence.TemporalType;
import javax.persistence.Version;

import com.google.common.annotations.VisibleForTesting;

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.hibernate.search.DateBridge;
import com.google.common.annotations.VisibleForTesting;

@EntityListeners({ModelEntityBase.EntityListener.class})
@MappedSuperclass
Expand Down Expand Up @@ -93,8 +87,6 @@ public void setCreationDate(Date creationDate) {
// NB: also used in HSimpleComment
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
@Field(analyze = Analyze.NO)
@FieldBridge(impl = DateBridge.class)
public Date getLastChanged() {
return lastChanged;
}
Expand Down
Expand Up @@ -39,8 +39,6 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.Version;
Expand Down Expand Up @@ -90,8 +88,6 @@ public class TranslationMemoryServiceImpl implements TranslationMemoryService {

private static final Version LUCENE_VERSION = Version.LUCENE_29;

private boolean USE_LUCENE_SORT = true;

@Override
public TransMemoryDetails
getTransMemoryDetail(HLocale hLocale, HTextFlow tf) {
Expand Down Expand Up @@ -139,21 +135,19 @@ public Optional<HTextFlowTarget> searchBestMatchTransMemory(
findMatchingTranslation(targetLocaleId, sourceLocaleId, query,
null, HTextFlowTarget.class);

System.out.println("after filter:" + matches.size());

if (matches.isEmpty()) {
return Optional.<HTextFlowTarget> absent();
}

if (!USE_LUCENE_SORT) {
Collections.sort(matches, new Comparator<Object[]>() {
@Override
public int compare(Object[] o1, Object[] o2) {
HTextFlowTarget target1 = (HTextFlowTarget) o1[1];
HTextFlowTarget target2 = (HTextFlowTarget) o2[1];
return target2.getLastChanged().compareTo(
target1.getLastChanged());
}
});
}
Collections.sort(matches, new Comparator<Object[]>() {
@Override
public int compare(Object[] o1, Object[] o2) {
return ((HTextFlowTarget) o2[1]).getLastChanged().compareTo(
((HTextFlowTarget) o1[1]).getLastChanged());
}
});

return Optional.of((HTextFlowTarget) matches.get(0)[1]);
}
Expand Down Expand Up @@ -258,6 +252,9 @@ private TransMemoryQuery buildTMQuery(HTextFlow textFlow,
* @param maxResults
* @return
*/
private final ValidTargetFilterPredicate predicate =
new ValidTargetFilterPredicate();

private List<Object[]> findMatchingTranslation(LocaleId targetLocaleId,
LocaleId sourceLocaleId, TransMemoryQuery transMemoryQuery,
Integer maxResults, Class<?>... entities) {
Expand All @@ -266,9 +263,9 @@ private List<Object[]> findMatchingTranslation(LocaleId targetLocaleId,
getSearchResult(transMemoryQuery, sourceLocaleId,
targetLocaleId, maxResults, entities);

System.out.println("before filter:" + matches.size());
// filter out invalid target
return Lists.newArrayList(Collections2.filter(matches,
new ValidTargetFilterPredicate()));
return Lists.newArrayList(Collections2.filter(matches, predicate));

} catch (ParseException e) {
if (transMemoryQuery.getSearchType() == HasSearchType.SearchType.RAW) {
Expand Down Expand Up @@ -534,22 +531,12 @@ private List<Object[]> getSearchResult(TransMemoryQuery query,
FullTextQuery ftQuery =
entityManager.createFullTextQuery(textQuery, entities);

if(USE_LUCENE_SORT) {
// sort desc by lastChanged of HTextFlowTarget
ftQuery.setSort(new Sort(
new SortField(IndexFieldLabels.LAST_CHANGED_FIELD,
SortField.STRING, true)));
}

ftQuery.setProjection(FullTextQuery.SCORE, FullTextQuery.THIS);

List<Object[]> matches;
if (maxResult != null) {
matches = ftQuery.setMaxResults(maxResult).getResultList();
} else {
matches = ftQuery.getResultList();
return ftQuery.setMaxResults(maxResult).getResultList();
}
return matches;
return ftQuery.getResultList();
}

/**
Expand Down Expand Up @@ -783,7 +770,7 @@ public boolean apply(TransMemoryResultItem tmResult) {
}

// filter out invalid targets(obsolete project or version)
private static class ValidTargetFilterPredicate implements
private static final class ValidTargetFilterPredicate implements
Predicate<Object[]> {
@Override
public boolean apply(Object[] input) {
Expand Down

0 comments on commit ba0316c

Please sign in to comment.