其他
Jetpack新成员AppSearch前来报到
https://huanglinqing.blog.csdn.net/
implementation("androidx.appsearch:appsearch:$appsearch_version")
kapt("androidx.appsearch:appsearch-compiler:$appsearch_version")
implementation("androidx.appsearch:appsearch-local-storage:$appsearch_version")
data class News(
@Document.Namespace
val namespace: String,
@Document.Id
val id: String,
@Document.StringProperty(indexingType = AppSearchSchema.StringPropertyConfig.INDEXING_TYPE_PREFIXES)
val newsTitle: String,
@Document.StringProperty(indexingType = AppSearchSchema.StringPropertyConfig.INDEXING_TYPE_PREFIXES)
val newsContent: String
)
val sessionFuture = LocalStorage.createSearchSession(
LocalStorage.SearchContext.Builder(context, /*databaseName=*/"news")
.build()
)
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}
https://issuetracker.google.com/issues/191389033
.Builder()
.addDocumentClasses(News::class.java).build()
var setSchemaFuture = Futures.transformAsync(
sessionFuture,
AsyncFunction<AppSearchSession?, SetSchemaResponse?> {
it?.setSchema(setChemaRequest)
},
mainExecutor
)
namespace = "new1",
id = "new_id_2",
newsTitle = "who is a boy",
newsContent = "Everyone, guess who is the handsome boy"
)
val putFuture = Futures.transformAsync(
sessionFuture,
AsyncFunction<AppSearchSession?, AppSearchBatchResult<String, Void>?> {
it?.put(putRequest)
},
mainExecutor
)
putFuture,
object : FutureCallback<AppSearchBatchResult<String, Void>?> {
override fun onSuccess(result: AppSearchBatchResult<String, Void>?) {
// Gets map of successful results from Id to Void
val successfulResults = result?.successes
// Gets map of failed results from Id to AppSearchResult
val failedResults = result?.failures
Log.d(TAG, "成功:" + successfulResults.toString())
Log.d(TAG, "失败:" + failedResults.toString())
}
override fun onFailure(t: Throwable) {
Log.d(TAG, t.message.toString())
}
},
mainExecutor
)
com.lonbon.appsearchdemo D/MainActivity: 失败:{}
namespace = "new1",
id = "new_id_1",
newsTitle = "Huang Linqing is handsome a boy",
newsContent = "Huang Linqing is an Android development engineer working in Hefei"
)
.addFilterNamespaces("new1")
.build()
sessionFuture,
Function<AppSearchSession?, SearchResults> {
it?.search("handsome", searchSpec)
},
mainExecutor
)
searchFuture,
object : FutureCallback<SearchResults> {
override fun onSuccess(result: SearchResults?) {
iterateSearchResults(result)
}
override fun onFailure(t: Throwable) {
Log.d(
TAG, "查询失败:" + t
.message
)
}
},
mainExecutor
)
Futures.transform(searchResults?.nextPage, Function<List<SearchResult>, Any> {
it?.let {
it.forEach { searchResult ->
val genericDocument: GenericDocument = searchResult.genericDocument
val schemaType = genericDocument.schemaType
if (schemaType == "News") {
try {
var note = genericDocument.toDocumentClass(News::class.java)
Log.d(
TAG,
"查询结果:新闻标题-" + note.newsTitle
)
Log.d(
TAG,
"查询结果:新闻内容-" + note.newsContent
)
} catch (e: AppSearchException) {
Log.e(
TAG,
"Failed to convert GenericDocument to Note",
e
)
}
}
}
}
}, mainExecutor)
}
.appsearchdemo D/MainActivity: 查询结果:新闻内容-Everyone, guess who is the handsome boy
.appsearchdemo D/MainActivity: 查询结果:新闻标题-Huang Linqing is a handsome boy
.appsearchdemo D/MainActivity: 查询结果:新闻内容-Huang Linqing is an Android development engineer working
.addIds("new_id_2")
.build()
val removeFuture = Futures.transformAsync(
sessionFuture, AsyncFunction {
it?.remove(deleteRequest)
},
mainExecutor
)
sessionFuture,
Function {
it?.close()
}, mainExecutor
)