elasticsearch - 如何在Elasticsearch中搜索字段数组

标签 elasticsearch dsl

我在 flex 搜索中有一个名为professor的索引

  • 如果需要交叉字段,则需要“与”条件
  • 我需要OR条件的相同字段数组的

  • 我需要搜索subjectPhysics,这是字段数组(OR)语句
  • 我需要搜索Accountingtype(&)条件
  • 我需要搜索PermanentLocation(&)条件
  • {{type':['Contract','Guest']}类型也有可能作为列表
  • 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'}]
    
    查询如下,第三个得到它,如何添加NY
    content_search = es.search(index="professor", body={
        "query": {
        "bool": {
          "must": {
            "match_all": {}
          },
          "filter": [
            {
              "term": {
                "Location.keyword": "NY"
              }
            }
          ]
        }
      }
    })
    content_search ['hits']['hits']
    
    预期是id 1 and 2

    最佳答案

    You need to use the bool query, to wrap all your conditions


    添加带有索引数据(与所讨论的数据相同),搜索查询和搜索结果的工作示例
    搜索查询:
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "type.keyword": "Permanent"
              }
            },
            {
              "match": {
                "Location.keyword": "NY"
              }
            }
          ],
          "should": [
            {
              "match": {
                "subject.keyword": "Accounting"
              }
            },
            {
              "match": {
                "subject.keyword": "Physics"
              }
            }
          ],
          "minimum_should_match": 1,
          "boost": 1.0
        }
      }
    }
    
    搜索结果:
    "hits": [
          {
            "_index": "stof_64370980",
            "_type": "_doc",
            "_id": "2",
            "_score": 1.8365774,
            "_source": {
              "id": 2,
              "name": "AB",
              "subject": [
                "Physics",
                "Engineering"
              ],
              "type": "Permanent",
              "Location": "NY"
            }
          }
        ]
    

    关于elasticsearch - 如何在Elasticsearch中搜索字段数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64370980/

    相关文章:

    dsl - 是否可以将 JetBrains MPS 编辑器构建到我的程序中?

    elasticsearch - 如何在Elasticsearch中搜索&符?

    elasticsearch - Azure 诊断 + Logstash

    elasticsearch - 从rss输入logstash插件中删除HTML标签

    用于描述 UI 的 Java DSL?

    java - 我可以指定特定的解析器规则来跳过空格吗?

    java - 用于非常标准对象模型的 DSL,带有到 SQL DB 的映射

    java - 如何获取索引别名的映射

    elasticsearch - 如何删除与foreach处理器中的条件匹配的特定字段?

    elasticsearch - 在Kibana 6.8.0搜索栏中无法将 “OR”与 “NOT _exists_”一起使用