首页 > 解决方案 > 对同一个嵌套对象的两个查询..在不同的值上,然后期望 inner_hits 中有 2 个匹配的对象,但在 Elasticsearch 中只有 1 个

问题描述

我有一个 penDocuments 嵌套对象,如下所示。执行 must 子句中的两个不同查询。我期待 innner_hits 包含两个平底锅,护照对象。这意味着总点击数应该是 2,但有 1 个对象是 pan 。为什么护照对象没有显示在 innerHits 中?我的嵌套字段是

         "penDocuments": {
         "type": "nested",
         "properties": {
        "type": {
          "type": "text",
          "index_options": "docs"
        },
        "values": {
          "type": "text",
          "index_options": "docs",
          "analyzer": "lowercase_analyzer"
        }
      }
    }

我的数据:

                   "penDocuments":  
                       [
                        {
                           "type": "PAN",
                            "values": [
                              "735fHgBT1",
                              "Zwgt3ES21"
                                   ]
                                 },
                        {
                         "type": "Passport",
                         "values": [
                         "zzzzz",
                         "wwwww"
                           ]
                          }
                        ]

我的查询是:

                {
            "query": {
              "bool": {
                  "must": [
                {
             "constant_score": {
        "filter": {
          "nested": {
            "inner_hits": {
              "_source": true
            },
            "path": "penDocuments",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "penDocuments.type": "Passport"
                    }
                  },
                  {
                    "match": {
                      "penDocuments.values": "zzzzz"
                    }
                  }
                ]
              }
            }
          }
        },
        "boost": 100
      }
    },
    {
      "constant_score": {
        "filter": {
          "nested": {
            "inner_hits": {
              "_source": true
            },
            "path": "penDocuments",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "penDocuments.type": "PAN"
                    }
                  },
                  {
                    "match": {
                      "penDocuments.values": "735fHgBT1"
                    }
                  }
                ]
              }
            }
          }
        },
        "boost": 100
      }
    }
  ]
}

} }

在inner_hits

         {
   "inner_hits": {
"penDocuments": {
  "hits": {
    "total": 1,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "neglistindividual_junit",
        "_type": "details",
        "_id": "869",
        "_nested": {
          "field": "penDocuments",
          "offset": 0
        },
        "_score": 1.3862944,
        "_source": {
          "type": "PAN",
          "values": [
            "735fHgBT1",
            "Zwgt3ES21"
          ]
        }
      }
    ]
  }
}

} }

标签: elasticsearch

解决方案


我试着和你一起映射和查询。由于 lowercase_analyzer 的定义不存在,我创建了自己的。由于在查询中使用了多个 inner_hits,您的查询给出了错误,我为此做了一些小改动。结果我得到了两个文件

映射

PUT testindex12
{
  "mappings": {
    "properties": {
      "penDocuments": {
        "type": "nested",
        "properties": {
          "type": {
            "type": "text",
            "index_options": "docs"
          },
          "values": {
            "type": "text",
            "index_options": "docs",
            "analyzer":"lowercase_analyzer"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }
}

数据:

POST testindex12/_doc
{
  "penDocuments":  
                       [
                        {
                           "type": "PAN",
                            "values": [
                              "735fHgBT1",
                              "Zwgt3ES21"
                                   ]
                                 },
                        {
                         "type": "Passport",
                         "values": [
                         "zzzzz",
                         "wwwww"
                           ]
                          }
                        ]

询问:

GET testindex12/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "constant_score": {
            "filter": {
              "nested": {
                "inner_hits": {
                  "name": "Passport" ---> multiple innerhits need different name since they take name of path , which is same for both query
                },
                "path": "penDocuments",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "penDocuments.type": "Passport"
                        }
                      },
                      {
                        "match": {
                          "penDocuments.values": "zzzzz"
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 100
          }
        },
        {
          "constant_score": {
            "filter": {
              "nested": {
                "inner_hits": {
                  "name": "PAN" 
                },
                "path": "penDocuments",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "penDocuments.type": "PAN"
                        }
                      },
                      {
                        "match": {
                          "penDocuments.values": "735fHgBT1"
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 100
          }
        }
      ]
    }
  }
}

结果:

 "inner_hits" : {
          "Passport" : {   --->passport doc
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.3862944,
              "hits" : [
                {
                  "_index" : "testindex12",
                  "_type" : "_doc",
                  "_id" : "Lz47SG0BbzgYofLx6DbY",
                  "_nested" : {
                    "field" : "penDocuments",
                    "offset" : 1
                  },
                  "_score" : 1.3862944,
                  "_source" : {
                    "type" : "Passport",
                    "values" : [
                      "zzzzz",
                      "wwwww"
                    ]
                  }
                }
              ]
            }
          },
          "PAN" : {    --->pan doc
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.3862944,
              "hits" : [
                {
                  "_index" : "testindex12",
                  "_type" : "_doc",
                  "_id" : "Lz47SG0BbzgYofLx6DbY",
                  "_nested" : {
                    "field" : "penDocuments",
                    "offset" : 0
                  },
                  "_score" : 1.3862944,
                  "_source" : {
                    "type" : "PAN",
                    "values" : [
                      "735fHgBT1",
                      "Zwgt3ES21"
                    ]
                  }
                }
              ]
            }
          }
        }

推荐阅读