Elasticsearch 更喜欢这个查询使用 like_text
Elasticsearch more like this query using like_text
我在使用 more like this 查询时遇到了一些挫折。
这是我创建的索引:
curl -XPUT 'http://127.0.0.1:9200/food/' -d
'{
"mappings": {
"fruit": {
"properties": {
"term": {
"type": "string",
"term_vector": "yes"
}
}
}
}
}'
下面是该索引中的一段数据示例:
{
"_index": "food",
"_type": "fruit",
"_id": "2",
"_score": 1,
"_source": {
"term": "yellow",
"property_ids": [
1
],
"id": 2
}
}
最后这里是我用来尝试 return 数据的更像这个查询:
curl -XGET 'http://127.0.0.1:9200/food/_search' -d '{
"query": {
"more_like_this": {
"fields": [
"term"
],
"like_text": "yellow",
"min_term_freq": 1,
"max_query_terms": 12
}
}
}
'
问题是当我执行此搜索时没有返回任何结果:
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
如果我只对 "yellow" 执行标准通配符查询,我会返回该结果。我错过了什么?
默认情况下 min_doc_freq
为 5 (check doc) 因此,您的查询不起作用,因为您的索引不包含至少 5 个 term
属性 的文档持有 yellow
。因此,在您的查询中将 min_doc_freq
设置为 1,它应该可以工作。示例:
{
"query": {
"more_like_this": {
"fields": [
"term"
],
"like_text": "yellow",
"min_term_freq": 1,
"min_doc_freq": 1,
"max_query_terms": 12
}
}
}
我在使用 more like this 查询时遇到了一些挫折。
这是我创建的索引:
curl -XPUT 'http://127.0.0.1:9200/food/' -d
'{
"mappings": {
"fruit": {
"properties": {
"term": {
"type": "string",
"term_vector": "yes"
}
}
}
}
}'
下面是该索引中的一段数据示例:
{
"_index": "food",
"_type": "fruit",
"_id": "2",
"_score": 1,
"_source": {
"term": "yellow",
"property_ids": [
1
],
"id": 2
}
}
最后这里是我用来尝试 return 数据的更像这个查询:
curl -XGET 'http://127.0.0.1:9200/food/_search' -d '{
"query": {
"more_like_this": {
"fields": [
"term"
],
"like_text": "yellow",
"min_term_freq": 1,
"max_query_terms": 12
}
}
}
'
问题是当我执行此搜索时没有返回任何结果:
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
如果我只对 "yellow" 执行标准通配符查询,我会返回该结果。我错过了什么?
默认情况下 min_doc_freq
为 5 (check doc) 因此,您的查询不起作用,因为您的索引不包含至少 5 个 term
属性 的文档持有 yellow
。因此,在您的查询中将 min_doc_freq
设置为 1,它应该可以工作。示例:
{
"query": {
"more_like_this": {
"fields": [
"term"
],
"like_text": "yellow",
"min_term_freq": 1,
"min_doc_freq": 1,
"max_query_terms": 12
}
}
}