问题描述
我在弹性搜索中有一个索引,称为 professor
I have a index in elastic search called professor
-
如果是交叉字段我需要与条件
If for cross field i need "AND" condition
对于同一字段数组,我需要进行条件处理
for same field array i need to OR condition
-
我需要搜索
主题
,这是物理
或Accounting
这是字段(或)语句的数组
I need to search
subject
which isPhysics
orAccounting
this is array of fields(OR) statement
AND
我需要搜索类型
是 Permanent
还是访客
条件为这是字段(OR)语句的数组
I need to search type
is Permanent
or GUEST
condition this is array of fields(OR) statement
AND
我需要搜索位置
是 NY
(&)条件
I need to search Location
is NY
(&) condition
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'}]
查询如下,第3个得到了,如何添加 1和2
Query is below,3rd one got it, How to add 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'}]
推荐答案
请仔细阅读布尔查询上的此Elasticsearch文档a>,以获得对此的详细了解。
添加一个包含索引数据(与该问题相同),搜索查询和搜索结果的有效示例
Adding a working example with index data(same as that in question), search query, and search result
{
"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"
}
}
]
}
}
}
{
"query": {
"bool": {
"must": [
{
"terms": {
"subject.keyword": [
"Physics",
"Accounting"
]
}
},
{
"terms": {
"type.keyword": [
"Guest",
"Permanent"
]
}
},
{
"match": {
"Location.keyword": "NY"
}
},
{
"query_string": {
"query": "ABCD"
}
}
]
}
}
}
这篇关于如何在Elasticsearch中搜索数组的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!