使用字段代替“ _id”进行类似查询

时间:2019-06-04 01:35:55

标签: elasticsearch morelikethis

我有一个子字段,而不是“ _id”字段,我想用它来标识要用作引用的对象。但是doc似乎没有将其用作参考,而是将其用作比较的查询。由于slug是一个带有简单分析器的唯一字段,因此它仅返回一个完全如下的结果。据我所知,没有办法将自定义字段用作_id字段: https://www.youtube.com/watch?v=HZpq46W4xo4

是双重查询吗?先找出elasticsearch的id,然后执行more_like_this是实现我所寻找的唯一方法? https://github.com/elastic/elasticsearch/issues/6730

ArticleDocument.search().query("bool",
                                should=Q("more_like_this",
                                        fields= ["slug", "text"],
                                        like={"doc": {"slug": "OEXxySDEPWaUfgTT54QvBg",
                                        }, "_index":"article", "_type":"doc"},
                                        min_doc_freq=1,
                                        min_term_freq=1
                                        )
                                ).to_queryset()

返回:

<ArticleQuerySet [<Article: OEXxySDEPWaUfgTT54QvBg)>]>

1 个答案:

答案 0 :(得分:0)

在导入数据时,您可以将某些文档字段设置为“默认” _id。

Logstash

output {
    elasticsearch {
        hosts => ["localhost:9200"]
        index => "my_name"
        document_id => "%{some_field_id}"
    }
}

火花(斯卡拉)

DF.saveToEs("index_name" + "/some_type", Map("es.mapping.id" -> "some_field_id"))

索引API

PUT twitter/_doc/1
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

{
    "_shards" : {
        "total" : 2,
        "failed" : 0,
        "successful" : 2
    },
    "_index" : "twitter",
    "_type" : "_doc",
    "_id" : "1",
    "_version" : 1,
    "_seq_no" : 0,
    "_primary_term" : 1,
    "result" : "created"
}