简体   繁体   English

elasticsearch聚合中的第一个字母匹配

[英]First letter match in elasticsearch aggregations

I'm migrating my elasticsearch from using facets to using aggregations, and I want to create a query where the aggregations represent all the creator names that begin with a certain letter. 我正在将弹性搜索从使用构面迁移到使用聚合,我想创建一个查询,其中聚合表示以特定字母开头的所有创建者名称。

I've created a nested index like so: 我创建了一个嵌套索引,如下所示:

 indexes creators, type: 'nested' do
  indexes :name, type: 'string', analyzer: 'caseinsensitive', index: 'not_analyzed'
end

The following query will return all the items where a creator's name begins with a "b". 以下查询将返回创建者姓名以“b”开头的所有项目。 Great working so far. 到目前为止工作很好。

      {
        "query": {
            "filtered": {
                "query": {"match_all": {}},
                "filter": {
                    "nested": {
                        "path": "creators",
                        "query": {
                            "prefix": {
                                "creators.name": {
                                    "value": "b"
                                }
                            }
                        }
                    }
                }
            }
        },
        "aggregations": {
          "creators": {
             "nested": {
                "path": "creators"
             },
             "aggs": {
                "name": {
                   "terms": {
                      "field": "creators.name",
                      "size": 100
                   }
                }
             }
          }
       }
    }

However, the aggregations part of the query returns ALL of the aggregations for the results, including instances creator names that do not begin with a "b." 但是,查询的聚合部分返回结果的所有聚合,包括不以“b”开头的实例创建者名称。 For instance, if I had an item with two creators: 例如,如果我有一个包含两个创建者的项目:

           "creators": [
              {
                 "name": "Beyonce"
              },
              {
                 "name": "JayZ"
              }
           ],

The aggregation results would include both JayZ and Beyonce. 聚合结果将包括JayZ和Beyonce。 Like most people, I only want Beyonce. 像大多数人一样,我只想要碧昂丝。

Try this query and see how it goes: 试试这个查询,看看它是怎么回事:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "path": "creators",
          "query": {
            "prefix": {
              "creators.name": {
                "value": "b"
              }
            }
          }
        }
      }
    }
  },
  "aggregations": {
    "creators": {
      "nested": {
        "path": "creators"
      },
      "aggs": {
        "NAME": {
          "filter": {
            "prefix": {
              "creators.name": "b"
            }
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "creators.name",
                "size": 100
              }
            }
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM