【问题标题】:How to make flattened sub-field in the nested field in elastic search?如何在弹性搜索的嵌套字段中制作扁平子字段?
【发布时间】:2020-11-09 21:09:37
【问题描述】:

在这里,我有一个像这样的索引文档:

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”应该嵌套?

【问题讨论】:

    标签: elasticsearch search-engine


    【解决方案1】:

    想匹配“我有一本书”,返回匹配的时间: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
                      }
                    }
                  ]
                }
              }
            }
          }
    

    【讨论】:

    • 这个答案的一个问题是,如果第四个元素有任何搜索词(例如do not match this book)或者如果您只是搜索I have this book,那么它也会显示出来,这可能不是你想要的
    • 您好,我还有一个关于在嵌套字段中使用扁平结构的问题,您可以看看这个:stackoverflow.com/questions/64762234/… 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多