Simple Search for Java is not working

Hello !
I have just started with GroupDocs and testing the search module. I tried simple search, but it is not working.
Following is the code snippet, that I tried. Kindly help !

Index index = new Index(Utilities.INDEX_PATH);
System.out.println(“index initialized”);
// Add documents to index
index.addToIndex(Utilities.DOCUMENTS_PATH);
System.out.println(“index added”);
// Search in index
SearchResults searchResults = index.search(“request”);
System.out.println(“search started”);
// Print results in the console
for (DocumentResultInfo result : searchResults) {
System.out.println(“inside for”);
System.out.println(result.getHitCount() + " hits are in " + result.getFileName());

@Kushal.20,

Can you please share the exception/error that you are facing?

@atirtahir3
A not facing any exception or error. Am just not getting any output

Code is running till here. But, It’s not going into the for loop, I guess.

1 Like

@Kushal.20,

You are not getting any results because the word you are searching “request” is not found in index. However, we added “request” word in a document and application successfully showed the result.

Please follow steps given below:

  • Create a new document with word “request” or amend any existing document
  • Delete all old indexed files from “Documents Indexes” folder
  • Now, create index by invoking Indexing.createIndexOnDisk(); in MainClass.java and then comment/disable this method
  • Call SimpleSearch method. We are getting this output at our end - output.JPG (139.4 KB)

We had another test:

  • Added “request” in the available excel file (Excel.xlsx)
  • Called SimpleSearch method. As you’ve changed a file in documents folder, you have to update index as well. So that you can get results from the updated files. For this, we added index.update(); before SearchResults searchResults = index.search("request"); and got this - multiple hits.JPG (119.3 KB).

We’d also recommend you to explore the API documentation.
Let us know if this helps.

@atirtahir3 Thanks !
But, am not able to understand the above mentioned steps very clearly !
Please help me with this…how to create indexes and where to create the documents.
Could I not search upon the files already existing in my directory ??
The code that I shared above :

Is it any useful ?

@Kushal.20,

Please download our open-source example project here. Then follow the steps we mentioned earlier.

Yes you can but in order to perform the search over a folder of documents (in your existing directory), it is required to create the index for this. The core purpose of indexing is to make the search process faster and accurate.

You have to specify directory for indexed files and for source documents.

Yes, this code is accurate. Please download our open-source project.

@atirtahir3
I already downloaded your example project.
I have even imported it and working on it only. I followed the above steps, but still not getting anything on the console.
I am getting an error too in the pom.xm, which I am attaching too . Please find the same. The error says,“Missing artifact com.groupdocs:groupdocs-search:jar:19.3”
For this, I manually added the 19.5 jar file, but no results.
pom.PNG (46.2 KB)

Also, do we have to make any changes in the Utilities.java file too ?
Please let me know how to proceed, as I followed the above steps, but am not getting any result.
Thanks !

@Kushal.20,

We are not able to reproduce this issue at our end. We compiled the code your provided and got this - ouput.JPG (144.2 KB).
Please make sure this word “request” is available in any of the document otherwise you will get no result. Secondly, once you update a document (add word “request” in a document), delete all files from Document Indexes folder and invoke createIndexOnDisk method again. New indexes will be created in Document Indexes folder (you can check folder). After this run simpleSearch method and it must show you results.

Lastly, if you are evaluating the API in trial mode. Please go through Evaluation Limitations article. You can request a temporary license here. You can follow this document - Wizard.zip (152.9 KB) in order to request a temporary license successfully.

This is not a issue at all. You can add JAR file manually. However, if you still face this issue, please share a screen-cast covering all the steps.

@atirtahir3
I have reached a step further. I am able to get some result now, at least better than before ! Phew!
Now, tell me , how to and where to give our source directory where the search is to be performed ? Suppose, I want my entire E Drive to be searched for, so how to do that. I tried this :
I actually, tried giving the path in DOCUMENTS_PATH. Is it good to go ?
But, another goodness ! The code reflected to freeze and kept on executing.
While, when I gave “E:\temp_folder” ( I moved all the files inside a folder), then it resulted with the hits way too fast, as compared.
So, will it be like this/ Or am getting something wrong ?

1 Like

@Kushal.20,

Good to know that.

Yes, you have to put all the documents (you want to perform search on) in a folder. Then create index for this folder in order to perform search.

Yes this is how it works. We’d recommend you to go through the documentation as well.

Any suggestions, why ?
If I want my entire directory to be searched instead of any specific folder, then ?

@Kushal.20,

You can set DOCUMENTS_PATH to “E://” or like this,

// Add documents to index
index.addToIndex("E://");

Please note that in the example project, INDEX_PATH is the path where indexes will be saved and DOCUMENTS_PATH is source documents path. It contains files over which you want to perform search operations.
Have a look at supported document formats and this output.png (74.1 KB). We performed a search over E drive.

This may take time because there could be a lot of data in your E drive and API has to create index for such data. Eventually, you can perform search over a directory but indexing may take time depending on data in it.

Okay !
Thanks a lot , @atirtahir3

1 Like

@Kushal.20,

You are welcome.

1 Like

@atirtahir3
Hi !
After 2-3 days, am again performing simple search with the same steps, as I did previously. The only difference is the search query. I have changed the search query this time, but it is not responding. While, I search for the queries that I searched previously are giving me results.
Why is it so ?
I have even cleared the Document Indexes folders, then called creatIndexOnDisk , and then called simpleSearch method. But, no response.
Can you diagnose this for me, please?
Thanks !

@Kushal.20,

What is your search term/query that is not showing results? And what output do you get, please share screenshot.

I am searching for the word, ‘away’
Here goes my code :
public static void simpleSearch() {
// Create index
Index index = new Index(Utilities.INDEX_PATH);

	// Add documents to index
	index.addToIndex(Utilities.DOCUMENTS_PATH);
	// Search in index
	SearchResults searchResults = index.search("away");
	// Print results in the console
	for (DocumentResultInfo result : searchResults) {
		System.out.println(result.getHitCount() + " hits are in " + result.getFileName());
	}
}

Here is the output : simpleSearch.PNG (31.6 KB)

@Kushal.20,

Do you know any document that has ‘away’ word in it under documents directory?

@atirtahir3
I have shared with you the code and the screenshot of the output console.
yes, it does exist. I have 2-3 documents containing ‘away’ in my directory.
I am afraid, as I assured my colleagues that I have got this working. Please help me get through this ASAP.

@Kushal.20,

Please share all the documents with ‘away’ word. Share password , if they are password protected. We’ll then investigate it.