0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElasticSearchでnestedを対象としたsearchQueryはpathをつけよう!

Posted at

概要

ElasticSearchで作成したIndexで、nestedで設定したfieldの検索で nested検索を利用すると取得出来るかもしれないといった奮闘記になります

前提

ElasticSearchに作成したIndex/Mapping/Documentは以下の通りです

# Index/Mapping
$ curl -H "Content-Type: application/json" -XPUT '127.0.0.1:9200/hoge_index?pretty' -d '
{
	"mappings": {
		"properties": {
			"company": {
				"type": "nested",
				"properties": {
					"user": {
						"properties": {
							"id": {
								"type": "long"
							}
						}
					}
				}
			}
		}
	}
}'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "hoge_index"
}

# Document
$ curl -H "Content-Type: application/json" -XPOST '127.0.0.1:9200/hoge_index/_doc/?pretty' -d '{
    "company":{"user":{"id":1}}
  }'
{
  "_index" : "hoge_index",
  "_type" : "_doc",
  "_id" : "9lYhJYwBYAWRxjRwHMJl",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

失敗した検索

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
上記を参考に、以下のようなQueryを作って検索してみました

$ curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/hoge_index/_search?pretty' -d '
{
  "query": {
    "bool": {
      "filter": [
        {"term": {"company.user.id": 1}}
      ]
    }
  }
}'

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

なんかそれっぽく作ってみたら、NoHit、つまりは検索が出来ませんでした

成功した検索

nestedの場合のQueryもあるっぽく、参考に作ってみました

$ curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/hoge_index/_search?pretty' -d '
{
  "query": {
    "bool": {
      "filter": [
        {
            "nested": {
                "path": "company",
                "query": {
                    "term": {
                        "company.user.id": 1
                    }
                }
            }
        }
      ]
    }
  }
}'

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "hoge_index",
        "_type" : "_doc",
        "_id" : "9lYhJYwBYAWRxjRwHMJl",
        "_score" : 0.0,
        "_source" : {
          "company" : {
            "user" : {
              "id" : 1
            }
          }
        }
      }
    ]
  }
}

nestedにpath/queryを繋ぐと、欲しいDocumentが検索できるようになりました

備考

ElasticSearchの公式やStackOverflowでも、search時のnestedの扱い方が探せなかったので、公式での使い方を見てみたい気持ちはあります...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?