Mercurial > hg > ng2-query-ismi
diff app/query.service.ts @ 10:66dce99cef4e
attribute contains works now.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 20 Jan 2016 17:02:00 +0100 |
parents | 402c7229dc7c |
children | 6989cd00e8d7 |
line wrap: on
line diff
--- a/app/query.service.ts Wed Jan 20 11:32:31 2016 +0100 +++ b/app/query.service.ts Wed Jan 20 17:02:00 2016 +0100 @@ -16,13 +16,14 @@ public QUERY_MODES: QueryMode[] = [ {id: 'type_is', label:'Object type is'}, - {id: 'att_contains', label: 'Attribute contains'}]; + {id: 'att_contains', label: 'Attribute'}]; constructor(private _http: Http) { this.state = { 'steps': [], - 'cypherQuery': '', - 'cypherParams': {}, + 'resultCypherQuery': '', + 'resultCypherParams': {}, + 'attributeCypherQuery': '', 'results': [], 'resultTypes': '', 'numResults': 0 @@ -44,19 +45,7 @@ getQueryOptions(queryMode: QueryMode) { var options = ['a1', 'b1', 'c1']; if (queryMode.id === 'att_contains') { - if (this.state.attributeCypherQuery) { - var res = this.fetchCypherResult(this.state.attributeCypherQuery); - res.subscribe( - data => { - console.debug("neo4j data=", data); - var atts = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); - console.debug("ismi attributes=", atts); - }, - err => console.error("neo4j error=", err), - () => console.debug('neo4j query Complete') - ); - } - options = ['d', 'e', 'f']; + options = this.state.nextQueryAttributes; } else if (queryMode.id === 'type_is') { options = this.ismiObjectTypes; } @@ -87,40 +76,67 @@ } createCypherQuery() { - var resultCypher = ''; - var attCypher = ''; + var queryMatch = ''; + var queryWhere = ''; + var queryReturn = ''; + var resultQuery = ''; + var attQuery = ''; var returnType = ''; this.state.steps.forEach((step) => { + // object type is if (step.mode.id === 'type_is') { - resultCypher = `MATCH (e:${step.objectType}) return e`; + queryMatch = `MATCH (n:${step.objectType})`; + queryWhere = ''; + queryReturn = 'RETURN n'; returnType = 'node'; - attCypher = `MATCH (e:${step.objectType}) WITH DISTINCT keys(e) AS atts - UNWIND atts AS att - RETURN DISTINCT att ORDER BY att`; } + + // attribute contains + if (step.mode.id === 'att_contains') { + queryWhere = `WHERE lower(n.${step.attribute}) CONTAINS lower('${step.value}')`; + queryReturn = 'RETURN n'; + returnType = 'node'; + } + }); - this.state.resultCypherQuery = resultCypher; - this.state.attributeCypherQuery = attCypher; + resultQuery = queryMatch + ' ' + queryWhere + ' ' + queryReturn; + attQuery = queryMatch + ' ' + queryWhere + ' WITH DISTINCT keys(n) AS atts UNWIND atts AS att RETURN DISTINCT att ORDER BY att'; + this.state.resultCypherQuery = resultQuery; + this.state.attributeCypherQuery = attQuery; this.state.resultTypes = returnType; } updateQuery() { this.createCypherQuery(); - var query = this.state.resultCypherQuery; - var params = this.state.cypherParams; - var res = this.fetchCypherResult(query, params); - res.subscribe( + // run query for result table + var resQuery = this.state.resultCypherQuery; + var resParams = this.state.cypherParams; + var resRes = this.fetchCypherResult(resQuery, resParams); + resRes.subscribe( data => { - console.debug("neo4j data=", data); + console.debug("neo4j result data=", data); this.state.results = data.results[0].data.map(elem => elem.row[0]); this.state.numResults = this.state.results.length; }, - err => console.error("neo4j error=", err), - () => console.debug('neo4j query Complete') + err => console.error("neo4j result error=", err), + () => console.debug('neo4j result query Complete') ); + // run query for attribute list + if (this.state.attributeCypherQuery) { + var attRes = this.fetchCypherResult(this.state.attributeCypherQuery); + attRes.subscribe( + data => { + console.debug("neo4j att data=", data); + this.state.nextQueryAttributes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");; + }, + err => console.error("neo4j att error=", err), + () => console.debug('neo4j att query Complete') + ); + } } fetchCypherResult(query: string, params = {}) { + console.debug("fetching cypher query: ", query); var headers = new Headers(); headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); headers.append('Content-Type', 'application/json');