Пример #1
0
  @Test
  public void test1_KeyValueCondition() throws Exception {
    System.out.println("test1_KeyValueCondition");

    manager.getContext().validKeys.clear();
    manager.getContext().validKeys.add("browser");

    db.open();

    AdDefinition b = new ImageAdDefinition();
    b.setId("1");
    KeyValueConditionDefinition sdef = new KeyValueConditionDefinition();
    sdef.getKeyValues().add(new KeyValue("browser", "firefox"));
    sdef.getKeyValues().add(new KeyValue("browser", "chrome"));
    b.addConditionDefinition(ConditionDefinitions.KEYVALUE, sdef);
    b.setFormat(new FullBannerAdFormat());
    db.addBanner(b);

    b = new ImageAdDefinition();
    b.setId("2");
    sdef = new KeyValueConditionDefinition();
    sdef.getKeyValues().add(new KeyValue("browser", "firefox"));
    sdef.getKeyValues().add(new KeyValue("browser", "ie"));
    b.addConditionDefinition(ConditionDefinitions.KEYVALUE, sdef);
    b.setFormat(new FullBannerAdFormat());
    db.addBanner(b);

    db.reopen();

    AdRequest request = new AdRequest();
    List<AdFormat> formats = new ArrayList<AdFormat>();
    formats.add(new FullBannerAdFormat());
    request.formats(formats);
    List<AdType> types = new ArrayList<AdType>();
    types.add(AdTypes.forType(ImageAdType.TYPE));
    request.types(types);
    request.keyValues().put("browser", "opera");

    List<AdDefinition> result = db.search(request);
    assertTrue(result.isEmpty());

    request.keyValues().clear();
    request.keyValues().put("browser", "firefox");
    result = db.search(request);
    assertEquals(2, result.size());

    request.keyValues().clear();
    request.keyValues().put("browser", "chrome");

    result = db.search(request);
    assertEquals(1, result.size());
    assertTrue(result.get(0).getId().equals("1"));

    request.keyValues().clear();
    request.keyValues().put("browser", "ie");

    result = db.search(request);
    assertEquals(1, result.size());
    assertTrue(result.get(0).getId().equals("2"));
  }
Пример #2
0
  @Override
  public void addQuery(AdRequest request, QueryBuilder builder) {
    if (request.date() != null) {
      List<DBObject> condition_queries = new ArrayList<DBObject>();

      /*
       * Prefix der Indizieren beachten.
       */
      for (int i = 0; i < DateConditionDefinition.MAX_PERIOD_COUNT; i++) {

        QueryBuilder condition = QueryBuilder.start();

        // from part of the query
        BasicDBObject query_from_1 =
            new BasicDBObject(
                AdDBConstants.ADDB_AD_DATE_FROM + i, new BasicDBObject("$lte", request.date()));
        BasicDBObject query_from_2 =
            new BasicDBObject(AdDBConstants.ADDB_AD_DATE_FROM + i, AdDBConstants.ADDB_AD_DATE_ALL);

        List<BasicDBObject> queries = new ArrayList<BasicDBObject>();
        queries.add(query_from_1);
        queries.add(query_from_2);
        BasicDBObject query_from = new BasicDBObject("$or", queries);

        condition.and(query_from);

        // to part of the query
        BasicDBObject query_to_1 =
            new BasicDBObject(
                AdDBConstants.ADDB_AD_DATE_TO + i, new BasicDBObject("$gte", request.date()));
        BasicDBObject query_to_2 =
            new BasicDBObject(AdDBConstants.ADDB_AD_DATE_TO + i, AdDBConstants.ADDB_AD_DATE_ALL);

        queries = new ArrayList<BasicDBObject>();
        queries.add(query_to_1);
        queries.add(query_to_2);
        BasicDBObject query_to = new BasicDBObject("$or", queries);

        condition.and(query_to);

        condition_queries.add(condition.get());
      }
      builder.and(new BasicDBObject("$or", condition_queries));
    }
  }
Пример #3
0
  @Override
  public void addQuery(AdRequest request, QueryBuilder builder) {
    if (request.day() == null) {
      return;
    }
    int day = request.day().getDay();
    if (day == Day.ALL.getDay()) {
      return;
    }

    List<String> days = new ArrayList<String>();
    days.add(String.valueOf(day));
    days.add(String.valueOf(Day.ALL.getDay()));

    BasicDBObject dq = new BasicDBObject(AdDBConstants.ADDB_AD_DAY, new BasicDBObject("$in", days));

    builder.and(dq);
  }
Пример #4
0
  @Override
  public void addQuery(AdRequest request, BooleanQuery mainQuery) {
    if (request.keywords() == null || request.keywords().size() == 0) {
      return;
    }

    BooleanQuery query = new BooleanQuery();

    BooleanQuery temp = new BooleanQuery();

    // keywords einfügen
    for (String k : request.keywords()) {
      temp.add(new TermQuery(new Term(AdDBConstants.ADDB_AD_KEYWORD, k)), Occur.SHOULD);
    }
    // all keywords einfügen
    temp.add(
        new TermQuery(new Term(AdDBConstants.ADDB_AD_KEYWORD, AdDBConstants.ADDB_AD_KEYWORD_ALL)),
        Occur.SHOULD);

    query.add(temp, Occur.MUST);
    mainQuery.add(query, Occur.MUST);
  }
Пример #5
0
  @Override
  public List<AdDefinition> search(AdRequest request) throws IOException {
    IndexSearcher searcher = nrt_manager.acquire();
    List<AdDefinition> result = new ArrayList<AdDefinition>();
    try {
      // Collector für die Banner
      AdCollector collector = new AdCollector(searcher.getIndexReader().numDocs());

      // MainQuery
      BooleanQuery mainQuery = new BooleanQuery();
      // Query für den/die BannerTypen
      BooleanQuery typeQuery = new BooleanQuery();
      for (AdType type : request.types()) {
        TermQuery tq = new TermQuery(new Term(AdDBConstants.ADDB_AD_TYPE, type.getType()));
        typeQuery.add(tq, Occur.SHOULD);
      }
      mainQuery.add(typeQuery, Occur.MUST);

      // Query für den/die BannerFormate
      BooleanQuery formatQuery = new BooleanQuery();
      for (AdFormat format : request.formats()) {
        TermQuery tq =
            new TermQuery(new Term(AdDBConstants.ADDB_AD_FORMAT, format.getCompoundName()));
        formatQuery.add(tq, Occur.SHOULD);
      }
      mainQuery.add(formatQuery, Occur.MUST);

      // Query für die Bedingungen unter denen ein Banner angezeigt werden soll
      Query cq = LuceneQueryHelper.getInstance().getConditionalQuery(request, this.addb);
      if (cq != null) {
        mainQuery.add(cq, Occur.MUST);
      }

      /*
       * Es sollen nur Produkte geliefert werden
       */
      if (request.products()) {
        // search online for products
        mainQuery.add(
            new TermQuery(
                new Term(AdDBConstants.ADDB_AD_PRODUCT, AdDBConstants.ADDB_AD_PRODUCT_TRUE)),
            Occur.MUST);

        // if possible add the product name, so online ads for that product will be found
        if (!Strings.isNullOrEmpty(request.product())) {
          mainQuery.add(
              new TermQuery(new Term(AdDBConstants.ADDB_AD_PRODUCT_NAME, request.product())),
              Occur.MUST);
        }

      } else {
        mainQuery.add(
            new TermQuery(
                new Term(AdDBConstants.ADDB_AD_PRODUCT, AdDBConstants.ADDB_AD_PRODUCT_FALSE)),
            Occur.MUST);
      }

      logger.debug(mainQuery.toString());
      System.out.println(mainQuery.toString());

      searcher.search(mainQuery, collector);

      BitSet hits = collector.getHits();
      // Ergebnis
      for (int i = hits.nextSetBit(0); i != -1; i = hits.nextSetBit(i + 1)) {
        Document doc = searcher.doc(i);
        result.add(addb.getBanner(doc.get(AdDBConstants.ADDB_AD_ID)));
      }
    } finally {
      nrt_manager.release(searcher);
    }

    return result;
  }