3

ストアのインデックスを作成するためにdjango-haystackとElasticSearchを使用しています。

これまで、各店舗には1つの緯度と経度の座標ペアがありました。これを変更して、1つの店舗が非常に異なる地域に商品を配送できるという事実を表す必要がありました(分離)。最大10の場所(緯度と経度のペア)を追加しました。

1つのロケーションフィールドを使用すると、すべてが正常に機能し、正しい結果が得られました。現在、複数の場所フィールドがあるため、同じユーザーと店舗の座標について、以前の結果も含めて、結果を取得できません。

私のインデックスは次のとおりです。

class StoreIndex(indexes.SearchIndex,indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True,
                         template_name='search/indexes/store/store_text.txt')
    location0 = indexes.LocationField()
    location1 = indexes.LocationField()   
    location2 = indexes.LocationField()
    location3 = indexes.LocationField()
    location4 = indexes.LocationField()
    location5 = indexes.LocationField()
    location6 = indexes.LocationField()
    location7 = indexes.LocationField()
    location8 = indexes.LocationField()
    location9 = indexes.LocationField()

    def get_model(self):
        return Store

    def prepare_location0(self, obj):
        # If you're just storing the floats...
        return "%s,%s" % (obj.latitude, obj.longitude)

    # .....   up to prepare_location9
    def prepare_location9(self, obj):
        # If you're just storing the floats...
        return "%s,%s" % (obj.latitude_9, obj.longitude_9) 

これは私のインデックスを作成する正しい方法ですか?

Elasticsearchから、次のマッピング情報を取得します。

curl -XGET http://localhost:9200/stores/_mapping?pretty=True
{
  "stores" : {
    "modelresult" : {
      "properties" : {
    "django_id" : {
      "type" : "string"
    },
    "location0" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location1" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location2" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location3" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location4" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location5" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location6" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location7" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location8" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "location9" : {
      "type" : "geo_point",
      "store" : "yes"
    },
    "text" : {
      "type" : "string",
      "analyzer" : "snowball",
      "store" : "yes",
      "term_vector" : "with_positions_offsets"
    }
  }
  } 
 }
}

次に、次のようにクエリを実行します。

sqs0 = SearchQuerySet().dwithin('location0', usuario, max_dist).distance('location0',usuario).using('stores')

どこ:

usuario は、自分の位置の近くに店舗を見つけようとしているユーザーを表すPointインスタンスです。

max_distはDインスタンスです。

curlを使用して直接クエリを実行すると、結果も得られませんでした。

複数のロケーションフィールドでcurlを使用してクエリを実行した結果は次のとおりです。

$ curl -XGET http://localhost:9200/stores/modelresult/_search?pretty=true -d '{  "query" : { "match_all": {} },  "filter" : {"geo_distance" : { "distance" : "6km", "location0" : { "lat" : -23.5,  "lon" : -46.6 } } } } '
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
  "total" : 0,
  "max_score" : null,
  "hits" : [ ]
  }
}

StoreIndexクラスのフィールドlocation1〜9をコメントアウトすると、すべて正常に機能しますが、複数のロケーションポイントを取得するためにフィールドを離れると、同じクエリ(ユーザー位置)の結果が得られません。これは、直接と同じdjangoで、curlを使用して同じクエリで発生します。つまり、場所が1つしかない場合(たとえばlocation0)、両方のクエリが正しい結果を返します。より多くの場所(location0-9)では、両方のクエリで結果が得られませんでした。

ロケーションフィールドが1つだけのcurlを使用して直接クエリを実行した結果は次のとおりです。

 $ curl -XGET http://localhost:9200/stores/modelresult/_search?pretty=true -d '{     "query" : { "match_all": {} },  "filter" : {"geo_distance" : { "distance" : "6km", "location0" : { "lat" : -23.5,  "lon" : -46.6 } } } } '
{
"took" : 3,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
 },
"hits" : {
  "total" : 9,
  "max_score" : 1.0,
  "hits" : [ {
    "_index" : "stores",
    "_type" : "modelresult",
    "_id" : "store.store.110",
    "_score" : 1.0, "_source" : {"django_ct": "store.store", "text": "RESULT OF THE SEARCH \n\n", "django_id": "110", "id": "store.store.110", "location0": "-23.4487554,-46.58912"}
  },
  lot's of results here
   ]
  }
 }

もちろん、StoreIndexを変更した後はrebuild_indexを使用します。

複数のロケーションフィールドをelasticsearchとdjangoで動作させる方法について何か助けはありますか?

PS:この質問をDjango-HaystackとElasticSearchGoogleグループにクロス投稿しました。

https://groups.google.com/d/topic/elasticsearch/85fg7vdCBBU/discussion

https://groups.google.com/d/topic/django-haystack/m2A3_SF8-ls/discussion

前もって感謝します

マリオ

4

0 に答える 0