Не удается получить запрос на объединение в эластичном - PullRequest
1 голос
/ 23 сентября 2019

Я получил версию эластичного поиска 7.3 и два индекса, profiles и purchases, вот их сопоставления:

\purchases
{
    "purchases": {
        "mappings": {
            "properties": {
                "product": {
                    "type": "keyword"
                },
                "profile": {
                    "type": "join",
                    "eager_global_ordinals": true,
                    "relations": {
                        "profiles": "purchases"
                    }
                }
            }
        }
    }
}

\profiles
{
    "profiles": {
        "mappings": {
            "properties": {
                "user": {
                    "type": "keyword"
                }
            }
        }
    }
}

Я добавил один профиль с user:abc, _id:1 и две покупки таким образом

{
    "profile": {"name": "profiles", "parent": "1"},
    "product" : "tomato",
}
{
    "profile": {"name": "profiles", "parent": "1"},
    "product" : "tomato 2",
}

Тогда я делаю поисковый запрос для покупок

{
    "query": {
    "has_parent": {
      "parent_type": "profiles",
      "query": {
        "query_string": {
          "query": "user:abc"
        }
      }
    }
  }
}

И я получаю пустой результат, что не так?

1 Ответ

1 голос
/ 23 сентября 2019

Как указано в документации к типу Join , вы можете not создавать отношения родитель-ребенок по нескольким индексам:

Тип соединенияспециальное поле, которое создает отношения родитель / потомок в документах с одинаковым индексом .

Если вы хотите использовать тип данных соединения, вы должны смоделировать его в один индекс .

ОБНОВЛЕНИЕ

Вот как будет выглядеть ваше отображение и индексирование документов:

PUT profiles-purchases-index
{
  "mappings": {
    "properties": {
      "user":{
        "type": "keyword"
      },
      "product":{
        "type": "keyword"
      },
      "profile":{
        "type": "join",
        "relations":{
          "profiles": "purchases"
        }
      }
    }
  }
}

Индекс родительского документа:

PUT profiles-purchases-index/_doc/1
{
  "user": "abc",
  "profile": "profiles"
}

Индекс дочерних документов:

PUT profiles-purchases-index/_doc/2?routing=1
{
  "product": "tomato",
  "profile":{
    "name": "purchases",
    "parent": 1
  }
}

PUT profiles-purchases-index/_doc/3?routing=1
{
  "product": "tomato 2",
  "profile":{
    "name": "purchases",
    "parent": 1
  }
}

Выполнить запрос:

GET profiles-purchases-index/_search
{
  "query": {
    "has_parent": {
      "parent_type": "profiles",
      "query": {
        "match": {
          "user": "abc"
        }
      }
    }
  }
}

Ответ:

{
  ...
    "hits" : [
      {
        "_index" : "profiles-purchases-index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "product" : "tomato",
          "profile" : {
            "name" : "purchases",
            "parent" : 1
          }
        }
      },
      {
        "_index" : "profiles-purchases-index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "product" : "tomato 2",
          "profile" : {
            "name" : "purchases",
            "parent" : 1
          }
        }
      }
    ]
  }
}

Обратите внимание, что необходимо настроить параметр маршрутизации для индексирования дочерних документов.Но, пожалуйста, обратитесь к документации для этого.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...