How to search on multiple fields of array in elasticsearch

好久不见. 提交于 2020-12-13 03:33:26

问题


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. I need to search subject which is Physics or Accounting this is array of fields(OR) statement

    AND

  2. I need to search type is Permanent or GUEST condition this is array of fields(OR) statement

    AND

  3. 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'}]

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']

Expected out is id [{ 'id':2,'name': 'AB','subject': ['Physics','Engineering'],'type':'Permanent','Location':'NY'},{'id':4,'name':'ABCD','subject': ['Physics','Engineering'],'type':['Contract','Guest'],'Location':'NY'}]


回答1:


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.

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

Search Query:

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

Search Result:

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

Another Search Query:

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

Update 1:

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


来源:https://stackoverflow.com/questions/64372692/how-to-search-on-multiple-fields-of-array-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!