Package org.elasticsearch.search.builder

Examples of org.elasticsearch.search.builder.SearchSourceBuilder$ScriptField


                                        types = (String[]) ((List) mapQuery.get("types")).toArray(new String[0]);
                                    } else if (mapQuery.get("types") instanceof String) {
                                        types = new String[]{((String) mapQuery.get("types"))};
                                    }

                                    SearchSourceBuilder searchSourceBuilder = null;
                                    if (mapQuery.get("sort") instanceof List) {
                                        if (searchSourceBuilder == null) {
                                            searchSourceBuilder = new SearchSourceBuilder();
                                        }
                                        for (Object sort : (List) mapQuery.get("sort")) {
                                            if (sort instanceof String) {
                                                searchSourceBuilder.sort((String) sort);
                                            } else if (sort instanceof Map) {
                                                for (Object field : ((Map) sort).keySet()) {
                                                    String sortField = (String) field;
                                                    String reverse = (String) ((Map) sort).get(field);
                                                    if ("asc".equals(reverse)) {
                                                        searchSourceBuilder.sort(sortField, SortOrder.ASC);
                                                    } else if ("desc".equals(reverse)) {
                                                        searchSourceBuilder.sort(sortField, SortOrder.DESC);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (mapQuery.get("fields") instanceof List) {
                                        if (searchSourceBuilder == null) {
                                            searchSourceBuilder = new SearchSourceBuilder();
                                        }
                                        for (Object field : (List) mapQuery.get("fields")) {
                                            if (field instanceof String) {
                                                searchSourceBuilder.field((String) field);
                                            }
                                        }
                                    }

                                    SearchRequest searchRequest = new SearchRequest();
View Full Code Here


    boolean facet = request.paramAsBoolean("facet", false);
    boolean qDsl = request.paramAsBoolean("q.dsl", false);
    boolean fqDsl = request.paramAsBoolean("fq.dsl", false);

    // build the query
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    if (q != null) {
      QueryBuilder queryBuilder;
      if (qDsl) {
        queryBuilder = QueryBuilders.wrapperQuery(q);
      } else {
        queryBuilder = QueryBuilders.queryString(q);
      }
      searchSourceBuilder.query(queryBuilder);
    }

    searchSourceBuilder.from(start);
    searchSourceBuilder.size(rows);

    // parse fl into individual fields
    // solr supports separating by comma or spaces
    if (fl != null) {
      if (!Strings.hasText(fl)) {
        searchSourceBuilder.noFields();
      } else {
        searchSourceBuilder.fields(fl.split("\\s|,"));
      }
    }

    // handle sorting
    if (sort != null) {
      String[] sorts = Strings.splitStringByCommaToArray(sort);
      for (int i = 0; i < sorts.length; i++) {
        String sortStr = sorts[i].trim();
        int delimiter = sortStr.lastIndexOf(" ");
        if (delimiter != -1) {
          String sortField = sortStr.substring(0, delimiter);
          if ("score".equals(sortField)) {
            sortField = "_score";
          }
          String reverse = sortStr.substring(delimiter + 1);
          if ("asc".equals(reverse)) {
            searchSourceBuilder.sort(sortField, SortOrder.ASC);
          } else if ("desc".equals(reverse)) {
            searchSourceBuilder.sort(sortField, SortOrder.DESC);
          }
        } else {
          searchSourceBuilder.sort(sortStr);
        }
      }
    } else {
      // default sort by descending score
      searchSourceBuilder.sort("_score", SortOrder.DESC);
    }

    // handler filters
    if (fqs != null && !fqs.isEmpty()) {
      FilterBuilder filterBuilder = null;

      // if there is more than one filter specified build
      // an and filter of query filters, otherwise just
      // build a single query filter.
      if (fqs.size() > 1) {
        AndFilterBuilder fqAnd = andFilter();
        for (String fq : fqs) {
          QueryBuilder queryBuilder = fqDsl ? QueryBuilders.wrapperQuery(fq) : QueryBuilders.queryString(fq);
          fqAnd.add(queryFilter(queryBuilder));
        }
        filterBuilder = fqAnd;
      } else {
        QueryBuilder queryBuilder = fqDsl ? QueryBuilders.wrapperQuery(fqs.get(0)) : QueryBuilders.queryString(fqs.get(0));
        filterBuilder = queryFilter(queryBuilder);
      }

      searchSourceBuilder.filter(filterBuilder);
    }

    // handle highlighting
    if (hl) {
      // get supported highlighting parameters if they exist
      String hlfl = request.param("hl.fl");
      int hlsnippets = request.paramAsInt("hl.snippets", 1);
      int hlfragsize = request.paramAsInt("hl.fragsize", 100);
      String hlsimplepre = request.param("hl.simple.pre");
      String hlsimplepost = request.param("hl.simple.post");

      HighlightBuilder highlightBuilder = new HighlightBuilder();
      if (hlfl == null) {
        // run against default _all field
        highlightBuilder.field("_all", hlfragsize, hlsnippets);
      } else {
        String[] hlfls = hlfl.split("\\s|,");
        for (String hlField : hlfls) {
          // skip wildcarded fields
          if (!hlField.contains("*")) {
            highlightBuilder.field(hlField, hlfragsize, hlsnippets);
          }
        }
      }

      // pre tags
      if (hlsimplepre != null) {
        highlightBuilder.preTags(hlsimplepre);
      }

      // post tags
      if (hlsimplepost != null) {
        highlightBuilder.postTags(hlsimplepost);
      }

      searchSourceBuilder.highlight(highlightBuilder);

    }

    // handle faceting
    if (facet) {
      // get supported facet parameters if they exist
      List<String> facetFields = params.get("facet.field");
      String facetSort = request.param("facet.sort");
      int facetLimit = request.paramAsInt("facet.limit", 100);

      List<String> facetQueries = params.get("facet.query");

      if (facetFields != null && !facetFields.isEmpty()) {
        for (String facetField : facetFields) {
          TermsFacetBuilder termsFacetBuilder = new TermsFacetBuilder(facetField);
          termsFacetBuilder.size(facetLimit);
          termsFacetBuilder.field(facetField);

          if (facetSort != null && facetSort.equals("index")) {
            termsFacetBuilder.order(TermsFacet.ComparatorType.TERM);
          } else {
            termsFacetBuilder.order(TermsFacet.ComparatorType.COUNT);
          }

          searchSourceBuilder.facet(termsFacetBuilder);
        }
      }

      if (facetQueries != null && !facetQueries.isEmpty()) {
        for (String facetQuery : facetQueries) {
          QueryFacetBuilder queryFacetBuilder = new QueryFacetBuilder(facetQuery);
          queryFacetBuilder.query(QueryBuilders.queryString(facetQuery));
          searchSourceBuilder.facet(queryFacetBuilder);
        }
      }
    }

    // get index and type we want to search against
View Full Code Here

    @AfterClass public void closeNodes() {
        closeAllNodes();
    }

    @Test public void testDfsQueryFetch() throws Exception {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(60).explain(true);

        List<DfsSearchResult> dfsResults = newArrayList();
        for (ShardIterator shardIt : clusterService.operationRouting().searchShards(clusterService.state(), new String[]{"test"}, new String[]{"test"}, null, null, null)) {
View Full Code Here

            assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - 60 - 1 - i)));
        }
    }

    @Test public void testDfsQueryFetchWithSort() throws Exception {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(60).explain(true).sort("age", SortOrder.ASC);

        List<DfsSearchResult> dfsResults = newArrayList();
        for (ShardIterator shardIt : clusterService.operationRouting().searchShards(clusterService.state(), new String[]{"test"}, new String[]{"test"}, null, null, null)) {
View Full Code Here

        assertThat(hits.totalHits(), equalTo(100l));
        assertThat(hits.hits().length, equalTo(0));
    }

    @Test public void testQueryFetchInOneGo() {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(20).explain(true);

        // do this with dfs, since we have uneven distribution of docs between shards
        List<DfsSearchResult> dfsResults = newArrayList();
View Full Code Here

//            assertEquals("id[" + hit.id() + "]", Integer.toString(100 - 60 - 1 - i), hit.id());
//        }
    }

    @Test public void testSimpleFacets() {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(20).explain(true).sort("age", SortOrder.ASC)
                .facet(queryFacet("all").query(termQuery("multi", "test")))
                .facet(queryFacet("test1").query(termQuery("name", "test1")));
View Full Code Here

    @AfterClass public void closeNodes() {
        closeAllNodes();
    }

    @Test public void testDfsQueryFetch() throws Exception {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(60).explain(true);

        List<DfsSearchResult> dfsResults = newArrayList();
        for (ShardIterator shardIt : clusterService.operationRouting().searchShards(clusterService.state(), new String[]{"test"}, new String[]{"test"}, null, null, null)) {
View Full Code Here

            assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - 60 - 1 - i)));
        }
    }

    @Test public void testDfsQueryFetchWithSort() throws Exception {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(60).explain(true).sort("age", SortOrder.ASC);

        List<DfsSearchResult> dfsResults = newArrayList();
        for (ShardIterator shardIt : clusterService.operationRouting().searchShards(clusterService.state(), new String[]{"test"}, new String[]{"test"}, null, null, null)) {
View Full Code Here

        assertThat(hits.totalHits(), equalTo(100l));
        assertThat(hits.hits().length, equalTo(0));
    }

    @Test public void testQueryFetchInOneGo() {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(20).explain(true);

        // do this with dfs, since we have uneven distribution of docs between shards
        List<DfsSearchResult> dfsResults = newArrayList();
View Full Code Here

//            assertEquals("id[" + hit.id() + "]", Integer.toString(100 - 60 - 1 - i), hit.id());
//        }
    }

    @Test public void testSimpleFacets() {
        SearchSourceBuilder sourceBuilder = searchSource()
                .query(termQuery("multi", "test"))
                .from(0).size(20).explain(true).sort("age", SortOrder.ASC)
                .facet(queryFacet("all").query(termQuery("multi", "test")))
                .facet(queryFacet("test1").query(termQuery("name", "test1")));
View Full Code Here

TOP

Related Classes of org.elasticsearch.search.builder.SearchSourceBuilder$ScriptField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.