3

I am trying to fetch the content from Fredhopper by using the SmartTarget 2014 Java API. After going through the documentation, I have followed two approaches as below :

        ClaimStore claims = AmbientDataContext.getCurrentClaimStore();
        if (claims != null) {

            Map<URI, Object> x = claims.getAll();
            for (Map.Entry<URI, Object> entry : x.entrySet()) {
                System.out.println("\"KEY\"  " + entry.getKey()
                        + " \"Value\"  " + entry.getValue());

            }
        }


        String triggers = AmbientDataHelper.getTriggers(claims);
        System.out.println(triggers);

//Approach 1

        QueryBuilder builder = new QueryBuilder();
        builder.setMaxItems(2);
        builder.addCriteria(new RegionCriteria("Homepage Promotions"));
        builder.addCriteria(new PublicationCriteria(new TcmUri("tcm:0-69-1")));
        builder.parseQueryString(triggers);
        ResultSet fredHopperResultset = builder.execute();
        System.out.println(fredHopperResultset);
        List<Promotion> promoList = fredHopperResultset.getPromotions();

        for (Iterator iterator = promoList.iterator(); iterator.hasNext();) {
            Promotion promotion = (Promotion) iterator.next();

            List<Item> itemList = promotion.getItems();
            for (Item loopItem : itemList) {
                System.out.println(loopItem.getPromotionID());
            }
        }

//Approach 2

        Query query = QueryFactory.createInstance();

        ResultSet rs = query.execute("fh_location=//catalog01/en_US/categories<{catalog01_tcm_0_69_1}&st_region=Homepage Promotions", "", "&sc_refererdomain=www.philips.com&st_publication=tcm:0-69-1");

        System.out.println(rs.getName());

        System.out.println(rs.getPromotions().size());

         Promotion promo = rs.getPromotions().get(0);
         for (Item item : promo.getItems()) {
             System.out.println(item.getId() + item.getPromotionID()); 
         }
        System.out.println(rs.getItemsTotalCount());

I am able to print the values of Triggers and Claims sucessfully. While with Approach 1, I am getting the value of Fredhopper Resultset as Null. On accessing the logs, "SortFieldFilter.java:38 50180 Removing the field 'publicationtitle' from the sort fields. Sorting on non-live attributes is not supported. Please include this field in the tree attributes in order to sort on it".

While in approach 2, I am not getting data as expected based on the trigger-types parameters passed. Kindly provide some pointers on where I am going wrong.

Thanks.

  • Could you remove the non essential code from the examples? That would it make easier to read. Also there there is some duplications in the code, for example the result sets are set multiple times. – Jan H Dec 30 '14 at 7:17
3

I recommend using the QueryBuilder as in approach #1.

If the resultset is actually null, there was an error executing the query. It could be a timeout or a problem connecting to Fredhopper in the first place.

In any case, to find the root of your issue you should check the SmartTarget log files (not just the Fredhopper log files). Any errors will always be logged, but if the problem is just with the results not being as expected you can increase the log level to DEBUG and it will show the query that is being executed. You can then compare that to the query executed in the preview window of the Business Manager when looking at the results you want.

|improve this answer|||||
  • Hi Peter, Thanks for the response. For approach 2, I tried to access the logs. I am able to see the query executed in fredhopper.log file and it should return a promotion based on trigger as I have used only a browser trigger. In fredhopper.log, I am seeing the following warning- "Removing the field 'publicationtitle' from the sort fields. Sorting on non-live attributes is not supported. Please include this field in the tree attributes in order to sort on it". Kindly provide some pointers on how can I include this field in tree attributes and whether this can effect the resuts returned. Thanks – Mark Thomas Jan 12 '15 at 10:31
  • You can ignore that warning. If you really want to get rid of it, you need to make the publicationtitle field 'live' by adding it to the navigation index (see the documentation). But it's not at all related to your current issue. – Peter Kjaer Jan 12 '15 at 10:46
  • Hi Peter, Thanks for the quick response. But by the approach I have followed, there is no exception listed in the logs. It says the query returned 0 promotions and 2 items. I have created 1 promotion with trigger type as browser. This is the query that is printed in the logs : (fh_location=//catalog01/en_US/categories<{catalog01_tcmID}&st_region=Homepage+Promotions&st_publication=tcmid&sc_ua_browser=Firefox). Can you please provide some pointers. Thanks – Mark Thomas Jan 12 '15 at 11:43
  • Your Promotion probably does not match all of those fields in the query. Double-check the region, Publication, and Browser -- and remember that values are case sensitive too. I assume you modified the TCM IDs in that query before posting it here, right? Otherwise you need to make sure you query for real values. You're probably not escaping the query values correctly, which is one reason why it's much better to use the QueryBuilder instead. – Peter Kjaer Jan 12 '15 at 11:56
  • Try comparing the QueryBuilder.toString() result with the one from the log; that might tell you what's going wrong in either case. – Peter Kjaer Jan 12 '15 at 11:59
1

Approach one and two do very different things. The latter contains keywords and the first does not.

Approach 1:

The rs variable (one of the result sets) will never get promotions cause there is no region our publication id set.

The first fredHopperResultSet is loaded when again there is no region or publication id set on the query, so do not expect any promotions either.

The second fredHopperResultSet could contain promotions which are retrieved with getPromotions(). This is because the region and publication id are set with builder.addCriteria(). Of course there should be some matching promotions set up in SmartTarget. For troubleshooting you can set up a promotion on the Homepage Promotions region in publication tcm:0-69-1 whith no triggers set. The promotion will then always return, a great way to test if the web application can actually retrieve any promotions.

By the way, fredHopperResultset is declared twice, the code in Approach 1 will not work.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.