0

社区:最近,当我使用 Elasticsearch[2.3.1] 进行项目时,我尝试使用在我添加到我的 .jar(elasticsearch.2.3.1.jar) 文件中编译的 java API 对 ES 进行简单查询项目,但是当我接下来编码时:

QueryBuilder qb = simpleQueryStringQuery("+kimchy -elasticsearch"); 

IDE 没有重新整理指令“simpleQueryStringQuery("+kimchy -elasticsearch")”,但在互联网和 ES 官方网站中的所有示例中都以这种形式出现。做错了什么?预先感谢。

 import java.net.InetAddress;
 import java.net.UnknownHostException;

 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.client.transport.TransportClient;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.transport.InetSocketTransportAddress;
 import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.sort.SortOrder;
 import org.elasticsearch.search.sort.SortParseElement;

  public class Search {


 public static void main(String[] args) {
    Client client;
    Settings settings = Settings.settingsBuilder()
            .put("client.transport.ignore_cluster_name", true).build();
    try {
        client = TransportClient
                .builder()
                .settings(settings)
                .build()
                .addTransportAddress(
                        new InetSocketTransportAddress(InetAddress
                                .getByName("localhost"), 9300));

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    QueryBuilder qb = simpleQueryStringQuery("+kimchy -elasticsearch");

    SearchResponse scrollResp = client.prepareSearch("thing")
            .addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC)
            .setScroll(new TimeValue(60000))
            .setQuery(qb)
            .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
    //Scroll until no hits are returned
    while (true) {

        for (SearchHit hit : scrollResp.getHits().getHits()) {
            //Handle the hit...
        }
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        //Break condition: No hits are returned
        if (scrollResp.getHits().getHits().length == 0) {
            break;
        }
    }   


}



 }
4

1 回答 1

1

你知道方法和导入是如何工作的吗?出现错误是因为您的类没有调用方法simpleQueryStringQuery并且您没有导入该方法。

你真正想要的是:要么使用QueryBuilders.simpleQueryStringQuery("...")

或者对. _ QueryBuilders.simpleQueryStringQuery请参阅:http ://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html或https://en.wikipedia.org/wiki/Static_import

于 2016-06-02T20:49:06.940 回答