Aggregation with keywords in Elasticsearch

纵饮孤独 提交于 2020-03-21 07:00:47

问题


My end goal is to build a faceted search page using an Elasticsearch index. I am following this tutorial

Why is this aggregation query not working with my data? I think I need to change my aggregation clause, so that it refers to the keywords rather than the field, but do not know the syntax.

Here is my data. I mean the response to the following Postman command:

GET https://<elastic username>:<elastic password>@<elastic url>:<port number>/<my index name>/_search 

Here's the response:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 11,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "contacts_6_vglz9gvldv",
                "_type": "_doc",
                "_id": "URYmrHABQ6Znr4-To-Ma",
                "_score": 1.0,
                "_source": {}
            },
            {
                "_index": "contacts_6_vglz9gvldv",
                "_type": "_doc",
                "_id": "2093",
                "_score": 1.0,
                "_source": {
                    "list_id": "6",
                    "contact_id": "2093",
                    "firstname": "DANIEL",
                    "middlename": "C",
                    "lastname": "BRYANT",
                    "suffix": "null",
                    "mobile_phone": "null",
                    "email": "",
                    "email2": "null",
                    "tags": "null",
                    "notes": "null",
                    "home_address1": "602  STONE CIRCLE CT APT 2",
                    "home_address2": "null",
                    "home_city": "SCHAUMBURG",
                    "home_state": "IL",
                    "home_zip": "60194",
                    "home_phone": "",
                    "county": "null",
                    "latitude": "42.030346",
                    "longitude": "-88.06422",
                    "location_point": "0101000020E6100000F2EF332E1C0456C03FC8B260E2034540",
                    "work_company": "null",
                    "work_occupation": "null",
                    "work_address1": "null",
                    "work_address2": "null",
                    "work_city": "null",
                    "work_state": "null",
                    "work_zip": "null",
                    "work_phone": "null",
                    "volun_walk": "null",
                    "volun_phone": "null",
                    "volun_yardsign": "null",
                    "volun_petition": "null",
                    "date_of_birth": "10/26/1991",
                    "sex": "M",
                    "race": "null",
                    "ethnicity": "null",
                    "religion": "null",
                    "married": "null",
                    "has_children": "null",
                    "dist_congress": "null",
                    "dist_state_house": "null",
                    "dist_state_senate": "null",
                    "dist_ward_township": "null",
                    "dist_precinct": "null",
                    "dist_library": "null",
                    "dist_county": "null",
                    "dist_judicial": "null",
                    "dist_school": "null",
                    "dist_fire": "null",
                    "dma": "null",
                    "linkedin": "null",
                    "facebook": "null",
                    "twitter": "null",
                    "instagram": "null",
                    "registered_party": "0",
                    "v2018_gen": "null",
                    "v2018_pri": "null",
                    "salutation": "null",
                    "honorific": "null",
                    "spouse": "null",
                    "assistant": "null",
                    "rnc_id": "null",
                    "state_voter_id": "null",
                    "i360_id": "null",
                    "household_id": "null",
                    "home_phone_reliability": "null",
                    "work_phone_reliability": "null",
                    "mobile_phone_reliability": "null",
                    "deceased": "null",
                    "has_moved": "null",
                    "created": "2019-11-13 21:24:55.825672",
                    "modified": "null",
                    "imported": "2019-11-13 15:24:51.006805",
                    "source": "null",
                    "custom": "null",
                    "fulltext": "'2':8 '60194':10 '602':3 'apt':7 'bryant':2 'circle':5 'ct':6 'daniel':1 'schaumburg':9 'stone':4",
                    "home_house_num": "602",
                    "home_predirection": "null",
                    "home_street_name": "STONE CIRCLE",
                    "home_street_type": "CT",
                    "home_postdirection": "null",
                    "home_unit_num": "APT 2",
                    "home_ruralroute": "null",
                    "registration_date": "null",
                    "registration_status": "null",
                    "web": "null",
                    "fake_col": "0.414"
                }
            },
            {
                "_index": "contacts_6_vglz9gvldv",
                "_type": "_doc",
                "_id": "2094",
                "_score": 1.0,
                "_source": {
                    "list_id": "6",
                    "contact_id": "2094",
                    "firstname": "Thomas",
                    "middlename": "Y",
                    "lastname": "Gehr",
                    "suffix": "null",
                    "mobile_phone": "null",
                    "email": "",
                    "email2": "null",
                    "tags": "null",
                    "notes": "null",
                    "home_address1": "174  Hemingway CT",
                    "home_address2": "null",
                    "home_city": "Vernon Hills",
                    "home_state": "IL",
                    "home_zip": "60061",
                    "home_phone": "",
                    "county": "null",
                    "latitude": "42.250633",
                    "longitude": "-87.95999",
                    "location_point": "0101000020E61000002C9FE57970FD55C03126FDBD14204540",
                    "work_company": "null",
                    "work_occupation": "null",
                    "work_address1": "null",
                    "work_address2": "null",
                    "work_city": "null",
                    "work_state": "null",
                    "work_zip": "null",
                    "work_phone": "null",
                    "volun_walk": "null",
                    "volun_phone": "null",
                    "volun_yardsign": "null",
                    "volun_petition": "null",
                    "date_of_birth": "11/23/1953",
                    "sex": "M",
                    "race": "null",
                    "ethnicity": "null",
                    "religion": "null",
                    "married": "null",
                    "has_children": "null",
                    "dist_congress": "null",
                    "dist_state_house": "null",
                    "dist_state_senate": "null",
                    "dist_ward_township": "null",
                    "dist_precinct": "null",
                    "dist_library": "null",
                    "dist_county": "null",
                    "dist_judicial": "null",
                    "dist_school": "null",
                    "dist_fire": "null",
                    "dma": "null",
                    "linkedin": "null",
                    "facebook": "null",
                    "twitter": "null",
                    "instagram": "null",
                    "registered_party": "1",
                    "v2018_gen": "null",
                    "v2018_pri": "null",
                    "salutation": "null",
                    "honorific": "null",
                    "spouse": "null",
                    "assistant": "null",
                    "rnc_id": "null",
                    "state_voter_id": "null",
                    "i360_id": "null",
                    "household_id": "null",
                    "home_phone_reliability": "null",
                    "work_phone_reliability": "null",
                    "mobile_phone_reliability": "null",
                    "deceased": "null",
                    "has_moved": "null",
                    "created": "2019-11-13 21:24:55.826028",
                    "modified": "null",
                    "imported": "2019-11-13 15:24:51.006805",
                    "source": "null",
                    "custom": "null",
                    "fulltext": "'174':3 '60061':8 'ct':5 'gehr':2 'hemingway':4 'hills':7 'thomas':1 'vernon':6",
                    "home_house_num": "174",
                    "home_predirection": "null",
                    "home_street_name": "HEMINGWAY",
                    "home_street_type": "CT",
                    "home_postdirection": "null",
                    "home_unit_num": "null",
                    "home_ruralroute": "null",
                    "registration_date": "null",
                    "registration_status": "null",
                    "web": "null",
                    "fake_col": "0.441"
                }
            },
            {
                "_index": "contacts_6_vglz9gvldv",
                "_type": "_doc",
                "_id": "2095",
                "_score": 1.0,
                "_source": {
                    "list_id": "6",
                    "contact_id": "2095",
                    "firstname": "EDYTA",
                    "middlename": "",
                    "lastname": "DZIADKOWIEC",
                    "suffix": "null",
                    "mobile_phone": "null",
                    "email": "",
                    "email2": "null",
                    "tags": "null",
                    "notes": "null",
                    "home_address1": "1005  TANGLEWOOD DR",
                    "home_address2": "null",
                    "home_city": "ALGONQUIN",
                    "home_state": "IL",
                    "home_zip": "60102",
                    "home_phone": "",
                    "county": "null",
                    "latitude": "42.1652",
                    "longitude": "-88.27284",
                    "location_point": "0101000020E61000009A42E735761156C04CA60A4625154540",
                    "work_company": "null",
                    "work_occupation": "null",
                    "work_address1": "null",
                    "work_address2": "null",
                    "work_city": "null",
                    "work_state": "null",
                    "work_zip": "null",
                    "work_phone": "null",
                    "volun_walk": "null",
                    "volun_phone": "null",
                    "volun_yardsign": "null",
                    "volun_petition": "null",
                    "date_of_birth": "10/31/1974",
                    "sex": "F",
                    "race": "null",
                    "ethnicity": "null",
                    "religion": "null",
                    "married": "null",
                    "has_children": "null",
                    "dist_congress": "null",
                    "dist_state_house": "null",
                    "dist_state_senate": "null",
                    "dist_ward_township": "null",
                    "dist_precinct": "null",
                    "dist_library": "null",
                    "dist_county": "null",
                    "dist_judicial": "null",
                    "dist_school": "null",
                    "dist_fire": "null",
                    "dma": "null",
                    "linkedin": "null",
                    "facebook": "null",
                    "twitter": "null",
                    "instagram": "null",
                    "registered_party": "1",
                    "v2018_gen": "null",
                    "v2018_pri": "null",
                    "salutation": "null",
                    "honorific": "null",
                    "spouse": "null",
                    "assistant": "null",
                    "rnc_id": "null",
                    "state_voter_id": "null",
                    "i360_id": "null",
                    "household_id": "null",
                    "home_phone_reliability": "null",
                    "work_phone_reliability": "null",
                    "mobile_phone_reliability": "null",
                    "deceased": "null",
                    "has_moved": "null",
                    "created": "2019-11-13 21:24:55.82604",
                    "modified": "null",
                    "imported": "2019-11-13 15:24:51.006805",
                    "source": "null",
                    "custom": "null",
                    "fulltext": "'1005':3 '60102':7 'algonquin':6 'dr':5 'dziadkowiec':2 'edyta':1 'tanglewood':4",
                    "home_house_num": "1005",
                    "home_predirection": "null",
                    "home_street_name": "TANGLEWOOD",
                    "home_street_type": "DR",
                    "home_postdirection": "null",
                    "home_unit_num": "null",
                    "home_ruralroute": "null",
                    "registration_date": "null",
                    "registration_status": "null",
                    "web": "null",
                    "fake_col": "0.257"
                }
            }
        ]
    }
}

Here is my query:

GET https://<elastic username>:<elastic password>@<elastic url>:<port number>/<my index name>/_search 

{
  "query": {
    "bool": {
        "should" : [
          {
           "match": {
             "firstname": "Thomas"
           }
         },
         {
           "match": {
             "lastname": "Bryant"
           }
         }
       ]
     }
   },
      "aggregations": {
      "home_state": {
         "terms": {"field": "home_state"}
      }
      }
}

Here is the error:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [home_state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "contacts_6_vglz9gvldv",
                "node": "pvhkxY1rT_qrL3Y-IvOoWw",
                "reason": {
                    "type": "illegal_argument_exception",
                    "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [home_state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
                }
            }
        ],
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [home_state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [home_state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
        }
    },
    "status": 400
}

回答1:


Field data is disabled by default since they consume a lot of heap space.

However, you can use field_name.keyword for aggregations. In you case, change your quesry to

{
  ...
   "aggregations": {
      "home_state": {
         "terms": {"field": "home_state.keyword"} <=== add '.keyword'
      }
      }
}


来源:https://stackoverflow.com/questions/60554841/aggregation-with-keywords-in-elasticsearch

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