简体   繁体   English

ElasticSearch:是否可以突出显示查询中的单词而不是结果

[英]ElasticSearch: is it possible to highlight words in the query rather than the results

We use ElasticSearch in a reverse manner from what I usually see. 与我通常看到的相反,我们使用ElasticSearch。 We store lots of small documents, usually 1 or 2 words, for example, Job Titles like "software engineering", "car mechanics", "architect", etc. 我们存储许多小文件,通常为1或2个字,例如,“软件工程”,“汽车修理工”,“建筑师”等职位。

Then we query with a longer string, for example a 1000 word Job Spec. 然后,我们使用更长的字符串进行查询,例如1000字的工作规范。 This way we get all Job Titles present in the text of the Job Spec. 这样,我们就可以将所有职称显示在工作规范的文本中。

It works well. 它运作良好。 But I was wondering whether I could get ElasticSearch to highlight the matching Job Titles in the Job Spec, ie highlight the results in the query. 但是我想知道是否可以让ElasticSearch在工作规范中突出显示匹配的职位,即在查询中突出显示结果。 I have tried the highlight keyword, but it doesn't highlight the query text, it highlights the results. 我尝试了Highlight关键字,但是它没有突出显示查询文本,而是突出显示了结果。 I'm not sure how to get the query to be returned in the ElasticSearch response, let alone whether it can be highlighted. 我不确定如何使查询在ElasticSearch响应中返回,更不用说是否可以突出显示该查询了。

You might wonder why I need ElasticSearch to highlight the query, can't I just pick out all the results from the text and highlight them myself? 您可能想知道为什么我需要ElasticSearch突出显示查询,难道我不就只是从文本中挑选出所有结果并自己突出显示吗? Yes I can, but there's various things to think about that makes it hard such as stemming and stopword removal. 是的,我可以,但是有很多事情需要考虑,例如阻止词干和停用词删除。 for example "jquery" is stemmed to "jqueri" when doing the tokenising in ElasticSearch, so it's found as a result, but if I want to highlight it myself, I have to unstem it so it matches the original text. 例如,在ElasticSearch中进行标记化时,“ jquery”的词根会被“ jqueri”修饰,因此可以找到它,但是如果我要突出显示它,则必须对其进行取消设置以使其与原始文本匹配。 Elasticsearch also removes symbols, so terms & conditions would become terms conditions which is problematic if I want to highlight it manually as I have to add back the "&" symbol. Elasticsearch还会删除符号,因此terms & conditions将成为terms conditions ,如果我要手动突出显示它,因为必须加回“&”符号,这将是一个问题。 There's a hundred other problem cases, hence the question about whether ElasticSearch can do it for me. 还有其他一百个问题案例,因此是关于ElasticSearch是否可以为我做到的问题。

I'm quite sure highlighting the query string isn't possible - only highlighting parts of documents in an index. 我非常确定不可能突出显示查询字符串-仅突出显示索引中的文档部分。

What you might try is indexing the query string itself in it's own index and then using the results of the first query as the query terms for a second query against the query string (in the second index). 您可以尝试在自己的索引中为查询字符串本身建立索引,然后将第一个查询的结果用作针对查询字符串(在第二个索引中)的第二个查询的查询项。 You could then have highlighting on the query string. 然后,您可以突出显示查询字符串。 You'll have to make an extra request to ES each time, but I think it'll get what you want. 您每次都必须向ES提出额外的请求,但我认为它将满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM