如何在Elasticsearch(v 6.3)中过滤聚合结果

时间:2018-08-14 17:25:12

标签: elasticsearch elasticsearch-5

我有一个用于现场商品行的数组,例如:[3,35,1,11,12],[3,12]。我正在尝试查询该字段的自动完成结果,当我与3匹配时,我需要输出3和35。我的索引在所有情况下都可以正常工作,除非我正在使用数组数据类型。

我将需要过滤聚合结果以给出3和35,这是我无法检索的。我需要使用facet_filter或带有前缀的filter。类似于solr中的facet.prefix。 让我知道是否需要更改查询或映射? 查询:

GET contracts/doc/_search
{
 "size":0,
 "query":{
   "bool":{
     "must":{
       "match":{
         "commodity_line.autocomplete":"3"

       }
     }

   }
 },
"aggs" : {
  "names":{
    "terms":{
      "field":"commodity_line.keyword"
    }
  }

    }
}

映射:

   PUT contracts
        {

           "settings":{
              "analysis":{
                 "filter":{


                "gramFilter": {

                     "type":     "edge_ngram",
                    "min_gram" : 1,
                    "max_gram" : 20,
                  "token_chars": [
                    "letter",
                   "symbol",
                    "digit"
                  ]
                }

              },
                 "analyzer":{


                      "autocomplete": { 
                  "type": "custom",
                  "tokenizer": "standard",
                  "filter": [
                    "lowercase",
                    "trim",
                    "gramFilter",
                     "asciifolding"


                  ]
                }




                 }
              }
              }

           ,
           "mappings":{
              "doc":{
                 "properties":{
         "commodity_line" :{
                       "type":"text",
                      "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      },

                      "autocomplete":{
                        "type":"text",
                        "analyzer":"autocomplete", 
                      "search_analyzer":"standard"
                      }
                      }


                   }
        }
        }
           }
        }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案, 我必须将其与前缀匹配,而不是过滤结果。

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';

import { map } from 'rxjs/operators';

@Injectable({ providedIn: 'root' })
export class AuthService {

  API_URL = 'http://localhost:8000';

  constructor(private http: HttpClient) { }

  login(email: string, password: string) {
    return this.http.post<any>(`${this.API_URL}/api/login`, { email, password })
      .pipe(
        map(user => {
          // login successful if there's a jwt token in the response
          if (user && user.token) {
            // store user details and jwt token in local storage to keep user logged in between page refreshes
            localStorage.setItem('currentUser', JSON.stringify(user));
          }

          return user;
        })
      );
  }

  logout() {
    return this.http.post<any>(`${this.API_URL}/api/logout`, null)
      .pipe(
        map(resp => {
          // remove user from local storage to log user out
          localStorage.removeItem('currentUser');

          return true;
        })
      );
  }
}