2013-05-16 132 views
6

我有一个弹性搜索索引与一个字段的精确匹配,并以某种方式我得到了很多类似的结果(我不介意)和那些类似的结果完全匹配,(我不介意)Elasticsearch不首先返回完全匹配

有人可以解释发生了什么以及如何解决它吗?

我的映射是这样的

"exact":{ 
    "type":"string", 
    "boost":10.0, 
    "analyzer":"keyword" 
}, 

我的查询,以搜索为“AAPL P 2014年1月885,00”是这样的:

{ 
    "size" : 21, 
    "query" : { 
    "field" : { 
     "exact" : "AAPL P JAN 2014 885,00" 
    } 
    }, 
    "explain" : true, 
    "sort" : [ { 
    "_score" : { 
     "order" : "desc" 
    } 
    } ], 
    "facets" : { 
    "category" : { 
     "terms" : { 
     "field" : "category", 
     "size" : 10 
     } 
    } 
    } 
} 

而返回的文档顺序结束:

  • { “精确”:[ “APPLE INC”, “US0378331005”, “AAPL”, “73773”], “ID-化合物”: “AAPL”}
  • {“exact”:[“AAPL”,“73773”,“AAPL P JAN 2014 675,00”],“id-compound”:“AAPL * PUT * 20140118 * 675”}
  • {“exact” :[ “AAPL”, “73773”, “AAPLç2014年1月500,00”], “ID-化合物”: “AAPL * CALL * 20140118 * 500”}

等,与完全匹配一个一线结果。

有人可以向我解释为什么精确匹配不会结束?

如果搜索结果有助于理解事情,那么以下全面说明的搜索结果如下。

"hits" : [ { 
    "_shard" : 0, 
    "_node" : "1", 
    "_index" : "instruments", 
    "_type" : "instrument", 
    "_id" : "AAPL", 
    "_score" : 1306.8339, "_source" : {"exact":["APPLE INC","US0378331005","AAPL","73773"],"id-compound":"AAPL"}, 
    "_explanation" : { 
    "value" : 1306.8339, 
    "description" : "product of:", 
    "details" : [ { 
     "value" : 6534.169, 
     "description" : "sum of:", 
     "details" : [ { 
     "value" : 6534.169, 
     "description" : "weight(exact:AAPL in 9096), product of:", 
     "details" : [ { 
      "value" : 0.25854474, 
      "description" : "queryWeight(exact:AAPL), product of:", 
      "details" : [ { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 0.0419026, 
      "description" : "queryNorm" 
      } ] 
     }, { 
      "value" : 25272.875, 
      "description" : "fieldWeight(exact:AAPL in 9096), product of:", 
      "details" : [ { 
      "value" : 1.0, 
      "description" : "tf(termFreq(exact:AAPL)=1)" 
      }, { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 4096.0, 
      "description" : "fieldNorm(field=exact, doc=9096)" 
      } ] 
     } ] 
     } ] 
    }, { 
     "value" : 0.2, 
     "description" : "coord(1/5)" 
    } ] 
    } 
}, { 
    "_shard" : 0, 
    "_node" : "1", 
    "_index" : "instruments", 
    "_type" : "instrument", 
    "_id" : "AAPL*PUT*20140118*675", 
    "_score" : 163.35423, "_source" : {"exact":["AAPL","73773","AAPL P JAN 2014 675,00"],"id-compound":"AAPL*PUT*20140118*675"}, 
    "_explanation" : { 
    "value" : 163.35423, 
    "description" : "product of:", 
    "details" : [ { 
     "value" : 816.7711, 
     "description" : "sum of:", 
     "details" : [ { 
     "value" : 816.7711, 
     "description" : "weight(exact:AAPL in 18), product of:", 
     "details" : [ { 
      "value" : 0.25854474, 
      "description" : "queryWeight(exact:AAPL), product of:", 
      "details" : [ { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 0.0419026, 
      "description" : "queryNorm" 
      } ] 
     }, { 
      "value" : 3159.1094, 
      "description" : "fieldWeight(exact:AAPL in 18), product of:", 
      "details" : [ { 
      "value" : 1.0, 
      "description" : "tf(termFreq(exact:AAPL)=1)" 
      }, { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 512.0, 
      "description" : "fieldNorm(field=exact, doc=18)" 
      } ] 
     } ] 
     } ] 
    }, { 
     "value" : 0.2, 
     "description" : "coord(1/5)" 
    } ] 
    } 
}, { 
    "_shard" : 0, 
    "_node" : "1", 
    "_index" : "instruments", 
    "_type" : "instrument", 
    "_id" : "AAPL*CALL*20140118*500", 
    "_score" : 163.35423, "_source" : {"exact":["AAPL","73773","AAPL C JAN 2014 500,00"],"id-compound":"AAPL*CALL*20140118*500"}, 
    "_explanation" : { 
    "value" : 163.35423, 
    "description" : "product of:", 
    "details" : [ { 
     "value" : 816.7711, 
     "description" : "sum of:", 
     "details" : [ { 
     "value" : 816.7711, 
     "description" : "weight(exact:AAPL in 383), product of:", 
     "details" : [ { 
      "value" : 0.25854474, 
      "description" : "queryWeight(exact:AAPL), product of:", 
      "details" : [ { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 0.0419026, 
      "description" : "queryNorm" 
      } ] 
     }, { 
      "value" : 3159.1094, 
      "description" : "fieldWeight(exact:AAPL in 383), product of:", 
      "details" : [ { 
      "value" : 1.0, 
      "description" : "tf(termFreq(exact:AAPL)=1)" 
      }, { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 512.0, 
      "description" : "fieldNorm(field=exact, doc=383)" 
      } ] 
     } ] 
     } ] 
    }, { 
     "value" : 0.2, 
     "description" : "coord(1/5)" 
    } ] 
    } 
}, { 
    "_id" : "AAPL*PUT*20140118*940", 
    "_score" : 163.35423, "_source" : {"exact":["AAPL","73773","AAPL P JAN 2014 940,00"],"id-compound":"AAPL*PUT*20140118*940"}, 
    "_explanation" : { 
    "value" : 163.35423, 
    "description" : "product of:", 
    "details" : [ { 
     "value" : 816.7711, 
     "description" : "sum of:", 
     "details" : [ { 
     "value" : 816.7711, 
     "description" : "weight(exact:AAPL in 794), product of:", 
     "details" : [ { 
      "value" : 0.25854474, 
      "description" : "queryWeight(exact:AAPL), product of:", 
      "details" : [ { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 0.0419026, 
      "description" : "queryNorm" 
      } ] 
     }, { 
      "value" : 3159.1094, 
      "description" : "fieldWeight(exact:AAPL in 794), product of:", 
      "details" : [ { 
      "value" : 1.0, 
      "description" : "tf(termFreq(exact:AAPL)=1)" 
      }, { 
      "value" : 6.1701355, 
      "description" : "idf(docFreq=211, maxDocs=37299)" 
      }, { 
      "value" : 512.0, 
      "description" : "fieldNorm(field=exact, doc=794)" 
      } ] 
     } ] 
     } ] 
    }, { 
     "value" : 0.2, 
     "description" : "coord(1/5)" 
    } ] 
    } 
} 

,只是在情况下的,如果我分析我试图存储中的数据会发生什么:

curl -XGET 'localhost:9200/instruments/_analyze?field=exact&pretty=true' -d 'ING P JUN 2013 6.00' 
{ 
    "tokens" : [ { 
    "token" : "ING P JUN 2013 6.00", 
    "start_offset" : 0, 
    "end_offset" : 20, 
    "type" : "word", 
    "position" : 1 
    } ] 

回答

0

所有三个文件得到完全相同的分数,你可以从解释输出看到他们都匹配“AAPL”。该术语在文档中总是出现一次(tf = 1),并且出现在37299个文档中的211个(idf = 6.1701355)。因为你正在使用索引时间提升(你的映射中的提升部分,10),所以现场常规要高得多,无论如何,没有什么大不了的,因为匹配总是在同一个域上。只是,如果你在其他领域有一场比赛,那么几乎总会赢,这对你的情况可能是有意义的。

但问题是,AAPL P JAN 2014 885,00是不完全匹配,如果我看你的文件。我所看到的是,在查询中的5个术语中只有一个匹配,这也由您的解释输出中的坐标确认:coord(1/5)`。

keyword分析器似乎被应用,但是从返回的文档中可以看出,您并不是将exact字段的内容作为单个值发送,而是作为一组值。由于您使用的是keyword分析仪,但其中每个项目都不会被标记,但仍有多个标记。我想你必须检查你如何索引文件。

+0

有一个在有精确匹配,它只是WAAAAAAAY下来。 –

+0

这是一个完全匹配你,而不是elasticsearch/lucene如果你看看分数。 – javanna

+0

什么我不明白它:为什么它甚至匹配...我使用关键字分析器,它应该分析整个字符串作为关键字...所以我正在阐述的是,当它搜索它转向整个事情变成一个令牌,它试图匹配。但显然这不是发生了什么事情。任何想法如何我可以得到我想要的行为?我只想要一场比赛,如果一切都匹配,直到空间。 (最好不区分大小写) –

1

我认为你已经找到了你的答案,只是想给其他同样的问题多一点信息。

您使用field查询从elasticsearch文档:

字段查询:

执行针对特定领域的查询字符串的查询。它是query_string查询的简化版本(通过将default_field设置为此查询执行的字段)。

我相信query_string查询文本,即:它做了很多的查询,使之模糊,等...

要使用(我想你发现了这一点什么)是一个term查询,它不会对搜索短语做任何事情,所以只给你完全匹配。

注意:分析发生在2个不同的时间,索引时间和查询时间。设置"analyzer": "keyword"似乎只影响搜索时间查询“使用查询字符串进行搜索时”form elasticsearch docs。我必须承认,我不知道究竟是什么意思(我猜query_string,但它也可能意味着像http://../_search?q=exact:{query here}搜索)

2

我不知道它是否技术上是最好的事情,但如果你只是在一个来自弹性搜索的单一特定答案,您可以使用一个过滤器和一个查找完全匹配的脚本。

{ 
    from : 0, 
    size : 1, 
    "query" : { 
    "text_phrase" : { 
     "title" : "AAPL P JAN 2014 885,00" 
    } 
    }, 
    "filter" : { 
    "script" : { 
     "script" : "_source.exact.contains(x)", 
     "params" : { 
     "x" : "AAPL P JAN 2014 885,00" 
     } 
    } 
    } 
} 

我用这个来获得弹性搜索一个已知的入口,它的工作很适合我。

1

你不应该分析你的id字段。

定义你的领域为:

"exact":{ 
    "type":"string", 
    "index":"not_analyzed" 
} 

看一看Finding Exact Values