我在 flex 搜索中有一个名为professor
的索引
subject
或Physics
,这是字段数组(OR)语句Accounting
是type
(&)条件Permanent
是Location
(&)条件test = [{'id':1,'name': 'A','subject': ['Maths','Accounting'],'type':'Contract', 'Location':'NY'},
{ 'id':2,'name': 'AB','subject': ['Physics','Engineering'],'type':'Permanent','Location':'NY'},
{'id':3,'name': 'ABC','subject': ['Maths','Engineering'],'type':'Permanent','Location':'NY'}]
查询如下,第三个得到它,如何添加NY
content_search = es.search(index="professor", body={
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"Location.keyword": "NY"
}
}
]
}
}
})
content_search ['hits']['hits']
预期是id 1 and 2
最佳答案
You need to use the bool query, to wrap all your conditions
添加带有索引数据(与所讨论的数据相同),搜索查询和搜索结果的工作示例
搜索查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"type.keyword": "Permanent"
}
},
{
"match": {
"Location.keyword": "NY"
}
}
],
"should": [
{
"match": {
"subject.keyword": "Accounting"
}
},
{
"match": {
"subject.keyword": "Physics"
}
}
],
"minimum_should_match": 1,
"boost": 1.0
}
}
}
搜索结果:"hits": [
{
"_index": "stof_64370980",
"_type": "_doc",
"_id": "2",
"_score": 1.8365774,
"_source": {
"id": 2,
"name": "AB",
"subject": [
"Physics",
"Engineering"
],
"type": "Permanent",
"Location": "NY"
}
}
]
关于elasticsearch - 如何在Elasticsearch中搜索字段数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64370980/