Neo4j全文检索

全文检索基本概念

  • 搜索
    搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户
  • 搜索引擎
    搜索引擎存在的目的是存储,查找和获取数据.Neo4j用的搜索引擎是Lucene
  • 文档
    在搜索软件中,文档是一等公民.存储,搜索,显示都是以文档为核心.文档简单可以理解为数据库中的一行数据,但是这行数据包括了field name.
  • 倒排索引
    倒排索引是搜索引擎中核心数据结构.简而言之,它将所有文档变成像是一本书后面词汇表的东西. 通过这种数据结构能够快速的从一个单词找到文档
  • Lucene搜索语法
Query implementation Purpose Example
TermQuery 单词匹配 neo4j
PhraseQuery 短语匹配 "graph database"
RangeQuery 范围匹配 [A TO Z] {A TO Z}
WildcardQuery 正则匹配 g*p?, d??abase
PrefixQuery 前缀匹配 algo*
FuzzyQuery 后缀匹配 cipher~
BooleanQuery 查询条件聚合 graph AND "shortest path"

环境准备

  • 容器启动Neo4j
    docker run -p 17687:7687 -p 17474:7474 --name=neo4j-test neo4j:3.5.3
  • 创建数据, 使用测试数据.
    :play northwind-graph

Neo4j全文检索

Neo4j全文检索有以下特性,不过用下来最重要的我感觉是创建索引的语句实际上只是创建于给命名控件. Neo4j从2.2.x时代开始就默认开启node_auto_indexing=true. 倒排索引在数据插入时候已经创建了. 创建索引/删除索引代价是非常小的

  • 支持关系与节点的索引
  • 支持常用analyzers扩展
  • 可以使用lucene query语句
  • 可以返回查询结果评分
  • 对索引自动更新
  • 单索引文档数量不限

索引创建与删除

建立两个索引, 一个是Product的该标签的索引. 另外一个全数据库全文检索的索引


   
   
  1. 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'])
  2. call db.index.fulltext.createNodeIndex( "product",[ 'Product'],[ 'reorderLevel', 'unitsInStock', 'unitPrice', 'supplierID', 'productID', 'quantityPerUnit', 'discontinued', 'productName', 'unitsOnOrder', 'categoryID'])

删除索引

call db.index.fulltext.drop("all")

  
  

可以通过函数获取所有标签和属性


   
   
  1. call db .propertyKeys
  2. call db .labels

查询

这里面的查询非常简单.只要记住一个语句就能应付大多数场景


   
   
  1. call db.index.fulltext.queryNodes(
  2. 'all', //这里索引名
  3. 'Av' // lucene查询语句
  4. ) yield node
  5. where node.address contains "12" // where语句
  6. return node
  7. order by node.address // order skip limit
  8. skip 0
  9. limit 1
原文地址:https://blog.csdn.net/weixin_43086579/article/details/88059801

猜你喜欢

转载自www.cnblogs.com/jpfss/p/11393184.html