如何在Elasticsearch中搜索数组的多个字段 [英] How to search on multiple fields of array in elasticsearch

查看:1370
本文介绍了如何在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'}]

推荐答案


filter子句(查询)必须出现在匹配的文档中。但是,
不同于必须忽略查询分数的情况。过滤器子句是在过滤器上下文中执行的
,这意味着计分被忽略,而
子句被认为用于缓存。

The filter clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

请仔细阅读布尔查询上的此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"
        }
      }
    ]

另一个搜索查询:


您甚至可以使用术语查询,它返回在提供的字段中包含
一个或多个确切术语的文档。术语查询与
相同

You can even use terms query that returns documents that contain one or more exact terms in a provided field.The terms query is the same as the term query, except you can search for multiple values.


{
  "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中搜索数组的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆