public Query build()

in baremaps-core/src/main/java/org/apache/baremaps/geocoder/GeonamesQueryBuilder.java [90:137]


  public Query build() throws ParseException {
    var builder = new BooleanQuery.Builder();

    if (queryText != null) {
      var queryTextEsc = QueryParser.escape(queryText);
      if (!queryTextEsc.isBlank()) {
        // Changing the fields here might affect queries using queryText.
        var fieldWeights = Map.of("name", 1f, "asciiname", 1f, "country", 1f, "countryCode", 1f);
        var parser = new SimpleQueryParser(analyzer, fieldWeights);
        if (andOperator) {
          // AND operator between query terms parsed instead of default OR
          parser.setDefaultOperator(BooleanClause.Occur.MUST);
        }
        var termsQuery = parser.parse(queryTextEsc);
        // at least one terms of the queryText must be present
        builder.add(termsQuery, BooleanClause.Occur.MUST);
      }
    }

    if (countryCode != null) {
      var countryCodeEsc = QueryParser.escape(countryCode);
      if (!countryCodeEsc.isBlank()) {
        var countryCodeQuery = new TermQuery(new Term("countryCode", countryCodeEsc));
        builder.add(countryCodeQuery, BooleanClause.Occur.MUST);
      }
    }


    if (!Strings.isNullOrEmpty(featureCode)) {
      var featureCodeQuery = new TermQuery(new Term("featureCode", featureCode));
      builder.add(featureCodeQuery, BooleanClause.Occur.MUST);
    }

    if (scoringByPopulation) {
      var query = builder.build();
      // ln(1+population) to tolerate entries with population=0
      Expression expr = JavascriptCompiler.compile("_score + ln(1+population)");

      var bindings = new SimpleBindings();
      bindings.add("_score", DoubleValuesSource.SCORES);
      bindings.add("population", DoubleValuesSource.fromIntField("population"));

      return new FunctionScoreQuery(
          query,
          expr.getDoubleValuesSource(bindings));
    }
    return builder.build();
  }