Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Fixed bug that gave incorrect results for multi-word search terms; se…
Browse files Browse the repository at this point in the history
…veral stability fixes
  • Loading branch information
Benjamin Winger committed Dec 19, 2014
1 parent a42ff68 commit cbe7572
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 114 deletions.
3 changes: 1 addition & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@
android:name="ca.dracode.ais.service.SearchService"
android:exported="true"
android:label="@string/app_name"
android:process=":search"
android:process=":index"
android:permission="ca.dracode.permission.AIS_SEARCH">
<intent-filter>
<action android:name="ca.drackde.ais.service.IndexService.SEARCH"/>
<action android:name="ca.dracode.ais.service.IndexService.SEARCH"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Expand Down
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
</goals>
<phase>package</phase>
<inherited>true</inherited>

<configuration>
<keystore>
my-release-key.keystore
Expand Down Expand Up @@ -141,7 +140,9 @@
<outputApk>${project.build.directory}/${project.artifactId}-${project.version}-RELEASE.apk
</outputApk>
</zipalign>

<proguard>
<skip>false</skip>
</proguard>
</configuration>
<executions>
<execution>
Expand Down
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
<string name="ais_permission_desc">Allows the app to search content stored in the Lucene
index created by the Android Indexing Service. All data stored in the index will be
available to the service.</string>
<string name="ais_permission_title">Connect to AIS Indexer</string>
</resources>
32 changes: 21 additions & 11 deletions src/ca/dracode/ais/indexclient/MClientService.aidl
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@

/**
* Copyright 2014 Benjamin Winger
*
* This file is part of The Android Indexing Service Client Library.
*
* The Android Indexing Service Client Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Android Indexing Service Client Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Android Indexing Service Client Library. If not, see <http://www.gnu.org/licenses/>.
*/

package ca.dracode.ais.indexclient;

/*
* MClientService.aidl
*
* Service interface file implemented by the client application that allows the
* index service to parse files using the client application
*
*/
interface MClientService {
/**
* Load libraries to access the file here so that it only has to be done once.
Expand All @@ -23,11 +33,11 @@ interface MClientService {
* @param page - the page to be returned from the file
* @return - A string containing all of the words on the page
*/
String getWordsForPage(int page);
String getWordsForPage(int page, String path);

/**
* Gets the number of pages in the file
* Gets the number of pages in the file previously specified in loadFile(String path)
* @return - the number of pages in the file specified at loadFile(String path)
*/
int getPageCount();
int getPageCount(String path);
}
2 changes: 1 addition & 1 deletion src/ca/dracode/ais/indexdata/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public LinkedHashMap<Integer, List<String>> getFirstResult(){
*/
public LinkedHashMap<Integer, List<String>> getResultAtIndex(int index){
if(this.results.size() > 0)
return (LinkedHashMap<Integer, List<String>>)this.results.entrySet().toArray()[index];
return (LinkedHashMap<Integer, List<String>>)(this.results.values().toArray()[index]);
else return null;
}

Expand Down
14 changes: 8 additions & 6 deletions src/ca/dracode/ais/indexer/FileIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void Build(IndexWriter writer, File file, int page,
writer.updateDocument(new Term("id", file.getPath() + ":"
+ page), doc);
}
//Log.i(TAG, "Done Indexing file: " + file.getName() + " " + page);
Log.i(TAG, "Done Indexing file: " + file.getName() + " " + page);
} catch(Exception e) {
Log.e(TAG, "Error ", e);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public int buildIndex(String filename, int pages) {
writer.updateDocument(new Term("id", file.getPath() + ":meta"),
doc);
}
Log.i(TAG, "Done creating metadata for file " + filename);
//Log.i(TAG, "Done creating metadata for file " + filename);
// Must only call ForceMerge and Commit once per document as they are very resource heavy operations
writer.commit();
} catch(Exception e) {
Expand Down Expand Up @@ -271,10 +271,12 @@ public int buildIndex(List<String> contents, File file) {
*/
public void close() {
try {
writer.commit();
// TODO - Determine how much of a speed increase is gained while searching after ForceMerge
writer.forceMerge(1);
writer.close();
if(writer != null) {
writer.commit();
// TODO - Determine how much of a speed increase is gained while searching after ForceMerge
writer.forceMerge(1);
writer.close();
}
} catch(IOException e) {
Log.e(TAG, "Error while closing indexwriter", e);
}
Expand Down
48 changes: 34 additions & 14 deletions src/ca/dracode/ais/indexer/FileSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,13 @@ public SearchResult findInFiles(int id, String term, String field,
public SearchResult findInFile(int id, String term, String field, String constrainValue,
String constrainField, int maxResults, int set, int type,
final int page) {

Query qry = this.getQuery(term, field, type);
Log.i(TAG, "Query: " + term + " " + field + " " + type + " " + constrainValue);
if(this.interrupt == id) {
this.interrupt = -1;
return null;
}
Query qry = this.getQuery(term, field, type);
if(qry != null){
String[] values = {constrainValue};
Filter filter;
Expand All @@ -301,8 +303,8 @@ public SearchResult findInFile(int id, String term, String field, String constra
filter = this.getFilter(constrainField, Arrays.asList(values), type, 0,
page - 1);
hits = concat(hits, indexSearcher.search(qry, filter,
maxResults * set + maxResults -hits.length,
new Sort(new SortField("page", SortField.Type.INT))).scoreDocs);
maxResults,
sort).scoreDocs);
}
} else {
sort = new Sort(new SortField("page", SortField.Type.INT, true));
Expand All @@ -314,7 +316,7 @@ public SearchResult findInFile(int id, String term, String field, String constra
filter = this.getFilter(constrainField, Arrays.asList(values), type, page,
Integer.MAX_VALUE);
hits = concat(hits, indexSearcher.search(qry, filter,
maxResults * -(set + 1) + maxResults - hits.length,
maxResults - hits.length,
sort).scoreDocs);
} else {
ScoreDoc[] tmp = hits;
Expand Down Expand Up @@ -364,8 +366,18 @@ private Query getQuery(String term, String field, int type) {
Query qry = null;
if(type == FileSearcher.QUERY_BOOLEAN) {
qry = new BooleanQuery();
((BooleanQuery) qry).add(new WildcardQuery(new Term(field, "*" + term + "*")),
String[] words = term.split(" ");
((BooleanQuery) qry).add(new WildcardQuery(new Term(field, "*" + words[0])),
BooleanClause.Occur.MUST);
if(words.length > 1) {
for(int i = 1; i < words.length - 1; i++) {
((BooleanQuery) qry).add(new WildcardQuery(new Term(field, words[i])),
BooleanClause.Occur.MUST);
}
((BooleanQuery) qry).add(new WildcardQuery(new Term(field,
words[words.length - 1] + "*")),
BooleanClause.Occur.MUST);
}
} else if(type == FileSearcher.QUERY_STANDARD) {
try {
qry = new QueryParser(Version.LUCENE_47, field,
Expand All @@ -387,9 +399,14 @@ private Filter getFilter(String constrainField, List<String> constrainValues,
int type, int startPage,
int endPage){
BooleanQuery cqry = new BooleanQuery();
for(String s : constrainValues) {
cqry.add(new TermQuery(new Term(constrainField, s)),
BooleanClause.Occur.SHOULD);
if(constrainValues.size() == 1){
cqry.add(new TermQuery(new Term(constrainField, constrainValues.get(0))),
BooleanClause.Occur.MUST);
} else {
for(String s : constrainValues) {
cqry.add(new TermQuery(new Term(constrainField, s)),
BooleanClause.Occur.SHOULD);
}
}
if(type == FileSearcher.QUERY_BOOLEAN && startPage != -1 && endPage != -1) {
cqry.add(NumericRangeQuery.newIntRange("page", startPage, endPage, true, true),
Expand All @@ -410,7 +427,7 @@ private Filter getFilter(String constrainField, List<String> constrainValues,
* @return A SearchResult containing the results sorted by relevance and page
*/
private SearchResult getHighlightedResults(List<Document> docs, Query qry, int type,
String term, int maxResults){
String term, int maxResults){
try {
int numResults = 0;
LinkedHashMap<String, LinkedHashMap<Integer, List<String>>> results = new LinkedHashMap<String, LinkedHashMap<Integer, List<String>>>();
Expand Down Expand Up @@ -469,9 +486,11 @@ private SearchResult getHighlightedResults(List<Document> docs, Query qry, int t
*/
private ArrayList<Document> getDocs(int maxResults, int set, ScoreDoc[] hits){
ArrayList<Document> docs = new ArrayList<Document>();
if(set > 0) {
for(int i = maxResults * set; i < hits.length && i < (maxResults * set +
maxResults);
int max = maxResults;
if(max > hits.length)max = hits.length;
Log.i(TAG, "Max: " + maxResults + " Set: " + set);
if(set >= 0) {
for(int i = 0; i < hits.length;
i++) {
try {
Document tmp = indexSearcher.doc(hits[i].doc);
Expand All @@ -481,8 +500,8 @@ private ArrayList<Document> getDocs(int maxResults, int set, ScoreDoc[] hits){
}
}
} else {
for(int i = 0; i < hits.length && i < (maxResults * -(set + 1) +
maxResults);
for(int i = 0; i < hits.length && i < (max * -(set + 1) +
max);
i++) {
try {
Document tmp = indexSearcher.doc(hits[i].doc);
Expand All @@ -492,6 +511,7 @@ private ArrayList<Document> getDocs(int maxResults, int set, ScoreDoc[] hits){
}
}
}
Log.i(TAG, "doc size" + docs.size());
return docs;
}

Expand Down
12 changes: 2 additions & 10 deletions src/ca/dracode/ais/service/BSearchService1_0.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,14 @@ interface BSearchService1_0 {
int resultSet);

/**
* used to send file contents to the indexing service. Because of the limitations of
* the service communicsation system the information may have to be sent in chunks as
* there can only be a maximum of about 1MB in the buffer at a time (which is shared
* among all applications). The client class sends data in chunks that do not exceed 256KB,
* currently pages cannot exceed 256KB as the data transfer will fail
* Tells the indexer to try to build the given file
* @param filePath - the location of the file to be built; used by the indexer to identify the file
* text - the text to be added to the index
* page - the page upon which the chunk of the file that is being transferred starts.
* It is a double to allow the transfer of parts of a single page if the page is too large
* maxPage - the total number of pages in the entire file
* @return 0 if index was built successfully;
* 1 if the file lock was in place due to another build operation being in progress;
* 2 if the Service is still waiting for the rest of the pages
* -1 on error
*/
int buildIndex(int id, String filePath, in List<String> text, double page, int maxPage);
int buildIndex(int id, String filePath);

/**
* Tells the indexer to load a file's metadata into memory for use in searches.
Expand Down
2 changes: 1 addition & 1 deletion src/ca/dracode/ais/service/FileListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void onEvent(int event, String path) {
}
// If a file was changed or created, re-index file or index the new file
mBoundService.createIndex(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path));
.getAbsolutePath() + path), null);
timerHandler.postDelayed(timerRunnable, DELAY);
Log.i(TAG, "File changed: " + path);
} else if(event == FileObserver.DELETE || event == FileObserver.MOVED_FROM) {
Expand Down
Loading

0 comments on commit cbe7572

Please sign in to comment.