簡體   English   中英

如何在彈性搜索的嵌套字段中制作扁平子字段?

[英]How to make flattened sub-field in the nested field in elastic search?

在這里,我有一個索引文檔,如:

doc = {  
  "id": 1,  
  "content": [  
    {  
      "txt": I,  
      "time": 0,  
    },  
    {  
      "txt": have,  
      "time": 1,  
    },  
    {  
      "txt": a book,  
      "time": 2,  
    },  
    {  
      "txt": do not match this block,  
      "time": 3,  
    },  
  ]  
}  

我想匹配“我有一本書”,並返回匹配的時間:0,1,2。 有沒有人知道如何為這種情況構建索引和查詢? 我認為“content.txt”應該變平但“content.time”應該嵌套?

想要匹配“我有一本書”,並返回匹配的時間:0,1,2。

添加帶有索引映射、搜索查詢和搜索結果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "content": {
        "type": "nested"
      }
    }
  }
}

搜索查詢:

{
  "query": {
    "nested": {
      "path": "content",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "content.txt": "I have a book"
              }
            }
          ]
        }
      },
      "inner_hits": {}
    }
  }
}

搜索結果:

"inner_hits": {
          "content": {
            "hits": {
              "total": {
                "value": 3,
                "relation": "eq"
              },
              "max_score": 2.5226097,
              "hits": [
                {
                  "_index": "64752029",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "content",
                    "offset": 2
                  },
                  "_score": 2.5226097,
                  "_source": {
                    "txt": "a book",
                    "time": 2
                  }
                },
                {
                  "_index": "64752029",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "content",
                    "offset": 0
                  },
                  "_score": 1.5580825,
                  "_source": {
                    "txt": "I",
                    "time": 0
                  }
                },
                {
                  "_index": "64752029",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "content",
                    "offset": 1
                  },
                  "_score": 1.5580825,
                  "_source": {
                    "txt": "have",
                    "time": 1
                  }
                }
              ]
            }
          }
        }
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM