【问题标题】:How to get results with IndexTank?如何使用 IndexTank 获得结果?
【发布时间】:2011-04-23 06:08:33
【问题描述】:

我在 Java 客户端中使用 IndexTank,但我似乎无法访问结果:

    SearchResults results = index.search(Query.forString(keywords));

    for (Map<String, Object> document : results.results) {
        System.out.println("doc id: " + document.get("docid"));

最后一行失败:Type mismatch: cannot convert from Object to String

有人知道我为什么会收到这个错误吗?谢谢。

【问题讨论】:

  • 试试String.valueOf(document.get("docid"))。然而,这只是一个猜测,因为String + Object 应该自动调用String.valueOf(Object)。无论如何,您能否发布一个异常及其堆栈跟踪?

标签: java search indextank


【解决方案1】:

你所拥有的看起来不错。您确定要导入所有正确的类吗?这对我有用:

import java.util.HashMap;
import java.util.Map;

import com.flaptor.indextank.apiclient.Index;
import com.flaptor.indextank.apiclient.IndexTankClient;
import com.flaptor.indextank.apiclient.IndexTankClient.Query;
import com.flaptor.indextank.apiclient.IndexTankClient.SearchResults;

public class IndexTankExample {
    public static void main(String[] args) throws Exception {
        IndexTankClient client = new IndexTankClient("<PRIVATE URL>");
        Index index = client.getIndex("test");

        Map<String, String> fields = new HashMap<String, String>();
        fields.put("text", "foo bar baz");
        index.addDocument("1", fields);

        SearchResults results = index.search(Query.forString("bar"));
        for (Map<String, Object> document : results.results) {
            System.out.println("doc id: " + document.get("docid"));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2017-02-06
    • 1970-01-01
    相关资源
    最近更新 更多