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

在ElasticSearch中加入查询

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

在ElasticSearch中加入查询

当您说加入时,这取决于您的打算。Elasticsearch与支持表之间的JOIN的常规数据库不同。它是一个文本搜索引擎,用于管理索引中的文档。

另一方面,您可以使用每种类型共有的字段在多个类型的同一索引内进行搜索。

例如,以您的数据为例,我可以创建一个具有2种类型的索引,它们的数据如下所示:

curl -XPOST localhost:9200/product -d '{    "settings" : {        "number_of_shards" : 5    }}'curl -XPOST localhost:9200/product/type1/_mapping -d '{        "type1" : { "properties" : {     "product_id" : { "type" : "string" },     "price" : { "type" : "integer" },     "stock" : { "type" : "integer" } }        }   }'curl -XPOST localhost:9200/product/type2/_mapping -d '{        "type2" : { "properties" : {     "product_id" : { "type" : "string" },     "category" : { "type" : "string" },     "manufacturer" : { "type" : "string" } }        }}'curl -XPOST localhost:9200/product/type1/1 -d '{        product_id: "1111",         price: "23",        stock: "100"}'curl -XPOST localhost:9200/product/type2/1 -d '{        product_id: "1111",        category: "iPhone case",        manufacturer: "Belkin"}'

我有效地创建了一个名为product的索引,具有2个type1和type2。现在,我可以执行以下查询,它将返回两个文档:

curl -XGET 'http://localhost:9200/product/_search?pretty=1' -d '{    "query": {        "query_string" : { "query" : "product_id:1111"        }    }}'{  "took" : 95,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 2,    "max_score" : 0.5945348,    "hits" : [ {      "_index" : "product",      "_type" : "type1",      "_id" : "1",      "_score" : 0.5945348, "_source" : {    product_id: "1111",    price: "23",    stock: "100"}    }, {      "_index" : "product",      "_type" : "type2",      "_id" : "1",      "_score" : 0.5945348, "_source" : {    product_id: "1111",    category: "iPhone case",    manufacturer: "Belkin"}    } ]  }}

原因是因为Elasticsearch将搜索该索引内的所有文档,而不管其类型如何。这与JOIN还是有区别的,在这种意义上,Elasticsearch不会对属于每种类型的文档进行笛卡尔积。

希望能有所帮助



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

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

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