aboutsummaryrefslogtreecommitdiffstats
path: root/sample-apps/blog-recommendation/src/main/java/com/yahoo/example/BlogTensorSearcher.java
blob: 3a2a3df455fc8b224282c442efb3929eb745ad30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.yahoo.example;

import com.yahoo.data.access.Inspectable;
import com.yahoo.data.access.Inspector;
import com.yahoo.prelude.query.IntItem;
import com.yahoo.prelude.query.NotItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.querytransform.QueryTreeUtil;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.tensor.Tensor;

import java.util.ArrayList;
import java.util.List;

public class BlogTensorSearcher extends Searcher {

    @Override
    public Result search(Query query, Execution execution) {

        Object userItemCfProperty = query.properties().get("user_item_cf");
        if (userItemCfProperty != null) {

            // Modify the query by restricting to blog_posts...
            query.getModel().setRestrict("blog_post");

            // ... that has a tensor field fed and does not contains already read items.
            NotItem notItem = new NotItem();
            notItem.addItem(new IntItem(1, "has_user_item_cf"));
            for (String item : getReadItems(query)) {
                notItem.addItem(new WordItem(item, "post_id"));
            }
            QueryTreeUtil.andQueryItemWithRoot(query, notItem);

            // Modify the ranking by using the 'tensor' rank-profile (as defined in blog_post.sd)...
            if (query.properties().get("ranking") == null) {
                query.properties().set(new CompoundName("ranking"), "tensor");
            }

            // ... and setting 'query(user_item_cf)' used in that rank-profile
            query.getRanking().getFeatures().put("query(user_item_cf)", toTensor(userItemCfProperty));
        }

        return execution.search(query);
    }

    private Tensor toTensor(Object tensor) {
        if (tensor instanceof Tensor) {
            return (Tensor) tensor;
        }
        return Tensor.from(tensor.toString());
    }

    private List<String> getReadItems(Query query) {
        List<String> items = new ArrayList<>();
        Object readItems = query.properties().get("has_read_items");
        if (readItems instanceof Inspectable) {
            for (Inspector entry : ((Inspectable)readItems).inspect().entries()) {
                items.add(entry.asString());
            }
        }
        return items;
    }

}