2

私はelasticsearchを見始めましたが、この操作をそれで実行できるかどうか疑問に思っています:(いくつか検索を行いましたが、何を探すべきかわかりません)。

次の 2 つのような連絡先データがあります。

{
  "id"     : "id1",
  "name"   : "Roger",
  "phone1" : "123",
  "phone2" : "",
  "phone3" : "980"
}

{
  "id"     : "id2",
  "name"   : "Lucas",
  "phone1" : "789",
  "phone2" : "123",
  "phone3" : ""
}

異なる電話フィールドにある場合でも、elasticsearch が電話番号の重複を見つけるのに役立つかどうか知りたいです (ここでは「123」が両方のレコードに存在します)。複数のフィールドで文字列を検索できることは既に説明したので、123 を検索すると、結果としてこれら 2 つのレコードを取得できます。ただし、次のようなものを返すことができるリクエストを発行する機能が必要です。

{
  "phones" : {
    "123" : ["id1", "id2"],
    "980" : ["id1"],
    "789" : ["id2"]
  }
}

または、これも便利です(番号を持つ連絡先の数):

{
  "phones" : {
    "123" : 2,
    "980" : 1,
    "789" : 1
  }
}

これが可能かどうか、何か考えはありますか? それができたらすごいことです。

4

2 に答える 2

4

データ構造を変更するという DrTech のアドバイスに同意します。ただし、何らかの理由でそのままにしておく場合は、複数フィールドの用語ファセットを使用して同じ結果を得ることができます。

curl "localhost:9200/phonefacet/_search?pretty=true&search_type=count" -d '{
    "query" : {
        "match_all" : {  }
    },
    "facets" : {
        "tag" : {
            "terms" : {
                "fields" : ["phone1", "phone2", "phone3"],
                "size" : 10
            }
        }
    }
}'

結果は次のようになります。

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "facets" : {
    "tag" : {
      "_type" : "terms",
      "missing" : 2,
      "total" : 4,
      "other" : 0,
      "terms" : [ {
        "term" : "123",
        "count" : 2
      }, {
        "term" : "980",
        "count" : 1
      }, {
        "term" : "789",
        "count" : 1
      } ]
    }
  }
}
于 2012-07-19T14:25:00.340 に答える
1

terms ファセットを使用してそこに到達することはできますが、データ構造を変更して、すべての電話番号を 1 つのフィールドに含める必要があります。

インデックスを作成します。

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' 

データのインデックスを作成します:

curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '
{
   "name" : "Roger",
   "id" : "id1",
   "phone" : [
      "123",
      "980"
   ]
}
'

curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '
{
   "name" : "Lucas",
   "id" : "id2",
   "phone" : [
      "789",
      "123"
   ]
}
'

すべてのフィールドを検索し、 内の用語の数を返しますphone:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "facets" : {
      "phone" : {
         "terms" : {
            "field" : "phone"
         }
      }
   }
}
'

# {
#    "hits" : {
#       "hits" : [
#          {
#             "_source" : {
#                "name" : "Roger",
#                "id" : "id1",
#                "phone" : [
#                   "123",
#                   "980"
#                ]
#             },
#             "_score" : 1,
#             "_index" : "test",
#             "_id" : "StaJK9A5Tc6AR7zXsEKmGA",
#             "_type" : "test"
#          },
#          {
#             "_source" : {
#                "name" : "Lucas",
#                "id" : "id2",
#                "phone" : [
#                   "789",
#                   "123"
#                ]
#             },
#             "_score" : 1,
#             "_index" : "test",
#             "_id" : "x8w39F-DR9SZOQoHpJw2FQ",
#             "_type" : "test"
#          }
#       ],
#       "max_score" : 1,
#       "total" : 2
#    },
#    "timed_out" : false,
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 5,
#       "total" : 5
#    },
#    "facets" : {
#       "phone" : {
#          "other" : 0,
#          "terms" : [
#             {
#                "count" : 2,
#                "term" : "123"
#             },
#             {
#                "count" : 1,
#                "term" : "980"
#             },
#             {
#                "count" : 1,
#                "term" : "789"
#             }
#          ],
#          "missing" : 0,
#          "_type" : "terms",
#          "total" : 4
#       }
#    },
#    "took" : 5
# }
于 2012-07-19T13:20:42.893 に答える