How to combine different matches from a free text search result

Hello!

I'm trying to union two separate matches on the results of a free text search query.

I've created a FTS index on two different node labels like so...

CALL db.index.fulltext.createNodeIndex("OccupationLabels",["esco__Occupation", "ncs__OccupationAltLabel"],["skos__prefLabel", "ncs__altLabel"], {analyser: "english"})

Nodes with the label esco__Occupation have a string property called skos__prefLabel.
Similarly, ncs__OccupationAltLabel nodes have a string property called ncs__altLabel.

The nodes relate to each other thus: (:esco__Occupation)-[:ncs__hasAltLabel]->(:ncs__OccupationAltLabel)

I'm trying to return all esco__Occupation nodes where the search term matches its skos__prefLabel as well as the esco__Occupation nodes that are related to an ncs__OccupationAltLabel where the ncs__altLabel matches.

I can get the results I want like this...

call db.index.fulltext.queryNodes("OccupationLabels", "skos__prefLabel: checkout^2 ncs__altLabel: checkout") yield node, score
match (node:esco__Occupation)
return node as occ
union
call db.index.fulltext.queryNodes("OccupationLabels", "skos__prefLabel: checkout^2 ncs__altLabel: checkout") yield node, score
match (node)<-[:ncs__hasAltLabel]-(occ)
return occ

but that query calls queryNodes twice. I could have two separate indexes, but I'm trying to only call a single queryNodes. I've tried unioning the results from a single queryNodes like this...

call db.index.fulltext.queryNodes("OccupationLabels", "skos__prefLabel: checkout^2 ncs__altLabel: checkout") yield node, score
optional match (node)<-[:ncs__hasAltLabel]-(occ)
return occ
union
match (node:esco__Occupation)
return node as occ

but that returns too many results.

If I execute each union'ed match on its own, it returns the correct results, but as soon as I try to union them together, It returns way too many results.

Any help on what I'm doing wrong, or a better approach gratefully received!

Hi @lazcool,

You can create index on each node separately and use the optional match to get the result