[英]Elastic search give priority to starts with match
I am using elasticsearch 6.8 version for document indexing and I have this requirement when searching.我正在使用 elasticsearch 6.8 版本进行文档索引,搜索时我有这个要求。 I have created an index with the following settings.
我使用以下设置创建了一个索引。
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"analysis": {
"analyzer": {
"default": {
"type": "custom",
"tokenizer": "icu_tokenizer"
}
}
}
},
"mappings": {
"person": {
"properties": {
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
},
"technologies": {
"type": "nested"
}
}
}
}
}
For example, I am creating 3 documents with following technologies.例如,我正在使用以下技术创建 3 个文档。
So when search by keyword - "test", it is returning all the documents, that is correct behavior, but I want the 2nd document get priority because it starts with test*.因此,当按关键字搜索 - “test”时,它返回所有文档,这是正确的行为,但我希望第二个文档获得优先级,因为它以 test* 开头。 So my requirement is to get starts with value the priority as below.
所以我的要求是从价值优先级开始,如下所示。
Search results should be:搜索结果应该是:
Here is my java code for searching.这是我用于搜索的java代码。 I am using elasticsearch-rest-high-level-client library.
我正在使用 elasticsearch-rest-high-level-client 库。
SearchRequest searchRequest = new SearchRequest("profile1"); SearchRequest searchRequest = new SearchRequest("profile1"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.boolQuery()
.must(QueryBuilders
.matchQuery("technologies.name", technology));
searchSourceBuilder.query(QueryBuilders
.nestedQuery("technologies",
queryBuilder,
ScoreMode.Avg));
searchRequest.source(searchSourceBuilder);
SearchResponse response =
client.search(searchRequest, RequestOptions.DEFAULT);
Is there something I am missing searching documents?我在搜索文档时遗漏了什么吗? Please help me on this.
请帮我解决这个问题。 Thanks.
谢谢。
I managed to find more useful answers from the following links.我设法从以下链接中找到了更多有用的答案。
https://www.elastic.co/blog/starts-with-phrase-matching https://www.elastic.co/blog/starts-with-phrase-matching
https://gist.github.com/polyfractal/4704772 https://gist.github.com/polyfractal/4704772
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.