elasticsearch的cross_fields查询
1.most_fields
这种方式搜索也存在某些问题
- 它不能使用
operator
或minimum_should_match
参数来降低次相关结果造成的长尾效应。
2.词 peter
和 smith
都必须出现,但是可以出现在任意字段中。
3.cross_fields
类型首先分析查询字符串并生成一个词列表,然后它从所有字段中依次搜索每个词。这种不同的搜索方式很自然的解决了 字段中心式 查询三个问题中的二个
4.经典案例
GET /_validate/query?explain { "query": { "multi_match": { "query": "peter smith", "type": "cross_fields", "operator": "and", "fields": [ "first_name", "last_name" ] } } }
参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cross_fields_queries.html
---------------------------------------------------------------------------------------------------------------------------
1.正则结合cross_fields
PUT /addressbook/_doc/2 { "name":"test url", "mobile":"123/456/url" }
GET /addressbook/_search { "query": { "multi_match": { "query": ".*456.*", #.*去掉也一样的效果 "fields": ["name","mobile"] } } }
---------------------------------------------------------------------------------------------------------
3.中文搜索,cross_field
3.1.定义映射
PUT yanbao072702 "mappings": { "_doc"{ "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "author": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "institution": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "industry": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "grade": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "doc_type": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "time": { "type": "date" , "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "doc_uri": { "type": "text", "index":false }, "doc_size": { "type": "integer", "index":false }, "market": { "type": "byte" } } } } }'
3.2 插入数据
PUT /yanbao0727/_bulk {"index":{"_id":"4"}} {"title":"香港-报告","author":"中信证券","institution":"中信中国","industry":"testindustry","grade":"testgrade","doc_type":"testdoc_type","time":"2019-07-27","doc_uri":"www.baidu.com","doc_size":"10M","market":"cn"}
3.3 测试分词器
POST _analyze { "analyzer": "ik_smart", "text":"test报告" }
3.4 搜索“君安 报告”
POST /yanbao0727/_search { "query": { "multi_match": { "query": "报告 君安", "type": "cross_fields", "fields": ["author","title"] } } }
3.5 搜索结果
{ "_index" : "yanbao0727", "_type" : "_doc", "_id" : "2", "_score" : 2.6205368, "_source" : { "title" : "test报告", "author" : "国泰君安", "institution" : "君安证券", "industry" : "testindustry", "grade" : "testgrade", "doc_type" : "testdoc_type", "time" : "2019-07-27", "doc_uri" : "www.baidu.com", "doc_size" : "10M", "market" : "cn" } }
用一个例子来演示会更加清晰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2018-07-27 用ansible2.5在Centos7.2上部署OpenShift3.9(转)