elasticsearch - 如何在 Elasticsearch 中编写复杂的 DSL 查询?
问题描述
这就是我用来在多字段映射中创建索引的方法
curl -X PUT http://localhost:9200/mapping_log
{ "mappings":{ "properties":{"data1:{"type": "text","fields":{"keyword":{"type":"keyword"}}}, {"data2":{"type": "text","fields":{"keyword":{"type":"keyword"}}}, {"data3":{"type": "text","fields":{"keyword":{"type":"keyword"}}}, } } }
这是我的弹性搜索中的数据,我想进行搜索,例如,当我搜索支持时,我应该获取值数据和服务器,当我搜索开发人员时,我应该获取 geo 和 graph,并且我想要 data3 的建议者,比如假设如果我输入 g 那么我应该得到图形和地理选项,有人可以帮我解决这个问题吗?
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "mapping_log",
"_type": "properties",
"_id": "1",
"_score": 1,
"_source": {
"data1": "support",
"data2": "data",
"data3": "datapos"
}
},
{
"_index": "mapping_log",
"_type": "properties",
"_id": "2",
"_score": 1,
"_source": {
"data1": "support",
"data2": "server",
"data3": "serverpos"
}
},
{
"_index": "mapping_log",
"_type": "properties",
"_id": "3",
"_score": 1,
"_source": {
"data1": "developer",
"data2": "graph",
"data3": "graphpos"
}
},
{
"_index": "mapping_log",
"_type": "properties",
"_id": "4",
"_score": 1,
"_source": {
"data1": "developer",
"data2": "geo",
"data3": "geopos"
}
}
]
}
}
解决方案
您需要将edge_ngram 标记器与源过滤一起使用
添加带有索引数据(与问题相同)、索引映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 10,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"properties": {
"data1": {
"type": "text"
},
"data2": {
"type": "text"
},
"data3": {
"type": "text",
"analyzer":"my_analyzer"
}
}
}
}
搜索查询:
当我寻求支持时,我应该得到值数据和服务器
{
"_source": [
"data2"
],
"query": {
"multi_match": {
"query": "support",
"fields": [
"data1",
"data2"
]
}
}
}
搜索结果:
"hits": [
{
"_index": "67260491",
"_type": "_doc",
"_id": "1",
"_score": 1.4094054,
"_source": {
"data2": "server"
}
},
{
"_index": "67260491",
"_type": "_doc",
"_id": "4",
"_score": 0.44183272,
"_source": {
"data2": "data"
}
}
]
搜索查询:
{
"_source": [
"data2"
],
"query": {
"multi_match": {
"query": "developer",
"fields": [
"data1",
"data2"
]
}
}
}
搜索结果:
"hits": [
{
"_index": "67260491",
"_type": "_doc",
"_id": "2",
"_score": 1.0296195,
"_source": {
"data2": "graph"
}
},
{
"_index": "67260491",
"_type": "_doc",
"_id": "3",
"_score": 1.0296195,
"_source": {
"data2": "geo"
}
}
]
搜索查询:
我想要 data3 的建议者,比如假设如果我输入 g 那么我应该选择图形和地理
{
"_source": [
"data3"
],
"query": {
"match": {
"data3": "g"
}
}
}
搜索结果:
"hits": [
{
"_index": "67260491",
"_type": "_doc",
"_id": "3",
"_score": 1.1123568,
"_source": {
"data3": "geopos"
}
},
{
"_index": "67260491",
"_type": "_doc",
"_id": "2",
"_score": 0.99270093,
"_source": {
"data3": "graphpos"
}
}
]
更新1:
您可以将索引映射更新为
PUT /<index-name>/_mapping
{
"properties": {
"data2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
推荐阅读
- vue.js - 如何使用 moment.js 从 vuetify v-text-field (type:time) 格式化 v-model 数据?
- bash - Shell / Bash:如何在命令行shell中未定义的函数上失败?
- outlook - 如何在 Outlook for Mac 中添加像 Cozi 这样的外部日历?
- sql-server - 如何自动创建另一个数据库的视图或同义词以及使用哪个
- vim - 如何记录 Vim 加密操作?
- ios - iPhoneXR 旋转后返回 rootview 控制器
- typescript - 方法装饰器 - 需要调用装饰器的类模块的完全限定路径
- delphi - 如何在delphi中为组件正确调用自定义组件编辑器表单
- pandas - 我需要在 python 2.7 中替换熊猫数据框列中的非 ASCII 字符
- php - 是否有更改 WordPress“重置密码表单”的钩子?