elasticsearch - 如何在elasticsearch中搜索字段数组
问题描述
我在弹性搜索中有一个名为professor
如果对于跨场我需要“AND”条件
对于相同的字段数组,我需要 OR 条件
- 我需要搜索
subject
这是Physics
或Accounting
这是字段数组(OR)语句 - 我需要搜索
type
的是Permanent
(&) 条件 - 我需要搜索
Location
的是NY
(&) 条件 - {'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'}]
解决方案
您需要使用bool 查询来包装所有条件
添加带有索引数据(与所讨论的相同)、搜索查询和搜索结果的工作示例
搜索查询:
{
"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"
}
}
]
推荐阅读
- excel - VBA 以原始格式显示注释
- python - to_html 将 daraframes 中的 <, > 替换为奇怪的字符,同时将其转换为 html
- python - Python中的fisher线性判别式
- websphere-liberty - 在 WebSphere Liberty 中忽略 LtpaToken
- xamarin - Xamarin 表单获取请求不适用于 HTTP 和 HTTPS
- google-bigquery - 如何在bigquery中将嵌套数组数据展平为行
- c++ - 如何使用 CMake 配置 TGUI + SFML
- python - Python 到 Blockly 中的块
- python - 仅在 PyCharm 的编辑器中缺少 qgis.core 模块中的类
- c# - 如何将 connectionString 从 App.config 移动到 C# 中的代码?