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"


 }


 }


 ]


 }


}



原作者:
...