Firebase 实时数据库在查询之间按单词搜索?

Posted

技术标签:

【中文标题】Firebase 实时数据库在查询之间按单词搜索?【英文标题】:Firebase Realtime Database Search by word in between the query? 【发布时间】:2021-04-07 05:44:37 【问题描述】:

在我的应用程序中,我在 Firebase 中有一个实时数据库,其中包含标题、图像、描述、搜索(应用程序中未显示但使用的额外字段包含带有小写字母的标题文本以进行搜索不敏感)。目前,当我在小写或大写或两者混合时键入相同的查询时,它搜索完美,但是当我键入查询之间的一些单词时它不起作用例如,如果查询是“这很长标题”当我输入“长”时它什么也不显示。我被卡住了,帮他解决这个问题。

我用过firebase ui库:

implementation 'com.firebaseui:firebase-ui-database:4.2.0'

实时数据库截图:

这是我的活动代码:

package com.blogspot.atifsoftwares.firebaseproject;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.io.ByteArrayOutputStream;

public class PostsListActivity extends AppCompatActivity 

    LinearLayoutManager mLayoutManager; //for sorting
    SharedPreferences mSharedPref; //for saving sort settings
    RecyclerView mRecyclerView;
    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mRef;

    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter;
    FirebaseRecyclerOptions<Model> options;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posts_list);

        //Actionbar
        ActionBar actionBar = getSupportActionBar();
        //set title
        mSharedPref = getSharedPreferences("SortSettings", MODE_PRIVATE);
        String mSorting = mSharedPref.getString("Sort", "newest"); //where if no settingsis selected newest will be default

        if (mSorting.equals("newest")) 
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means newest first
            mLayoutManager.setReverseLayout(true);
            mLayoutManager.setStackFromEnd(true);
         else if (mSorting.equals("oldest")) 
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means oldest first
            mLayoutManager.setReverseLayout(false);
            mLayoutManager.setStackFromEnd(false);
        

        //RecyclerView
        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);

        //send Query to FirebaseDatabase
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mRef = mFirebaseDatabase.getReference("Data");

        showData();
    

    //show data
    private void showData()
        options = new FirebaseRecyclerOptions.Builder<Model>().setQuery(mRef, Model.class).build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) 
            @Override
            protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) 
                holder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage());
            

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
                //Inflating layout row.xml
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);

                ViewHolder viewHolder = new ViewHolder(itemView);
                //item click listener
                viewHolder.setOnClickListener(new ViewHolder.ClickListener() 
                    @Override
                    public void onItemClick(View view, int position) 
                        //get data from firebase at the position clicked
                        String mTitle = getItem(position).getTitle();
                        String mDesc = getItem(position).getDescription();
                        String mImage = getItem(position).getImage();

                        //pass this data to new activity
                        Intent intent = new Intent(view.getContext(), PostDetailActivity.class);
                        intent.putExtra("title", mTitle); // put title
                        intent.putExtra("description", mDesc); //put description
                        intent.putExtra("image", mImage); //put image url
                        startActivity(intent); //start activity
                    

                    @Override
                    public void onItemLongClick(View view, int position) 
                        //Todo implement you on long click functionality here
                    
                );

                return viewHolder;
            
        ;

        //set layout as LinearLayout
        mRecyclerView.setLayoutManager(mLayoutManager);
        firebaseRecyclerAdapter.startListening();
        //set adapter to firebase recycler view
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    

    //search data
    private void firebaseSearch(String searchText) 

        //convert string entered in SearchView to lowercase
        String query = searchText.toLowerCase();

        Query firebaseSearchQuery = mRef.orderByChild("search").startAt(query).endAt(query + "\uf8ff");


        options = new FirebaseRecyclerOptions.Builder<Model>().setQuery(firebaseSearchQuery, Model.class).build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) 
            @Override
            protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) 
                holder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage());
            

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
                //Inflating layout row.xml
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);

                ViewHolder viewHolder = new ViewHolder(itemView);
                //item click listener
                viewHolder.setOnClickListener(new ViewHolder.ClickListener() 
                    @Override
                    public void onItemClick(View view, int position) 
                        //get data from firebase at the position clicked
                        String mTitle = getItem(position).getTitle();
                        String mDesc = getItem(position).getDescription();
                        String mImage = getItem(position).getImage();

                        //pass this data to new activity
                        Intent intent = new Intent(view.getContext(), PostDetailActivity.class);
                        intent.putExtra("title", mTitle); // put title
                        intent.putExtra("description", mDesc); //put description
                        intent.putExtra("image", mImage); //put image url
                        startActivity(intent); //start activity
                    

                    @Override
                    public void onItemLongClick(View view, int position) 
                        //Todo implement you on long click functionality here
                    
                );

                return viewHolder;
            
        ;

        //set layout as LinearLayout
        mRecyclerView.setLayoutManager(mLayoutManager);
        firebaseRecyclerAdapter.startListening();
        //set adapter to firebase recycler view
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);

    


    //load data into recycler view onStart
    @Override
    protected void onStart() 
        super.onStart();
        if (firebaseRecyclerAdapter !=null)
            firebaseRecyclerAdapter.startListening();
        
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        //inflate the menu; this adds items to the action bar if it present
        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
            @Override
            public boolean onQueryTextSubmit(String query) 
                firebaseSearch(query);
                return false;
            

            @Override
            public boolean onQueryTextChange(String newText) 
                //Filter as you type
                firebaseSearch(newText);
                return false;
            
        );
        return super.onCreateOptionsMenu(menu);
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        int id = item.getItemId();

        //handle other action bar item clicks here
        if (id == R.id.action_sort) 
            //display alert dialog to choose sorting
            showSortDialog();
            return true;
        
        if (id == R.id.action_add) 
            //start Add Post Activity
            startActivity(new Intent(PostsListActivity.this, AddPostActivity.class));
            return true;
        

        return super.onOptionsItemSelected(item);
    

    private void showSortDialog() 
        //options to display in dialog
        String[] sortOptions = " Newest", " Oldest";
        //create alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Sort by") //set title
                .setIcon(R.drawable.ic_action_sort) //set icon
                .setItems(sortOptions, new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                        // The 'which' argument contains the index position of the selected item
                        // 0 means "Newest" and 1 means "oldest"
                        if (which == 0) 
                            //sort by newest
                            //Edit our shared preferences
                            SharedPreferences.Editor editor = mSharedPref.edit();
                            editor.putString("Sort", "newest"); //where 'Sort' is key & 'newest' is value
                            editor.apply(); // apply/save the value in our shared preferences
                            recreate(); //restart activity to take effect
                         else if (which == 1) 
                            
                                //sort by oldest
                                //Edit our shared preferences
                                SharedPreferences.Editor editor = mSharedPref.edit();
                                editor.putString("Sort", "oldest"); //where 'Sort' is key & 'oldest' is value
                                editor.apply(); // apply/save the value in our shared preferences
                                recreate(); //restart activity to take effect
                            
                        
                    
                );
        builder.show();
    


【问题讨论】:

【参考方案1】:

当我在查询之间输入一些单词时,它不起作用

发生这种情况是因为 Firebase支持本机索引或在数据库属性中搜索文本字段。此外,下载整个节点以在客户端搜索字段根本不切实际。要启用 Firebase 实时数据库数据的全文搜索,我建议您使用第三方搜索服务,例如 Algolia 或 Elasticsearch。

This 是一个关于它如何与 Cloud Firestore 配合使用的示例,但您也可以将它与 Firebase 实时数据库配合使用。

【讨论】:

嗨阿提夫!一切都好吗,我可以帮助您了解其他信息吗? 是的,如果可以的话,一定要帮助我

以上是关于Firebase 实时数据库在查询之间按单词搜索?的主要内容,如果未能解决你的问题,请参考以下文章

Firebase - 可以在多个实时数据库之间共享分片数据吗

使用多个 LIKE 条件时,搜索查询无法按预期工作

将 Firebase 查询与 GeoQuery 相结合,按名称搜索附近区域的帖子

在firebase实时数据库中按时间戳删除帖子

Firebase 实时数据库按状态值计算活跃用户数

如何在 Firebase 实时数据库中搜索类似数组的数据?