全文檢索基本概念
- 搜索
搜索這個(gè)行為是用戶與搜索引擎的一次交互過(guò)程,用戶需要找一些數(shù)據(jù),他提供給搜索引擎一些約束條件.搜索引擎通過(guò)約束條件抽取一些結(jié)果給用戶 - 搜索引擎
搜索引擎存在的目的是存儲(chǔ),查找和獲取數(shù)據(jù).Neo4j用的搜索引擎是Lucene
- 文檔
在搜索軟件中,文檔是一等公民.存儲(chǔ),搜索,顯示都是以文檔為核心.文檔簡(jiǎn)單可以理解為數(shù)據(jù)庫(kù)中的一行數(shù)據(jù),但是這行數(shù)據(jù)包括了field name. - 倒排索引
倒排索引是搜索引擎中核心數(shù)據(jù)結(jié)構(gòu).簡(jiǎn)而言之,它將所有文檔變成像是一本書后面詞匯表的東西. 通過(guò)這種數(shù)據(jù)結(jié)構(gòu)能夠快速的從一個(gè)單詞找到文檔 - Lucene搜索語(yǔ)法
Query implementation | Purpose | Example |
---|---|---|
TermQuery | 單詞匹配 | neo4j |
PhraseQuery | 短語(yǔ)匹配 | "graph database" |
RangeQuery | 范圍匹配 | [A TO Z] {A TO Z} |
WildcardQuery | 正則匹配 | g*p?, d??abase |
PrefixQuery | 前綴匹配 | algo* |
FuzzyQuery | 后綴匹配 | cipher~ |
BooleanQuery | 查詢條件聚合 | graph AND "shortest path" |
環(huán)境準(zhǔn)備
- 容器啟動(dòng)Neo4j
docker run -p 17687:7687 -p 17474:7474 --name=neo4j-test neo4j:3.5.3
- 創(chuàng)建數(shù)據(jù), 使用測(cè)試數(shù)據(jù).
:play northwind-graph
Neo4j全文檢索
Neo4j全文檢索有以下特性,不過(guò)用下來(lái)最重要的我感覺(jué)是創(chuàng)建索引的語(yǔ)句實(shí)際上只是創(chuàng)建于給命名控件. Neo4j從2.2.x時(shí)代開始就默認(rèn)開啟node_auto_indexing=true
. 倒排索引在數(shù)據(jù)插入時(shí)候已經(jīng)創(chuàng)建了. 創(chuàng)建索引/刪除索引代價(jià)是非常小的
- 支持關(guān)系與節(jié)點(diǎn)的索引
- 支持常用
analyzers
擴(kuò)展 - 可以使用
lucene query
語(yǔ)句 - 可以返回查詢結(jié)果評(píng)分
- 對(duì)索引自動(dòng)更新
- 單索引文檔數(shù)量不限
索引創(chuàng)建與刪除
建立兩個(gè)索引, 一個(gè)是Product
的該標(biāo)簽的索引. 另外一個(gè)全數(shù)據(jù)庫(kù)全文檢索的索引
call db.index.fulltext.createNodeIndex("all",['Product', 'Category', 'Supplier'],['reorderLevel', 'unitsInStock', 'unitPrice', 'supplierID', 'productID', 'discontinued', 'quantityPerUnit', 'categoryID', 'unitsOnOrder', 'productName', 'description', 'categoryName', 'picture', 'country', 'address', 'contactTitle', 'city', 'phone', 'contactName', 'postalCode', 'companyName', 'fax', 'region', 'homePage'])
call db.index.fulltext.createNodeIndex("product",['Product'],['reorderLevel', 'unitsInStock', 'unitPrice', 'supplierID', 'productID', 'quantityPerUnit', 'discontinued', 'productName', 'unitsOnOrder', 'categoryID'])
刪除索引
call db.index.fulltext.drop("all")
可以通過(guò)函數(shù)獲取所有標(biāo)簽和屬性
call db.propertyKeys
call db.labels
查詢
這里面的查詢非常簡(jiǎn)單.只要記住一個(gè)語(yǔ)句就能應(yīng)付大多數(shù)場(chǎng)景
call db.index.fulltext.queryNodes(
'all', //這里索引名
'Av' // lucene查詢語(yǔ)句
) yield node
where node.address contains "12" // where語(yǔ)句
return node
order by node.address // order skip limit
skip 0
limit 1