How do I submit a query for materials that must have the elements B and one or more of these: Mn, Cr, Fe, or Co

I need help trying to submit a query with the Materials Project API for materials that have B, and atleast one of these elements: Mn, Co, Fe, Cr.

I would recommend doing four different queries which pass pairs of elements to the elements field.

For example, to get the IDs of materials that contain at least B and Mn you could do:

from mp_api import MPRester

with MPRester("your_api_key") as mpr:
    docs =["B", "Mn"], fields=["material_id"])
    mpids = [doc.material_id for doc in docs]

Okay so I tried to do what you suggested and included exclude_elements field to remove oxides, phos any materials containing , and each metal so that I am not getting duplicates in each query. However I am noticing that it’s not really working for example when I ran this:

with MPRester(MY_API_KEY) as mpr:
docsBFe =[“B”,“Fe”], exclude_elements=[“O”,“P”]
,fields=[“material_id”, “formula_pretty”, “cif”, “spacegroup_number”,

it found 907 documents, but from looking at the first entry in the list, it is mp-1207649 with the formula_pretty being YFe3(BO3)4 which contains O in it.

Also when I excluded Mn, Cr, Co for the Fe & B query above to avoid finding redundant elements in my other queries like this:

with MPRester(JOHAN_API_KEY) as mpr:
docsBFe =[“B”,“Fe”], exclude_elements=[“O”,“P”,“Mn”,“Cr”,“Co”]
,fields=[“material_id”, “formula_pretty”, “cif”, “spacegroup_number”,

I suprisingly obtained more documents (2933) which intuitively doesn’t make sense to me because I am adding more restrictions.

thanks for all the help btw, I really appreciate it.

Hi @J0chin, thanks for bringing this up. This could be a bug in how exclude_elements is working. I am investigating now…

– Jason

@J0chin, this is fixed as of mp-api v0.24.7. The client tries to multithread requests to our server, and there was an issue with how it was using exclude_elements to perform that parallelization. I have confirmed your examples work now.

– Jason

Okay ya it’s also working for me too. Thanks again!