我正在嘗試基于多個引數進行搜索。例如,我的檔案結構如下,
{
"id": "101",
"start_time": "2021-12-13T06:57:29.420198Z",
"end_time": "2021-12-13T07:00:23.511722Z",
"data": [{
"starttimestamp": "2022-01-03T11:21:22.107413Z",
"user": "John",
"speech": "Thanks. I’m just happy that it’s over. I was really nervous about it.",
"endtimestamp": "2022-01-03T11:21:22.247482Z"
},
{
"starttimestamp": "2022-01-03T11:21:26.905401Z",
"user": "Tom",
"speech": "Well, I’m sure you did great. where is the university",
"endtimestamp": "2022-01-03T11:21:27.065316Z"
},
{
"starttimestamp": "2022-01-03T11:21:33.165617Z",
"user": "John",
"speech": "university is in canada.",
"endtimestamp": "2022-01-03T11:21:33.165900Z"
}
]
}
現在我需要搜索一個特定的用戶,
例如:正例:搜索用戶是“John”的檔案,他在 starttimestamp “2022-01-03T11:21:33.165617Z”輸出中談到了“canada”:我們應該得到上述查詢的結果
否定案例:搜索一個檔案,其中用戶“Tom”在 starttimestamp “2022-01-03T11:21:33.165617Z” 輸出時談到“加拿大”:結果在這里應該是空的,因為“Tom”沒有談到“加拿大”
我嘗試使用以下查詢來實作預期的 o/p,
{
"query": {
"bool": {
"must": [
{
"term": {"data.user": "Tom"}
},
{
"bool": {
"must": [
{"term": {"data.speech": "canada"}}
]
}
}
]
}
}
}
在上面的查詢中,我們應該得到一個空串列,但結果是上面的檔案。
我參考了其他一些資源: 基于條件的彈性搜索查詢(if else 或 Union case) 在 ElasticSearch 中使用多個欄位和條件進行查詢
我沒有找到可以解決我的問題的確切資訊。
uj5u.com熱心網友回復:
請查看此檔案以了解陣列欄位在 Elasticsearch 中的作業方式。當您使用物件陣列時,您不能單獨查詢每個物件。
物件陣列不會像您期望的那樣作業:您不能獨立于陣列中的其他物件查詢每個物件。如果您需要能夠做到這一點,那么您應該使用嵌套資料型別而不是物件資料型別。
您可以使用以下示例創建索引:
PUT index_name
{
"mappings": {
"properties": {
"data":{
"type": "nested"
}
}
}
}
索引下面的檔案:
POST index_name/_doc
{
"id": "101",
"start_time": "2021-12-13T06:57:29.420198Z",
"end_time": "2021-12-13T07:00:23.511722Z",
"data": [
{
"starttimestamp": "2022-01-03T11:21:22.107413Z",
"user": "John",
"speech": "Thanks. I’m just happy that it’s over. I was really nervous about it.",
"endtimestamp": "2022-01-03T11:21:22.247482Z"
},
{
"starttimestamp": "2022-01-03T11:21:26.905401Z",
"user": "Tom",
"speech": "Well, I’m sure you did great. where is the university",
"endtimestamp": "2022-01-03T11:21:27.065316Z"
},
{
"starttimestamp": "2022-01-03T11:21:33.165617Z",
"user": "John",
"speech": "university is in canada.",
"endtimestamp": "2022-01-03T11:21:33.165900Z"
}
]
}
示例查詢:
POST index_name/_search
{
"query": {
"nested": {
"path": "data",
"query": {
"bool": {
"must": [
{
"match": {
"data.user": "John"
}
},
{
"match": {
"data.speech": "canada"
}
}
]
}
}
}
}
}
請查看此博客以更清楚地了解物件和嵌套型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444301.html
