本文介绍了如何在Elasticsearch中搜索数组的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弹性搜索中有一个索引,称为 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


  1. 我需要搜索主题,这是物理 Accounting 这是字段(或)语句的数组

  1. I need to search subject which is Physics or Accounting 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>,以获得对此的详细了解。

Please go through this Elasticsearch documentation on bool queries, to get a detailed understanding about it.

添加一个包含索引数据(与该问题相同),搜索查询和搜索结果的有效示例

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

更新1:

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

这篇关于如何在Elasticsearch中搜索数组的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多