如何在elasticsearch中搜索字段数组

2ul0zpep  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(437)

我在ElasticSearch中有一个索引叫做 professor 如果是交叉场,我需要“和”条件
对于相同的字段数组,我需要
我需要搜查一下 subject 哪个是 Physics 或者 Accounting 这是字段数组(或)语句
我需要搜查一下 typePermanent (&)条件
我需要搜查一下 LocationNY (&)条件
有可能{type':['contract','guest']}类型也作为列表出现

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'}]

下面是查询,第三个得到了,怎么添加 1 and 2 ```
content_search = es.search(index="professor", body={
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"Location.keyword": "NY"
}
}
]
}
}
})
content_search ['hits']['hits']

预期输出为id `[{ 'id':2,'name': 'AB','subject': ['Physics','Engineering'],'type':'Permanent','Location':'NY'}]` 
wfsdck30

wfsdck301#

您需要使用bool查询来 Package 所有条件
添加一个工作示例,其中包含索引数据(与所讨论的相同)、搜索查询和搜索结果
搜索查询:

{
  "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"
        }
      }
    ]

相关问题