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

非斯

我在弹性搜索中有一个索引叫做 professor

  • 如果对于交叉场,我需要“与”条件

  • 对于相同的字段数组,我需要进行OR条件

  1. 我需要搜索subjectPhysicsAccounting这是fields(OR)语句的数组

  2. 我需要搜索typePermanentGUEST条件这是fields(OR)语句的数组

  3. 我需要搜索的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'}]

ESCoder

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在URI搜索中搜索多个字段

如何在Elasticsearch中搜索具有多个值的一个字段?

如何使用elasticsearch在多个字段中搜索?

在Kibana / ElasticSearch中搜索多个字段

如何在弹性搜索中匹配每个数组项的多个字段

如何在多个字段/列中搜索数据?

如何在ng-select中搜索多个字段?

如何在Elasticsearch中过滤多个字段和值?

如何在Elasticsearch中搜索字段数组

Pymongo:如何在文档中插入多个字段的数组?

Postgres全文搜索:如何在多个字段中搜索多个单词?

如何从多个字段中搜索?

如何在elasticsearch中同时在嵌套字段中搜索单个对象的两个字段

在Elasticsearch中,如何从多层嵌套对象的多个字段中搜索字符串

ElasticSearch并在PHP中搜索多个字段

在 ElasticSearch 中按多个字段搜索精确短语

如何使用ElasticSearch和CURL请求在多个字段中搜索?

如何使用搜索在Swift中搜索多个字段?

Elasticsearch URI搜索多个字段

Elasticsearch 通配符搜索多个字段

如何在弹性搜索中进行多个字段匹配

如何在Vue.js 2中搜索多个字段

如何在Solr的一个字段中搜索多个单词?

如何在ElasticSearch中将多个字段添加到常用术语查询中?

如何在ElasticSearch中的数组中搜索单独的键和值字段?

如何在数组列中搜索多个字符串

ElasticSearch:如何在一个或多个索引的所有类型的任何字段中搜索值?

如何在Mongodb,Express,NodeJs中更新数组内的多个字段?

如何在Elasticsearch中按文档数组中的多个对象字段进行查询?