是否可以在 Compass 中进行基于日期的查询时间提升?

发布于 2024-10-18 14:45:28 字数 94 浏览 2 评论 0原文

我正在尝试提高 Compass 索引中最近项目的查询时间。我尝试在类映射上使用属性,但这似乎只影响索引时间期间的提升,而不影响查询时间的提升。

有什么想法吗?

I'm trying to get a query-time boost on recent items in a Compass index. I have tried using a property on my class mapping, but this only seems to affect the boost during index-time, not query-time.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-10-25 14:45:28
            DefaultCompassQuery query = (DefaultCompassQuery) compassBuilderQuery.toQuery();
            query.setTypes( types.toArray( new Class[types.size()] ) );
            LuceneSearchEngineQuery searchEngineQuery = (LuceneSearchEngineQuery) query.getSearchEngineQuery();
            final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
            final long timeInMillis = Calendar.getInstance().getTimeInMillis();
            ValueSourceQuery valSrcQuery = new ValueSourceQuery( new ValueSource() {

                private static final long serialVersionUID = 1L;

                @Override
                public int hashCode() {
                    return System.identityHashCode( this );
                }

                @Override
                public DocValues getValues( final IndexReader reader ) throws IOException {
                    return new DocValues() {

                        @Override
                        public float floatVal( int doc ) {
                            try {
                                Document document = reader.document( doc );
                                Field field = document.getField( "date" );
                                if (null != field) {
                                    Date parse = sdf.parse( field.stringValue() );

                                    long t = timeInMillis - parse.getTime();

                                    float f = (1.0f / (t * (1.0f / TimeUnit.DAYS.toMillis( 30 )) + 1.0f));
                                    if (logger.isDebugEnabled()) {
                                        logger.debug( "Date match: " + parse.toString() );
                                        logger.debug( "Calculated date boost as: " + f + " for doc id: " + doc );
                                    }
                                    return f;
                                }
                            } catch (CorruptIndexException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            return 1.0f;
                        }

                        @Override
                        public String toString( int doc ) {
                            return description() + "=" + strVal( doc );
                        }

                    };
                }

                @Override
                public boolean equals( Object o ) {
                    return this == o;
                }

                @Override
                public String description() {
                    return "[boost: date]";
                }
            } );
            CustomScoreQuery sq = new CustomScoreQuery( searchEngineQuery.getQuery(), valSrcQuery );
            searchEngineQuery.setQuery( sq );

其中“date”是文档中您想要增强的日期字段,compassBuilderQuery 是您使用指南针查询生成器生成的查询。您还可以调整那里的“30”,使日期根据年龄增加或减少。

            DefaultCompassQuery query = (DefaultCompassQuery) compassBuilderQuery.toQuery();
            query.setTypes( types.toArray( new Class[types.size()] ) );
            LuceneSearchEngineQuery searchEngineQuery = (LuceneSearchEngineQuery) query.getSearchEngineQuery();
            final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
            final long timeInMillis = Calendar.getInstance().getTimeInMillis();
            ValueSourceQuery valSrcQuery = new ValueSourceQuery( new ValueSource() {

                private static final long serialVersionUID = 1L;

                @Override
                public int hashCode() {
                    return System.identityHashCode( this );
                }

                @Override
                public DocValues getValues( final IndexReader reader ) throws IOException {
                    return new DocValues() {

                        @Override
                        public float floatVal( int doc ) {
                            try {
                                Document document = reader.document( doc );
                                Field field = document.getField( "date" );
                                if (null != field) {
                                    Date parse = sdf.parse( field.stringValue() );

                                    long t = timeInMillis - parse.getTime();

                                    float f = (1.0f / (t * (1.0f / TimeUnit.DAYS.toMillis( 30 )) + 1.0f));
                                    if (logger.isDebugEnabled()) {
                                        logger.debug( "Date match: " + parse.toString() );
                                        logger.debug( "Calculated date boost as: " + f + " for doc id: " + doc );
                                    }
                                    return f;
                                }
                            } catch (CorruptIndexException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            return 1.0f;
                        }

                        @Override
                        public String toString( int doc ) {
                            return description() + "=" + strVal( doc );
                        }

                    };
                }

                @Override
                public boolean equals( Object o ) {
                    return this == o;
                }

                @Override
                public String description() {
                    return "[boost: date]";
                }
            } );
            CustomScoreQuery sq = new CustomScoreQuery( searchEngineQuery.getQuery(), valSrcQuery );
            searchEngineQuery.setQuery( sq );

Where 'date' is a date field on your document that you want to boost and compassBuilderQuery is a query you have generated with the compass query builder. You can also tweak the '30' up there to make the dates boosted less or more based on their age.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文