others - 查询Elasticsearch索引,索引与文档不是1:1的数据关系

137 0


Book 


|->belongs to Author



我在ES索引中将它非规范化,得到如下行:


book1 author1


book2 author1


book3 author2


book4 author3



这个索引应该会回答关于book的查询,因为每个文档都是1:1,带有book 。

但是,如果我想查询回答有关author的问题怎么办?每个文档与author都不是1:1,如上面的例子所示: 有两行,值为author1

时间: 原作者:

79 5

它取决于你的数据集有多复杂,如果你有大量的字段需要规范化,可以使用Terms查找和分隔Author和Book单独的索引,并且使用id。

但是,如果数据集不太大,那么你可以像下面例子那样简单处理:

你可以搜索author或book,它将返回与查询匹配的所有文档,


PUT t1



POST t1/_doc/1


{


"book":"book1",


"author":"author1"


}



POST t1/_doc/2


{


"book":"book2",


"author":"author1"


}



POST t1/_doc/3


{


"book":"book3",


"author":"author2"


}



POST t1/_doc/4


{


"book":"book4",


"author":"author3"


}



查询1book查找—返回book 1


 POST t1/_search


 {


"query": {


"terms": {


"book": [


"book1"


 ]


 }


 }


 }



 #Response


 {


"took" : 0,


"timed_out" : false,


"_shards" : {


"total" : 1,


"successful" : 1,


"skipped" : 0,


"failed" : 0


 },


"hits" : {


"total" : {


"value" : 1,


"relation" :"eq"


 },


"max_score" : 1.0,


"hits" : [


 {


"_index" :"t1",


"_type" :"_doc",


"_id" :"1",


"_score" : 1.0,


"_source" : {


"book" :"book1",


"author" :"author1"


 }


 }


 ]


 }


 }



查询2--author 查找—返回book1和book2


POST t1/_search


{


"query": {


"terms": {


"author": [


"author1"


 ]


 }


 }


}



#Author Response


{


"took" : 0,


"timed_out" : false,


"_shards" : {


"total" : 1,


"successful" : 1,


"skipped" : 0,


"failed" : 0


 },


"hits" : {


"total" : {


"value" : 2,


"relation" :"eq"


 },


"max_score" : 1.0,


"hits" : [


 {


"_index" :"t1",


"_type" :"_doc",


"_id" :"1",


"_score" : 1.0,


"_source" : {


"book" :"book1",


"author" :"author1"


 }


 },


 {


"_index" :"t1",


"_type" :"_doc",


"_id" :"2",


"_score" : 1.0,


"_source" : {


"book" :"book2",


"author" :"author1"


 }


 }


 ]


 }


}



原作者:
...