Search 之Creating a Search Interface

Android 提供了search framework来为application 提供search experience.为application提供两张方式使用search。一种是在屏幕上面的search dialog,一种是通过searchView的提供的search widget.
还可能使能voice search,提供search request等功能.
当用户执行search 是,系统会新建一个Intent来启动和search interface关联的activity.
要使用search interface,可以follow下面的步奏.
1.首先在res/xml/searchable.xml中配置
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>
其中android:label是必须的,而android:hint 是可选的.


2.定义一个activity 来处理用户的search 需求.在<intent-filter>中必须包含ACITON_SEARCH,在meta-data中指定我们在第一步中的search 配置.
<application ... >
    <activity android:name=".SearchableActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
    ...
</application>


3.我们在searchable的activity中的onCreate 函数中解释发送过来携带搜索数据的Intent
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);


    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      doMySearch(query);
    }
}


一般情况下用ListView来呈现搜索的结果,如果搜索的数据来自SQLite,
则应该使用CursorAdapter,如果是其他来源,则使用BaseAdapter.
通过ListActivity的子类来呈现搜索结果.


如果search dialog被激活hi,可以通过实现onSeatchRequested方法来得到通知
@Override
public boolean onSearchRequested() {
    pauseSomeStuff();
    return super.onSearchRequested();
}


如果你设定Android的launchMode为singleTop,则应该在onNewIntent中处理Intent.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    handleIntent(getIntent());
}


@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}


private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      doMySearch(query);
    }
}


Using the Search Widget
需要在onCreateOptionsMenu 中实现
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the options menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);


    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default


    return true;
}


当同时使用widget和dialog时,可以通过onOptionsItemSelected()来激活widget。可以使用onSearchRequested()来激活dialog.


通过在xml中增加android:voiceSearchMode 来增加voicesearch
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable> Search
Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了强大的全文搜索、结构化查询、分析能力和实时数据分析等功能。而Golang是一种编程语言,也被称为Go语言,它具有高效、简洁、并发安全等特点。 在Golang中使用Elasticsearch可以通过第三方库进行操作,最常用的是官方提供的Elasticsearch客户端库——"github.com/elastic/go-elasticsearch"。这个库提供了与Elasticsearch进行交互的API,可以进行索引、搜索、聚合等操作。 使用go-elasticsearch库,你可以通过以下步骤来使用Elasticsearch: 1. 安装go-elasticsearch库:在终端中执行命令`go get github.com/elastic/go-elasticsearch/v8`来安装该库。 2. 导入库:在你的Go代码中导入"go.elastic.co/elasticsearch/v8"。 3. 创建Elasticsearch客户端:使用库提供的`elasticsearch.NewClient()`函数创建一个Elasticsearch客户端实例。 4. 执行操作:通过客户端实例调用相应的API方法来执行索引、搜索、聚合等操作。 以下是一个简单的示例代码,展示了如何使用go-elasticsearch库进行基本的索引和搜索操作: ```go package main import ( "context" "fmt" "log" "github.com/elastic/go-elasticsearch/v8" ) func main() { // 创建Elasticsearch客户端 cfg := elasticsearch.Config{ Addresses: []string{"http://localhost:9200"}, } es, err := elasticsearch.NewClient(cfg) if err != nil { log.Fatalf("Error creating the client: %s", err) } // 索引文档 doc := `{"title" : "Elasticsearch Golang Example"}` res, err := es.Index("my-index", strings.NewReader(doc)) if err != nil { log.Fatalf("Error indexing document: %s", err) } defer res.Body.Close() // 搜索文档 var buf bytes.Buffer query := map[string]interface{}{ "query": map[string]interface{}{ "match": map[string]interface{}{ "title": "example", }, }, } if err := json.NewEncoder(&buf).Encode(query); err != nil { log.Fatalf("Error encoding query: %s", err) } res, err = es.Search( es.Search.WithContext(context.Background()), es.Search.WithIndex("my-index"), es.Search.WithBody(&buf), es.Search.WithTrackTotalHits(true), es.Search.WithPretty(), ) if err != nil { log.Fatalf("Error searching for documents: %s", err) } defer res.Body.Close() // 处理搜索结果 var r map[string]interface{} if err := json.NewDecoder(res.Body).Decode(&r); err != nil { log.Fatalf("Error parsing the response body: %s", err) } fmt.Println(r) } ``` 这只是一个简单的示例,你可以根据自己的需求进行更复杂的操作。你可以参考go-elasticsearch库的文档(https://pkg.go.dev/github.com/elastic/go-elasticsearch/v8)来了解更多关于使用Elasticsearch和Golang的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值