简体   繁体   中英

Elasticsearch-Java Search is not giving any results

Just started off with ElasticSearch and JAVA Client APIs today itself and trying with a very simple Hello World program. However when i am trying to search the document for a particular keyword, i am not getting any results.

Need some help here -

Node node = nodeBuilder().clusterName("elasticsearch").node();
Client client = node.client();
client.prepareIndex("kodcucom", "article", "1")
                .setSource(putJsonDocument("Anurag",
                        "ElasticSeach provides Java API, thus it executes all operations "
                                + "asynchronously by using client object..",
                        new Date(), new String[] { "elasticsearch" }, "Anurag Jain"))
                .execute().actionGet();

client.prepareIndex("kodcucom", "article", "2")
                .setSource(putJsonDocument("Java Web Application and ElasticSearch (Video)",
                        "Today, here I am for exemplifying the usage of ElasticSearch which is an open source, distributed "
                                + "and scalable full text search engine and a data analysis tool in a Java web application.",
                        new Date(), new String[] { "elasticsearch" }, "Saanchi Jain"))
                .execute().actionGet();

PutJsonDocument method -

public static Map<String, Object> putJsonDocument(String title, String content, Date postDate, String[] tags,
            String author) {

        Map<String, Object> jsonDocument = new HashMap<String, Object>();

        jsonDocument.put("title", title);
        jsonDocument.put("content", content);
        jsonDocument.put("postDate", postDate);
        jsonDocument.put("tags", tags);
        jsonDocument.put("author", author);

        return jsonDocument;
    }

This is putting the documents in ES as the getDocuments are giving me the results.

However searching the documents is not working for me -

public static void searchDocument(Client client, String index, String type, String field, String value) {

        SearchResponse response = client.prepareSearch(index)
                .setTypes(type)
                .setSearchType(SearchType.DEFAULT)
                .setQuery(QueryBuilders.termQuery(field, value))
                .setFrom(0).setSize(60)
                .setExplain(true)
                .execute()
                .actionGet();

        SearchHit[] results = response.getHits().getHits();

        System.out.println("Current results: " + results.length);
        for (SearchHit hit : results) {
            System.out.println("------------------------------");
            Map<String, Object> result = hit.getSource();
            System.out.println(result);
        }
    }

This is how its being called -

searchDocument(client, "kodcucom", "article", "title", "Anurag");

Output is -

Current results: 0

Now my goal is simple that i just want to search the title field for a keyword say "Anurag" and return the corresponding document. Any advice/pointers?

BR,

Anurag

It's probably because your title field is an analyzed string by default. If you search for anurag in lowercase, you'll get a match.

searchDocument(client, "kodcucom", "article", "title", "anurag");
                                                        ^
                                                        |
                                                use lowercase here

Since you are running Elasticsearch indexing and searching API's on the same JAVA program sequentially, there is not enough time for Elasticsearch index to be refreshed with your new indexed json document.

By default, Elasticsearch will call refresh periodically every 1 second.
JAVA code (as you know) run faster than the time it took Elasticsearch to prepare the document for searching and therefore it didn't return your document.

One way to solve it is to Thread.sleep(2000); before searching (which is a very bad practice but for learning the API it would work).

Another way to solve it, is by using prepareRefresh() before searching:
client.admin().indices().prepareRefresh().get();
Which forces Elasticsearch to refresh its indexes and be ready for consecutive search requests.

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