阅读 194

es - elasticsearch search - inner hits

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :inner hits有什么特点?
答 :
在这里插入图片描述
问 :inner hits如何使用?
答 :

# inner hits
PUT /inner_hits_test
{
  "mappings" : {
    "dynamic"    : "strict",
    "properties" : {
      "nested_1" : {
        "type"       : "nested",
        "properties" : {
          "name"     : {
            "type" : "text"
          },
          "nested_2" : {
            "type"       : "nested",
            "properties" : {
              "age" : {
                "type" : "integer"
              }
            }
          }
        }
      },
      "join_1" : {
        "type"      : "join",
        "relations" : {
          "my_parent" : "my_child"
        } 
      }
    }
  }
}

# 索引
POST /inner_hits_test/_doc/1
{
  "nested_1" : [
    {
      "name"     : "hello",
      "nested_2" : [
        {
          "age" : "18"
        }
      ]
    }
  ],
  "join_1" : "my_parent"
}

# 索引
POST /inner_hits_test/_doc/2?routing=1
{
  "nested_1" : [
    {
      "name"     : "hello",
      "nested_2" : [
        {
          "age" : "19"
        }
      ]
    }
  ],
  "join_1" : {
    "name"   : "my_child",
    "parent" : 1
  }
}


# 搜索 - 正常
GET /inner_hits_test/_search
{
  "query" : {
    "nested" : {
      "path"  : "nested_1",
      "query" : {
        "match" : {
          "nested_1.name" : "hello"
        }
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "18"
                }
              ]
            }
          ],
          "join_1" : "my_parent"
        }
      },
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_routing" : "1",
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "19"
                }
              ]
            }
          ],
          "join_1" : {
            "name" : "my_child",
            "parent" : 1
          }
        }
      }
    ]
  }
}

# 搜索 - inner_hits
GET /inner_hits_test/_search
{
  "query" : {
    "nested" : {
      "path"  : "nested_1",
      "query" : {
        "match" : {
          "nested_1.name" : "hello"
        }
      },
      "inner_hits" : {}
    }
  }
}

# 结果
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "18"
                }
              ]
            }
          ],
          "join_1" : "my_parent"
        },
        "inner_hits" : {
          "nested_1" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 0.18232156,
              "hits" : [
                {
                  "_index" : "inner_hits_test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "nested_1",
                    "offset" : 0
                  },
                  "_score" : 0.18232156,
                  "_source" : {
                    "name" : "hello",
                    "nested_2" : [
                      {
                        "age" : "18"
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_routing" : "1",
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "19"
                }
              ]
            }
          ],
          "join_1" : {
            "name" : "my_child",
            "parent" : 1
          }
        },
        "inner_hits" : {
          "nested_1" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 0.18232156,
              "hits" : [
                {
                  "_index" : "inner_hits_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_nested" : {
                    "field" : "nested_1",
                    "offset" : 0
                  },
                  "_score" : 0.18232156,
                  "_source" : {
                    "name" : "hello",
                    "nested_2" : [
                      {
                        "age" : "19"
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

# 搜索 - inner_hits - 多层嵌套
GET /inner_hits_test/_search
{
  "query" : {
    "nested" : {
      "path"  : "nested_1.nested_2",
      "query" : {
        "match" : {
          "nested_1.nested_2.age" : "19"
        }
      },
      "inner_hits" : {}
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "19"
                }
              ]
            }
          ],
          "join_1" : {
            "name" : "my_child",
            "parent" : 1
          }
        },
        "inner_hits" : {
          "nested_1.nested_2" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "inner_hits_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_nested" : {
                    "field" : "nested_1",
                    "offset" : 0,
                    "_nested" : {
                      "field" : "nested_2",
                      "offset" : 0
                    }
                  },
                  "_score" : 1.0,
                  "_source" : {
                    "age" : "19"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

# 搜索 - join - 查子类
GET /inner_hits_test/_search
{
  "query" : {
    "has_child" : {
      "type"  : "my_child",
      "query" : {
        "nested" : {
          "path"  : "nested_1.nested_2",
          "query" : {
            "match" : {
              "nested_1.nested_2.age" : "19"
            }
          }
        }
      },
      "inner_hits" : {}
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "inner_hits_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "nested_1" : [
            {
              "name" : "hello",
              "nested_2" : [
                {
                  "age" : "18"
                }
              ]
            }
          ],
          "join_1" : "my_parent"
        },
        "inner_hits" : {
          "my_child" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "inner_hits_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : 1.0,
                  "_routing" : "1",
                  "_source" : {
                    "nested_1" : [
                      {
                        "name" : "hello",
                        "nested_2" : [
                          {
                            "age" : "19"
                          }
                        ]
                      }
                    ],
                    "join_1" : {
                      "name" : "my_child",
                      "parent" : 1
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
复制代码
文章分类
后端
文章标签