Skip to content

Commit

Permalink
Improve mapping of primaries/secondaries
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed May 22, 2024
1 parent 6899268 commit 7e31651
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
36 changes: 19 additions & 17 deletions src/ext/java/org/opentripplanner/ext/geocoder/LuceneIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
Expand Down Expand Up @@ -42,6 +41,7 @@
import org.apache.lucene.search.suggest.document.SuggestIndexSearcher;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.opentripplanner.ext.geocoder.StopCluster.Coordinate;
import org.opentripplanner.framework.collection.ListUtils;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.transit.model.framework.FeedScopedId;
Expand Down Expand Up @@ -96,8 +96,8 @@ public LuceneIndex(TransitService transitService) {
directoryWriter,
StopLocation.class,
stopLocation.getId().toString(),
stopLocation.getName(),
stopLocation.getCode(),
ListUtils.ofNullable(stopLocation.getName()),
ListUtils.ofNullable(stopLocation.getCode()),
stopLocation.getCoordinate().latitude(),
stopLocation.getCoordinate().longitude(),
Set.of(),
Expand All @@ -112,8 +112,8 @@ public LuceneIndex(TransitService transitService) {
directoryWriter,
StopLocationsGroup.class,
stopLocationsGroup.getId().toString(),
stopLocationsGroup.getName(),
null,
ListUtils.ofNullable(stopLocationsGroup.getName()),
List.of(),
stopLocationsGroup.getCoordinate().latitude(),
stopLocationsGroup.getCoordinate().longitude(),
Set.of(),
Expand All @@ -130,13 +130,13 @@ public LuceneIndex(TransitService transitService) {
addToIndex(
directoryWriter,
StopCluster.class,
stopCluster.id().toString(),
I18NString.of(stopCluster.name()),
stopCluster.code(),
stopCluster.primaryId(),
stopCluster.names(),
stopCluster.codes(),
stopCluster.coordinate().lat(),
stopCluster.coordinate().lon(),
stopCluster.modes(),
stopCluster.agencyIds()
List.of()
)
);
}
Expand Down Expand Up @@ -233,8 +233,8 @@ private static void addToIndex(
IndexWriter writer,
Class<?> type,
String id,
I18NString name,
@Nullable String code,
Collection<I18NString> names,
Collection<String> codes,
double latitude,
double longitude,
Collection<String> modes,
Expand All @@ -245,22 +245,24 @@ private static void addToIndex(
Document document = new Document();
document.add(new StoredField(ID, id));
document.add(new TextField(TYPE, typeName, Store.YES));
document.add(new TextField(NAME, Objects.toString(name), Store.YES));
document.add(new TextField(NAME_NGRAM, Objects.toString(name), Store.YES));
document.add(new ContextSuggestField(SUGGEST, Objects.toString(name), 1, typeName));
for(var name: names) {
document.add(new TextField(NAME, Objects.toString(name), Store.YES));
document.add(new TextField(NAME_NGRAM, Objects.toString(name), Store.YES));
document.add(new ContextSuggestField(SUGGEST, Objects.toString(name), 1, typeName));
}
document.add(new StoredField(LAT, latitude));
document.add(new StoredField(LON, longitude));

if (code != null) {
for (var code : codes) {
document.add(new TextField(CODE, code, Store.YES));
document.add(new ContextSuggestField(SUGGEST, code, 1, typeName));
}

for (var mode : modes) {
document.add(new TextField(MODE, mode, Store.YES));
}
for (var ids : agencyIds) {
document.add(new TextField(AGENCY_IDS, ids, Store.YES));
for (var aId : agencyIds) {
document.add(new TextField(AGENCY_IDS, aId, Store.YES));
}

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.opentripplanner.ext.geocoder;

import java.util.Collection;
import javax.annotation.Nullable;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.framework.i18n.I18NString;

/**
* A package-private helper type for transporting data before serializing.
*/
record LuceneStopCluster(
FeedScopedId id,
@Nullable String code,
String name,
StopCluster.Coordinate coordinate,
String primaryId,
Collection<String> secondaryIds,
Collection<I18NString> names,
Collection<String> modes,
Collection<String> agencyIds
) {}
Collection<String> codes,
StopCluster.Coordinate coordinate){
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Iterables;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.opentripplanner.framework.collection.ListUtils;
import org.opentripplanner.framework.geometry.WgsCoordinate;
Expand Down Expand Up @@ -61,33 +62,34 @@ Iterable<LuceneStopCluster> generateStopClusters(

LuceneStopCluster map(StopLocationsGroup g) {
var modes = transitService.getModesOfStopLocationsGroup(g).stream().map(Enum::name).toList();
var agencies = agenciesForStopLocationsGroup(g)
.stream()
.map(s -> s.getId().toString())
.toList();

var childStops = g.getChildStops();
var ids = childStops.stream().map(s -> s.getId().toString()).toList();
var childNames = childStops.stream().map(StopLocation::getName).filter(Objects::nonNull).toList();
var codes = childStops.stream().map(StopLocation::getCode).filter(Objects::nonNull).toList();

return new LuceneStopCluster(
g.getId(),
null,
g.getName().toString(),
toCoordinate(g.getCoordinate()),
g.getId().toString(),
ids,
ListUtils.combine(List.of(g.getName()), childNames),
codes,
modes,
agencies
toCoordinate(g.getCoordinate())
);
}

Optional<LuceneStopCluster> map(StopLocation sl) {
var agencies = agenciesForStopLocation(sl).stream().map(a -> a.getId().toString()).toList();
return Optional
.ofNullable(sl.getName())
.map(name -> {
var modes = transitService.getModesOfStopLocation(sl).stream().map(Enum::name).toList();
return new LuceneStopCluster(
sl.getId(),
sl.getCode(),
name.toString(),
toCoordinate(sl.getCoordinate()),
sl.getId().toString(),
List.of(),
List.of(name),
modes,
agencies
ListUtils.ofNullable(sl.getCode()),
toCoordinate(sl.getCoordinate())
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public static <T> List<T> distinctByKey(

return ret;
}

/**
* Take a single nullable variable and return an empty list if it is null. Otherwise
* return a list with one element.
*/
public static <T> List<T> ofNullable(T input) {
if(input == null){
return List.of();
}
else {
return List.of(input);
}
}
}

0 comments on commit 7e31651

Please sign in to comment.