栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在elasticsearch中避免嵌套类型的跨对象搜索行为

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在elasticsearch中避免嵌套类型的跨对象搜索行为

嵌套类型是您要寻找的,而不必太担心性能。

在为文档建立索引之前,您需要为文档设置映射:

curl -XDELETE localhost:9200/indexcurl -XPUT localhost:9200/indexcurl -XPUT localhost:9200/index/type/_mapping -d '{    "type": {        "properties": { "field_x": {     "type": "nested",     "include_in_parent": false,     "include_in_root": false,     "properties": {         "user": {  "type": "string"         },         "field_x": {  "type": "string",  "index" : "not_analyzed" // NOTE*         }     } }        }    }}'
  • 注意:如果您的字段仅包含“ A”和“ B”之类的单数字母,则您不想分析该字段,否则elasticsearch会删除这些单数字母“ words”。 如果这只是您的示例,并且您在实际的文档中正在搜索适当的单词,请删除此行,并让elasticsearch分析该字段。

然后,索引您的文档:

curl -XPUT http://localhost:9200/index/type/1 -d '{     "field_a": "foo",    "field_b": "bar",    "field_x" : [{        "user" : "1",        "field_x" : "A"    },    {        "user" : "2",        "field_x" : "B"    }]}'

并运行查询:

curl -XGET localhost:9200/index/type/_search -d '{     "query": {        "nested" : { "path" : "field_x", "score_mode" : "avg", "query" : {     "bool" : {         "must" : [  {      "term": {          "field_x.user": "1"      }  },  {      "term": {          "field_x.field_x": "A"      }  }         ]     } }        }    }}';

这将导致

{"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.987628,"hits":[{"_index":"index","_type":"type","_id":"1","_score":1.987628, "_source" : {     "field_a": "foo",    "field_b": "bar",    "field_x" : [{        "user" : "1",        "field_x" : "A"    },    {        "user" : "2",        "field_x" : "B"    }]}}]}}

但是,查询

curl -XGET localhost:9200/index/type/_search -d '{     "query": {        "nested" : { "path" : "field_x", "score_mode" : "avg", "query" : {     "bool" : {         "must" : [  {      "term": {          "field_x.user": "1"      }  },  {      "term": {          "field_x.field_x": "B"      }  }         ]     } }        }    }}';

不会返回任何结果

{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号