如何在elasticsearch中搜索多个数组字段

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

我在ElasticSearch中有一个索引叫做 professor 如果是交叉场,我需要“和”条件
对于相同的字段数组,我需要
我需要搜查一下 subject 哪个是 Physics 或者 Accounting 这是字段数组(或)语句

我需要搜查一下 typePermanent 或者 GUEST 条件这是字段数组(or)语句

我需要搜查一下 LocationNY (&)条件

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'},
{'id':4,'name':'ABCD','subject': ['Physics','Engineering'],'type':['Contract','Guest'],'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'},{'id':4,'name':'ABCD','subject': ['Physics','Engineering'],'type':['Contract','Guest'],'Location':'NY'}]` 
yks3o0rb

yks3o0rb1#

筛选子句(查询)必须出现在匹配的文档中。但是,查询的分数将被忽略。filter子句是在filter上下文中执行的,这意味着评分被忽略,子句被考虑用于缓存。
请阅读关于bool查询的elasticsearch文档,以获得对它的详细了解。
添加一个工作示例,其中包含索引数据(与所讨论的相同)、搜索查询和搜索结果
搜索查询:

{
  "query": {
    "bool": {
      "must": {
        "match": {
          "Location.keyword": "NY"
        }
      },
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "subject.keyword": "Accounting"
                }
              },
              {
                "match": {
                  "subject.keyword": "Physics"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                   "type.keyword": "Permanent"
                }
              },
              {
                "match": {
                  "type.keyword": "Guest"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64370980",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.10536051,
        "_source": {
          "id": 2,
          "name": "AB",
          "subject": [
            "Physics",
            "Engineering"
          ],
          "type": "Permanent",
          "Location": "NY"
        }
      },
      {
        "_index": "stof_64370980",
        "_type": "_doc",
        "_id": "4",
        "_score": 0.10536051,
        "_source": {
          "id": 4,
          "name": "ABCD",
          "subject": [
            "Physics",
            "Engineering"
          ],
          "type": [
            "Contract",
            "Guest"
          ],
          "Location": "NY"
        }
      }
    ]

另一个搜索查询:
您甚至可以使用术语查询来返回在提供的字段中包含一个或多个精确术语的文档。术语查询与术语查询相同,只是您可以搜索多个值。

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "subject.keyword": [
              "Physics",
              "Accounting"
            ]
          }
        },
        {
          "terms": {
            "type.keyword": [
              "Guest",
              "Permanent"
            ]
          }
        },
        {
          "match": {
            "Location.keyword": "NY"
          }
        }
      ]
    }
  }
}

更新1:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "subject.keyword": [
              "Physics",
              "Accounting"
            ]
          }
        },
        {
          "terms": {
            "type.keyword": [
              "Guest",
              "Permanent"
            ]
          }
        },
        {
          "match": {
            "Location.keyword": "NY"
          }
        },
        {
          "query_string": {
            "query": "ABCD"
          }
        }
      ]
    }
  }
}

相关问题