我可以按公式对分组的搜索结果进行排序吗?

时间:2019-09-06 11:36:19

标签: elasticsearch

我正在尝试实现将按公式对汇总结果进行排序的查询。

例如,我们有下一个实体:

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Scratch {

    private static final int DOC_COUNT = 500;
    private static final int ITERATION_COUNT = 5;

    private static final boolean ANONYMOUS_CHILDREN = true;
    private static final boolean LABELED_CHILDREN = false;

    public static void main(String[] args) throws IOException, SolrServerException {
        long anonymousTime = 0;
        long labelledTime = 0;

        for (int i = 0; i < ITERATION_COUNT; i++) {
            List<SolrInputDocument> anonymousDocs = createSolrDocuments(ANONYMOUS_CHILDREN);
            cleanSolrCollection();
            anonymousTime += writeToSolr(anonymousDocs);

            List<SolrInputDocument> labeledDocs = createSolrDocuments(LABELED_CHILDREN);
            cleanSolrCollection();
            labelledTime += writeToSolr(labeledDocs);
        }

        System.out.println("Avg anonymous time: " + (anonymousTime / ITERATION_COUNT));
        System.out.println("Avg labelled time: " + (labelledTime / ITERATION_COUNT));
    }

    private static List<SolrInputDocument> createSolrDocuments(boolean isAnonymous) {
        List<SolrInputDocument> request = new ArrayList<>();

        for (int i = 0; i < DOC_COUNT; i++) {
            SolrInputDocument parent = new SolrInputDocument();
            parent.setField("id", "parent_" + i);

            SolrInputDocument child = new SolrInputDocument();
            child.setField("id", "child_" + i);

            if (isAnonymous) {
                parent.addChildDocument(child);
            } else {
                parent.addField("items", child);
            }
            request.add(parent);
        }
        return request;
    }

    private static void cleanSolrCollection() throws IOException, SolrServerException {
        try (SolrClient client = getSolrClient()) {
            client.deleteByQuery("main", "*:*");
        }
    }

    private static long writeToSolr(List<SolrInputDocument> documents) throws IOException, SolrServerException {
        long startAt = System.currentTimeMillis();
        try (SolrClient client = getSolrClient()) {
            client.add("main", documents);
        }
        return System.currentTimeMillis() - startAt;
    }

    private static SolrClient getSolrClient() {
        return new HttpSolrClient.Builder("http://localhost:8983/solr")
                .allowCompression(true)
                .build();
    }

}

现在,我没有排序的查询如下:

{
"price":"1000",
"zip":"77777",
"field1":"1",
"field2":"5"
},
{
"price":"2222",
"zip":"77777",
"field1":"2",
"field2":"5"
},
{
"price":"1111",
"zip":"77777",
"field1":"1",
"field2":"5"
}

第一个我需要将结果按field1和field2分组,然后计算每组的平均价格。

然后,我需要将每个文档的价格除以平均价格值,然后根据该价格对文档进行排序。

是否可以通过某种方式做到这一点?

0 个答案:

没有答案