【问题标题】:Elasticsearch parent - child mapping: Search in both and highlightElasticsearch 父子映射:同时搜索并突出显示
【发布时间】:2015-12-15 15:44:25
【问题描述】:

我有以下 elasticsearch 1.6.2 索引映射:父 item 和子 document。一个项目可以有多个文档。文档不是嵌套的,因为它们包含 base64 数据(mapper-attachments-plugin)并且无法使用项目进行更新。

"mappings" : {
  "document" : {
    "_parent" : {
      "type" : "item"
    }, 
    "_routing" : {
      "required" : true
    },
    "properties" : {
      "extension" : {
        "type" : "string",
        "term_vector" : "with_positions_offsets",     
        "include_in_all" : true
      }, ...
    },
 }
 "item" : { 
    "properties" : {
      "prop1" : {
        "type" : "string",
        "include_in_all" : true
      }, ...
   }
}

我喜欢在两个索引中搜索,但总是返回items。如果文档中有匹配项,则返回相应的项目。如果项目中有匹配项,则返回该项目。如果两者都为真,则返回该项目。

是否可以结合 has_childhas_parent 搜索?

此搜索仅在文档中搜索并返回项目:

{
 "query": {
  "has_child": {
    "type": "document",
    "query": {
      "query_string":{"query":"her*}
  },
     "inner_hits" : {
      "highlight" : {
             "fields" : {
                 "*" : {}              
             }
           }
       }
}

示例

GET index/item/174
{
  "_type" : "item",
  "_id" : "174",
  "_source":{"prop1":"Perjeta construction"}
}

GET index/document/116
{
  "_type" : "document",
  "_id" : "116", 
  "_source":{"extension":"pdf","item": {"id":174},"fileName":"construction plan"}
} 

__POSSIBLE SEARCH RESULT searching for "constr*"__

{
"hits": {
  "total": 1,
  "hits": [
     {
        "_type": "item",
        "_id": "174",
        "_source": {
           "prop1": "Perjeta construction"
        },
        "highlight": {
           "prop1": [
              "Perjeta <em>construction<\/em>"
           ]
        },
        "inner_hits": {
           "document": {
              "hits": {
                 "hits": [
                    {
                       "_type": "document",
                       "_id": "116",
                       "_source": {
                          "extension": "pdf",
                          "item": {
                             "id": 174
                          },                              
                          "fileName": "construction plan"
                       },
                       "highlight": {
                          "fileName": [
                             "<em>construction<\/em> plan"
                          ]
                       }
                    }
                 ]
              }
           }
        }
     }
  ]
}
}

【问题讨论】:

  • 您能否提供一些示例文档以及您希望看到的查询输出?

标签: elasticsearch


【解决方案1】:

我可以用回答我的问题“是否可以将 has_child 和 has_parent 结合起来”。

您一次只能在一个索引上使用一个。

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 2019-06-18
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多