[Huawei Developers] ask011:
Overview

Smart search application provides user to search capabilities through the device-side SDK and cloud-side APIs. Moreover this app results the selection search of Web Page Search, Image Search, Video Search and News Search. To achieve this the app is using HMS Search kit.

Huawei Search Kit

HUAWEI Search Kit fully opens Petal Search capabilities through the device-side SDK and cloud-side APIs, enabling ecosystem partners to quickly provide the optimal mobile app search experience.

Advantages of Using HMS Search kit

Rapid app development

Opens search capabilities through the device-side SDK and cloud-side APIs for different kinds of apps to build their own search capabilities within a short period of time.

Quick response

Allows you to use the global index library built by Petal Search and its massive computing and storage resources to quickly return the search results from the destination site that you specified.

Low cost

Frees you from having to consider such problems as technical implementation, operations, and maintenance, thus decreasing your development cost.

Integration Steps

1) Create an app in App Gallery Connect (AGC)

2) Enable Search kit from Manage Apis.

3) Add SHA-256 certificate fingerprint , Set Data storage location.

4) Now download the agconnect-service.json file from app information and paste in your app folder in android studio.

5) Add the maven url in buildscript and all projects in project level gradle file.

maven {url 'https://developer.huawei.com/repo/'}

6) Add the below dependencies in app level gradle file.

implementation 'com.huawei.hms:searchkit:5.0.4.303'

7) Set minimum SDK version as 24.

android {
defaultConfig {
minSdkVersion 24
}
}

8) Create Application class and set the AppID.

class ApplicationClass: Application() {
override fun onCreate() {
super.onCreate()
SearchKitInstance.init(this, Constants.APP_ID)
}
}

9) Now sync the gradle.

Development

1. Web Page Search

This explains how to use web page search and in this you can set the restriction for the search such as Language,Region, Page number and the number of search results to be displayed.

Add this code in the MainActivity and call this by passing searchText whenever the user search using WebSearch.

fun webSearch(searchText: String, token: String) : ArrayList<WebItem>{
val webSearchRequest = WebSearchRequest()
webSearchRequest.setQ(searchText)
webSearchRequest.setLang(Language.ENGLISH)
webSearchRequest.setSregion(Region.UNITEDKINGDOM)
webSearchRequest.setPs(10)
webSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val webSearchResponse = SearchKitInstance.getInstance().webSearcher.search(webSearchRequest)
for(i in webSearchResponse.getData()){
webResults.clear()
webResults.add(i)
}
return webResults
}

2. Image Search

This explains how to use Image search and the expected result will be in Image. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.

Add this code in the MainActivity and call this by passing searchText whenever the user search using ImageSearch.

fun imageSearch(searchText: String, token: String) : ArrayList<ImageItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val imageSearchResponse = SearchKitInstance.getInstance().imageSearcher.search(commonSearchRequest)
for(i in imageSearchResponse.getData()) {
imageResults.clear()
imageResults.add(i)
}
return imageResults
}

3. Video Search

This explains how to use Video search and the expected result will be in the Video format. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.

Add this code in the MainActivity and call this by passing searchText whenever the user search using VideoSearch.

fun videoSearch(searchText: String, token: String) : ArrayList<VideoItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val videoSearchResponse = SearchKitInstance.getInstance().videoSearcher.search(commonSearchRequest)
for(i in videoSearchResponse.getData()) {
videoResults.clear()
videoResults.add(i)

}
return videoResults
}

4. News Search

This explains how to use News search in your application. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.

Add this code in the MainActivity and call this by passing searchText whenever the user search using NewsSearch.

fun newsSearch(searchText: String, token: String) : ArrayList<NewsItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val newsSearchResponse = SearchKitInstance.getInstance().newsSearcher.search(commonSearchRequest)
for(i in newsSearchResponse.getData()) {
newsResults.clear()
newsResults.add(i)

}
return newsResults
}

5. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.SearchActivity">

<RelativeLayout
android:id="@+id/searchview_layout"
android:layout_height="36dp"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">

<EditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="36dp"
android:background="@drawable/shape_search_text"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical|start"
android:hint="Search"
android:imeOptions="actionSearch"
android:paddingStart="42dp"
android:paddingEnd="40dp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="13"
android:textAlignment="viewStart"
android:textColor="#000000"
android:textColorHint="#61000000"
android:textCursorDrawable="@drawable/selectedittextshape"
android:textSize="16sp" />

<ImageView
android:id="@+id/search_src_icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="3dp"
android:clickable="false"
android:focusable="false"
android:padding="10dp"
android:src="@drawable/ic_public_search" />
</RelativeLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/suggest_list"
android:visibility="gone"
android:layout_below="@+id/searchview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

<LinearLayout
android:id="@+id/linear_spell_check"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/searchview_layout"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="spellCheck:"
android:textColor="#000000"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="20dp"/>

<TextView
android:id="@+id/tv_spell_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="10dp"/>

</LinearLayout>
<View
android:id="@+id/v_line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:layout_below="@+id/linear_spell_check"
android:background="#e2e2e2"/>

<LinearLayout
android:id="@+id/linear_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/v_line"
android:orientation="vertical">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
app:tabIndicatorColor="#0000ad"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabRippleColor="#0D000000"
app:tabSelectedTextColor="#0000ad">
</com.google.android.material.tabs.TabLayout>

<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/content_list"
android:visibility="gone"
android:layout_below="@+id/v_line"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>

Now the implementation is done. From the search bar edit text you can call any of the respective search method and obtain the result as expected.

Result

Tips and Tricks

1) Do not forget to add the AppID in Application class.

2) Maintain minSdkVersion 24 and above.

Conclusion

This application is very much useful for those who wants to create a search application using HMS Search kit. Moreover it helps to understand the usage of HMS Search kit and to get the appropriate search results.

Reference

https://developer.huawei.com/consum.../HMSCore-Guides/introduction-0000001055591730