Elasticsearch服务与Angular 4
我很难弄清楚如何将Elasticsearch(5.x)与Angular 4结合使用 我安装了ES JS客户端vie npm,还通过npm安装--save@types/elasticsearch安装了类型定义 我正在尝试使用自动完成。但是,我不断收到此错误Elasticsearch服务与Angular 4,angular,ng-bootstrap,
elasticsearch-5,Angular,Ng Bootstrap,
elasticsearch 5,我很难弄清楚如何将Elasticsearch(5.x)与Angular 4结合使用 我安装了ES JS客户端vie npm,还通过npm安装--save@types/elasticsearch安装了类型定义 我正在尝试使用自动完成。但是,我不断收到此错误错误:找不到类型为“object”的不同支持对象“[object object]” 因此,我一直在使用formatMatches函数来格式化输入/输出,因为我的服务返回的是{},而不是[]。似乎什么都不管用 formatMatches = (qu
错误:找不到类型为“object”的不同支持对象“[object object]”
因此,我一直在使用formatMatches函数来格式化输入/输出,因为我的服务返回的是{}
,而不是[]
。似乎什么都不管用
formatMatches = (query: any) => query.hits.**hits**._source.suggestions || '';
第二个匹配项是一个数组,其中包含{u source:{“suggestions”:“suggestion\u 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"]
}
}));
}
}
@Injectable()
导出类Elasticsearch{
私人结果:可观察;
//私人客户调查:客户;
私人客户:客户;
构造函数(){
//this.clientElasticsearch=新客户端({
this.esClient=新客户端({
主持人:'http://localhost:9200',
apiVersion:'5.x',
日志:“跟踪”
});
}
//公共测试搜索(值):可观察{
公共搜索(查询):可观察{
可观察的回报(
this.esClient.search({
索引:“查询索引”,
正文:{
“查询”:{
“匹配短语前缀”:{
“建议”:{
“查询”:查询,
“最大扩展”:10
//“宽大”:真的吗
}
}
},
“尺寸”:5,
“from”:0,
“_来源”:[“建议”]
}
}));
}
}
我开始认为我不必通过npm使用ES JS客户端,因为我知道JS客户端是为承诺而构建的。我可以简单地使用web api吗?试试以下方法:
@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);
}
@Injectable()
导出类查询服务{
私人客户:客户;
私有_通配符='*';
构造函数(){
此.\u esClient=新客户端({
主持人:'http://localhost:9200',
日志:“错误”,
});
}
getQueryfieldclin(字段名:字符串):可观察{
可观察的回报(
此.\u esClient.search({
'索引':'金融时报查询',
“类型”:“电信公司”,
“尺寸”:50,
“身体”:{
“查询”:{
“布尔”:{
“必须”:[
{
“范围”:{
“发票日期”:{
‘gte’:‘2004-01-01’,
“lte”:“2017-12-31”
}
}
}
],
“应该”:[
{
'通配符':{'CLIN.keyword':'*'}
}
],“应匹配的最小数量”:1
}
}
},
}),
)
.map((响应:SearchResponse)=>response.hits.hits)
.do(数据=>数据);
}
}有解决这个问题的方法吗?你找到了吗?如果你找到了,你能分享一些想法吗?@user3125823