ElasticSearch+Neo4j+ElasticSearch Head实现全文检索应用

本文主要阐述利用ES、知识图谱(Neo4j)实现全文检索,并利用ElasticSearch Head开源项目进行全文检索测试。实验在图谱的帮助下如何开展全文检索功能,为后续的复杂查询或语义检索做准备。
一、运行环境
1.Linux 7.5
2.JDK 1.8.0_191
3.ElasticSearch 7.17.4,注意用ES自带的jdk,因为这个版本的ES需要JDK11以上,配置见第二条内容。
4.Neo4j 3.5.28
二、ES和Neo4j的安装配置:
详细的安装配置本文略去,主要给出关键配置信息,可以参考其他安装配置,如大家需要,再咨询我。
1. 配置ES--java11环境:打开ES的bin下面elasticsearch和elasticsearch-cli文件,在最前面添加如下信息,将默认jdk调整为es自带的jdk,这样不改变整个系统的java环境,只是在运行ES时用指定的java环境。
#配置自己的jdk11
export JAVA_HOME=/home/es/elasticsearch-7.17.4/jdk
export PATH=$JAVA_HOME/bin:$PATH
2.配置ES--在config目录,打开 elasticsearch.yml,配置信息如下
node.master:true 或者 node.name: node-1
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
#允许各种http请求
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: Content-Type,Accept,Authorization,x-requested-with
#集群节点存储空间大小限制解决的配置选项
cluster.routing.allocation.disk.threshold_enabled: false
cluster.initial_master_nodes: ["node-1"]
#配置X-Pack,401问题解决的配置选项
xpack.security.enabled: false
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
3.启动ElasticSearch
前面配置完毕后,基本可以启动ES了,进入bin目录,运行 elasticsearch 或者 elasticsearch -d,后者表示后台启动,如果出现如下错误,请安装要求配置。
错误1 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
root 用户:vi /etc/sysctl.conf 添加 vm.max_map_count=262144:保存退出之后,执行命令 sysctl -p
错误2max number of threads is too low
root 用户:vi /etc/security/limits.conf 在文件最后添加
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
错误3max number of threads [1024] for user [elasticsearch] is too low, increase to at least [4096],该错误对应的配置文件,默认配置好了, 不需要单独配置。
vi /etc/security/limits.d/90-nproc.conf 改为4096
修改完记得退出es用户,再执行启动命令。
cp -r /home/hadoop/app/elasticsearch-7.17.4 /home/es/elasticsearch-7.17.4
4.查看ES运行情况(浏览器)
http://192.168.107.128:9200/ 查看可用情况
5.配置访问ES的用户密码,主要用于配置ElasticSearch-head和Kibana客户端访问ES的用户密码。
执行命令:elasticsearch-setup-passwords interactive
user:elastic/kibana_system/其他用户等
pwd:eses456
其他用户的密码同上。
6.安装plugin
安装IK分词器:elasticsearch-plugin install analysis-icu
查看安装的plugin elasticsearch-plugin list
删除pluginelasticsearch-plugin remove analysis-icu
7.配置用户自定义配置词典,进入es的config下,进入analysis-ik,打开IKAnalyzer.cfg.xml,配置自定义词典信息如下:
<entry key="ext_dict">userdefine.dic</entry>
userdefine.dic配置如下信息,重启ES后就会自动整体分词。
美团
地层
储层
8.配置Neo4j- elasticsearch
下载neo4j-elasticsearch-3.5.6.jar,放在neo4j的plugin目录下。
配置neo4j,neo4j.conf
elasticsearch.host_name= http://192.168.107.128:9200
elasticsearch.index_spec=result:result( name),resultsummary:resultSummary(abstract,name),resultkeys:resultKeys(name)  #这里的第一个result表示节点标签,第二个result表示ES中的result索引名称,name表示将result节点中name属性值。 多个索引关联见下一行的示例(中间逗号分隔)
9.Elasticsearch-head配置:下载完成后进入根目录,启动前端项目后直接运行(npm run start)。
elasticsearch-head主要是验证在Neo4j插入新的节点和关系时,数据同时同步到ES中,并测试是否提供全文检索等。
问题: elasticsearch- head无法连接ES的问题及解决方案
打开es配置文件,看看是否有以下几行配置信息。即可解决。
#允许跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
#允许各种http请求
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: Content-Type,Accept,Authorization,x-requested-with
#401问题解决的配置选项
xpack.security.enabled: false
#集群节点存储空间大小限制解决的配置选项
cluster.routing.allocation.disk.threshold_enabled: false
三、全文检索应用实践
(一)直接在控制台下,运行测试Neo4j或Neo4j和ES配置是否正常,具体如下:
查询(neo4j):
curl -X POST http: / /192.168.107.128:7474/db /data/cypher -H 'authorization: Basic bmVvNGo6bmVvbmVv' -H 'content-type: application/json' -d '{ "query" : "MATCH (n:result{name:'张三') Return n" }'
查询(es):
创建新节点(neo4j+es):
curl -X POST http: / /192.168.107.128:7474/db /data/cypher -H 'authorization: Basic bmVvNGo6bmVvbmVv' -H 'content-type: application/json' -d '{ "query" : "CREATE (n:result { name: { name1 }}) RETURN n" , "params" : { "name1" : "测试20221218"  } }'
(二)利用apoc调用ES(官方- ElasticSearch - APOC Extended Documentation
1. 查看es状态:  call  apoc . es . stats ( "192.168.107.128" )
2.查询检索:
CALL apoc.es.getRaw("192.168.107.128",'resultsummary/_search?',null) YIELD value 
UNWIND value.hits.hits as hits
RETURN hits['_source'] LIMIT 100
3.查询,关键词过滤-dsl query
CALL apoc.es.getRaw("192.168.107.128","resultsummary/_search?",{
  query: { match_phrase:{ name: "处理"}}
})
YIELD value 
UNWIND value.hits.hits as hits
RETURN hits['_source'] LIMIT 100
4.查找resultsummary索引中name包含处理的节点,并返回符合查询的title列表
CALL apoc.es.getRaw("192.168.107.128","resultsummary/_search?",{
  query: { match_phrase:{ name: "处理"}}
})
YIELD value 
UNWIND value.hits.hits as hits
UNWIND hits['_source']['name'] as name_list RETURN name_list
5.查找 knowledgegraph索引中title包含"python"的节点,并返回符合查询的title列表,并查询与包含"python"的result节点相连的一度ACTED_IN边
CALL apoc.es.getRaw("192.168.107.128","knowledgegraph/_search?",{
  query: { match_phrase:{ title: "python"}}
})
YIELD value 
UNWIND value.hits.hits as hits
with collect(hits['_source']['title']) as title_list 
MATCH p=(m:result)-[r:ACTED_IN]-(n) where m.name in title_list RETURN p
四、Neo4j自带的全文检索插件应用。
1.查看分词器: call  db . index . fulltext . listAvailableAnalyzers
2.创建索引: CALL  db . index . fulltext . createNodeIndex ( "summaryFullIndex" ,[ "resultSummary" ],[ "name" ],  {  analyzer :  "cjk" })
3.使用索引:
查找包含储层评价的报告
CALL db.index.fulltext.queryNodes("summaryFullIndex","储层评价") YIELD node, score
RETURN node, score
查找即包含综合,又做过核磁或密度的数据
CALL db.index.fulltext.queryNodes("summaryFullIndex","综合") YIELD node
where node.name contains "合成"  
where node.name contains "密度" 
return node  
order by node.name  
skip 0
limit 10
4.删除全文索引
call db.index.fulltext.drop("summaryFullIndex")

猜你喜欢

转载自blog.csdn.net/hhue2007/article/details/128422239