2017-08-08 96 views
1

我有一個艱難的時間搞清楚如何與角4與角4

使用Elasticsearch(5.x的)Elasticsearch搜索服務,我安裝了ES JS客戶爭奪NPM還通過安裝在類型定義npm install --save @ types/elasticsearch。

我正在嘗試使用NgbBootstrap Typeahead進行自動填充。但我一直得到這個錯誤ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'

所以我一直在玩格式化輸入/輸出formatMatches函數,因爲我的服務返回{}而不是[]。似乎沒有任何工作。

formatMatches = (query: any) => query.hits.**hits**._source.suggestions || ''; 

第二個安打,是包含{「_source」的數組:{「建議」:「SUGGESTION_TEXT」}

所以現在我想,也許我的服務是不完全正確,因爲我基本上使用NgbBootstrap提供的相同的HTML。

這裏是我的服務

@Injectable() 
export class Elasticsearch { 
    private results: Observable<Suggestion[]>; 

    //private clientElasticsearch: Client; 
    private esClient: Client; 
    constructor() { 
    //this.clientElasticsearch = new Client({ 
    this.esClient = new Client({ 
     host: 'http://localhost:9200', 
     apiVersion: '5.x', 
     log: 'trace' 
     }); 
    } 

    //public test_search(value): Observable<SearchResponse<{}>> { 
    public search(query): Observable<SearchResponse<{}>> { 
    return Observable.fromPromise(<Promise<SearchResponse<{}>>> 

     this.esClient.search({ 
     index: 'query-index', 
     body: { 
     "query": { 
      "match_phrase_prefix": { 
      "suggestions": { 
       "query": query, 
       "max_expansions": 10 
       //"lenient": true 
      } 
      } 
     }, 
     "size": 5, 
     "from": 0, 
     "_source": ["suggestions"] 
     } 
    })); 
    } 
} 

我開始想,我不完全具備通過NPM使用ES JS客戶端,因爲我知道JS客戶端是建立與承諾的工作。我可以簡單地使用網絡API如this

+0

當時有過一個解決方案? –

+0

你有沒有想過,如果你是分享任何想法? @ user3125823 –

回答

-1

試試這個:

@Injectable() 
export class QueryService { 
private _esClient: Client; 
private _wildCard = '*'; 
constructor() { 
    this._esClient = new Client({ 

    host: 'http://localhost:9200', 
    log: 'error', 

    }); 
} 
getQueryfieldclin(fieldName: string): Observable<Object[]> { 
    return Observable.fromPromise(
      <Promise<any>> this._esClient.search({ 
       'index': 'ft-query', 
       'type': 'telco', 
       'size': 50, 
       'body': { 
        'query': { 
         'bool': { 
          'must': [ 
           { 
            'range': { 
             'InvoiceDate': { 
              'gte': '2004-01-01', 
              'lte': '2017-12-31' 
             } 
            } 
           } 
          ], 
          'should': [ 
           { 
            'wildcard' : { 'CLIN.keyword' : '*'} 
           } 
          ], 'minimum_number_should_match': 1 
         } 
        } 
       }, 
      }), 
     ) 
     .map((response: SearchResponse<string>) => response.hits.hits) 
     .do(data => data); 
} 

}