简体   繁体   中英

Elasticesearch Java API 2.3 Trouble

Community: Recently while I work in a project with Elasticsearch[2.3.1], I try to make a simple query to ES using a java API compile in a .jar(elasticsearch.2.3.1.jar) file that I add to my project, but when I code next :

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

The IDE didnt reconize the instruction "simpleQueryStringQuery("+kimchy -elasticsearch")" but in all example in internet and in ES official site appears in this form. What is doing wrong? Thank in advance.

 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;
        }
    }   


}



 }

You know how methods and imports work? The error comes because your class doesn't have a method called simpleQueryStringQuery and you don't import that method.

What you really want is: either use QueryBuilders.simpleQueryStringQuery("...")

Or use a static import for QueryBuilders.simpleQueryStringQuery . See: http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html or https://en.wikipedia.org/wiki/Static_import

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM