Android 接收到异常失败\u前提条件:查询需要索引

Android 接收到异常失败\u前提条件:查询需要索引,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我使用云数据库在firebase中创建项目,并希望将文档中存储的empUID字段与当前UID进行比较 我创建了一个查询,当当前UID和empUID匹配时获取数据,但无法获取数据 请说明我的查询是否正确,或者我是否需要更改代码 我的子集合路径 雇主(根集合)-->职位(子集合)-->职位申请人 详细信息(职位子集合) Logcat 2020-02-10 10:38:36.239 29836-29895/com.example.part_time_job I/Adreno: Build Config

我使用云数据库在firebase中创建项目,并希望将文档中存储的
empUID
字段与当前
UID
进行比较

我创建了一个查询,当当前
UID
empUID
匹配时获取数据,但无法获取数据

请说明我的查询是否正确,或者我是否需要更改代码

我的子集合路径

雇主(根集合)-->职位(子集合)-->职位申请人 详细信息(职位子集合)

Logcat

2020-02-10 10:38:36.239 29836-29895/com.example.part_time_job I/Adreno: Build Config                     : S P 6.0.7 AArch64
2020-02-10 10:38:36.231 29836-29836/com.example.part_time_job W/RenderThread: type=1400 audit(0.0:9359): avc: denied { search } for name="proc" dev="debugfs" ino=16085 scontext=u:r:untrusted_app:s0:c183,c257,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
2020-02-10 10:38:36.242 29836-29895/com.example.part_time_job I/Adreno: PFP: 0x016ee180, ME: 0x00000000
2020-02-10 10:38:36.231 29836-29836/com.example.part_time_job W/RenderThread: type=1400 audit(0.0:9360): avc: denied { search } for name="ctx" dev="debugfs" ino=16114 scontext=u:r:untrusted_app:s0:c183,c257,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
2020-02-10 10:38:36.245 29836-29895/com.example.part_time_job I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-02-10 10:38:36.245 29836-29895/com.example.part_time_job I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-02-10 10:38:36.245 29836-29895/com.example.part_time_job D/libEGL: eglInitialize: exit(res=1)
2020-02-10 10:38:36.245 29836-29895/com.example.part_time_job I/OpenGLRenderer: Initialized EGL, version 1.4
2020-02-10 10:38:36.246 29836-29895/com.example.part_time_job D/OpenGLRenderer: Swap behavior 2
2020-02-10 10:38:36.241 29836-29836/com.example.part_time_job W/RenderThread: type=1400 audit(0.0:9361): avc: denied { search } for name="ctx" dev="debugfs" ino=16114 scontext=u:r:untrusted_app:s0:c183,c257,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
2020-02-10 10:38:36.369 29836-29894/com.example.part_time_job W/e.part_time_jo: Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
2020-02-10 10:38:36.391 29836-29894/com.example.part_time_job W/e.part_time_jo: Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
2020-02-10 10:38:36.391 29836-29894/com.example.part_time_job W/e.part_time_jo: Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
2020-02-10 10:38:37.656 29836-29836/com.example.part_time_job E/RecyclerView: No adapter attached; skipping layout
2020-02-10 10:38:38.262 29836-29836/com.example.part_time_job E/Register4: Received an exception FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/part-time-job-74d92/firestore/indexes?create_composite=Cmxwcm9qZWN0cy9wYXJ0LXRpbWUtam9iLTc0ZDkyL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9Kb2IgQXBwbGljYW50IERldGFpbHMvaW5kZXhlcy9DSUNBZ09qWGg0RUsQAhoKCgZlbXBVSUQQARoKCgZ1c2VySWQQARoMCghfX25hbWVfXxAB
2020-02-10 10:38:38.263 29836-29877/com.example.part_time_job W/Firestore: (21.3.1) [Firestore]: Listen for Query(target=Query( collectionGroup=Job Applicant Details where userId == uOQXpA8WSgWb5DeDqXxLsdEz0G22 and empUID == null order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/part-time-job-74d92/firestore/indexes?create_composite=Cmxwcm9qZWN0cy9wYXJ0LXRpbWUtam9iLTc0ZDkyL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9Kb2IgQXBwbGljYW50IERldGFpbHMvaW5kZXhlcy9DSUNBZ09qWGg0RUsQAhoKCgZlbXBVSUQQARoKCgZ1c2VySWQQARoMCghfX25hbWVfXxAB, cause=null}
2020-02-10 10:38:41.232 29836-29870/com.example.part_time_job V/FA: Inactivity, disconnecting from the service
我的Java代码

package com.example.part_time_job;


import android.location.Address;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

import model.FInalApplyNotification;
import model.Jobpost;
import ui.JournalRecyclerAdapterNotificationEmp;
import util.JournalApi;


/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryFragment extends Fragment {

    private static final String TAG = "Register4";


    private FirebaseUser user;
    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener authStateListener;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private List<FInalApplyNotification> journalList;
    private String empid;
    private String title;
    private String currentUserId;







    private RecyclerView recyclerViewnotifyemp;
    private JournalRecyclerAdapterNotificationEmp journalRecyclerAdapterNotificationEmp;


    private CollectionReference collectionReference=db.collection("Employer");
    private TextView no_listnotifyemp;




    public HistoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_history, container, false);
        firebaseAuth= FirebaseAuth.getInstance();
        user =firebaseAuth.getCurrentUser();

        no_listnotifyemp= view.findViewById(R.id.no_listnotifyemp);
        journalList=new ArrayList<>();

        recyclerViewnotifyemp=view.findViewById(R.id.recyclerViewnotifyemp);
        recyclerViewnotifyemp.setHasFixedSize(true);
        recyclerViewnotifyemp.setLayoutManager(new LinearLayoutManager(getActivity()));


        currentUserId=JournalApi.getInstance().getUserId();





        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (journalList!=null){
            journalList.clear();
        }

        FInalApplyNotification fInalApplyNotification=new FInalApplyNotification();
        fInalApplyNotification.setTitle(title);
        fInalApplyNotification.setUserId(currentUserId);

                db.collectionGroup("Job Applicant Details").whereEqualTo("userId",currentUserId).whereEqualTo("empUID",fInalApplyNotification.getEmpUID())
                    .get()
                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                            if (!queryDocumentSnapshots.isEmpty()) {
                                for (QueryDocumentSnapshot journals : queryDocumentSnapshots) {
                                    FInalApplyNotification fInalApplyNotification = journals.toObject(FInalApplyNotification.class);
                                    journalList.add(fInalApplyNotification);


                                }

                                journalRecyclerAdapterNotificationEmp = new JournalRecyclerAdapterNotificationEmp(getActivity(), journalList);
                                recyclerViewnotifyemp.setAdapter(journalRecyclerAdapterNotificationEmp);
                                journalRecyclerAdapterNotificationEmp.notifyDataSetChanged();


                            } else {

                                no_listnotifyemp.setVisibility(View.VISIBLE);

                            }
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e(TAG, "Received an exception " + e.getMessage() );


                        }
                    });



    }




}

在异常状态下,必须为正在执行的查询创建索引

失败的前提条件:查询需要索引。您可以在这里创建:{some link}


当输入浏览器时,异常提供的链接将加载Firebase控制台,其中包含创建查询所需索引所需的所有详细信息。因此,您只需访问该链接并按照说明进行操作。

作为异常状态,您必须为正在执行的查询创建索引

失败的前提条件:查询需要索引。您可以在这里创建:{some link}


当输入浏览器时,异常提供的链接将加载Firebase控制台,其中包含创建查询所需索引所需的所有详细信息。因此,您只需访问链接并按照说明进行操作。

Firebase firestore主要有两种查询类型


  • 简单查询只有where条件,所以它们不需要数据库索引,比如
  • FirebaseFirestore.getInstance()集合(“城市”)
    .whereEqualTo(“大写”,真实)
    .get()
    .addOnCompleteListener(新的OnCompleteListener(){
    @凌驾
    未完成的公共void(@NonNull任务){
    if(task.issusccessful()){
    对于(QueryDocumentSnapshot文档:task.getResult()){
    Log.d(标记,document.getId()+“=>”+document.getData());
    }
    }否则{
    Log.d(标记“获取文档时出错:”,task.getException());
    }
    }
    });
    
  • 您的查询是复合查询,它有多个,其中查询链接在一起,需要,您可以在中创建复合索引,也可以使用创建复合索引,使用错误消息创建索引更简单,它会打印在日志消息中,因为它处理字段和排序顺序,并直接为您创建索引

  • Firebase firestore主要有两种查询类型


  • 简单查询只有where条件,所以它们不需要数据库索引,比如
  • FirebaseFirestore.getInstance()集合(“城市”)
    .whereEqualTo(“大写”,真实)
    .get()
    .addOnCompleteListener(新的OnCompleteListener(){
    @凌驾
    未完成的公共void(@NonNull任务){
    if(task.issusccessful()){
    对于(QueryDocumentSnapshot文档:task.getResult()){
    Log.d(标记,document.getId()+“=>”+document.getData());
    }
    }否则{
    Log.d(标记“获取文档时出错:”,task.getException());
    }
    }
    });
    
  • 您的查询是复合查询,它有多个,其中查询链接在一起,需要,您可以在中创建复合索引,也可以使用创建复合索引,使用错误消息创建索引更简单,它会打印在日志消息中,因为它处理字段和排序顺序,并直接为您创建索引
  • db.collectionGroup("Job Applicant Details").whereEqualTo("userId",currentUserId).whereEqualTo("empUID",fInalApplyNotification.getEmpUID())
    
    FirebaseFirestore.getInstance().collection("cities")
            .whereEqualTo("capital", true)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });