Search in sources :

Example 21 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method testThatSortingOnCompletionFieldReturnsUsefulException.

public void testThatSortingOnCompletionFieldReturnsUsefulException() throws Exception {
    createIndexAndMapping(completionMappingBuilder);
    client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder().startObject().startObject(FIELD).startArray("input").value("Nirvana").endArray().endObject().endObject()).get();
    refresh();
    try {
        client().prepareSearch(INDEX).setTypes(TYPE).addSort(new FieldSortBuilder(FIELD)).execute().actionGet();
        fail("Expected an exception due to trying to sort on completion field, but did not happen");
    } catch (SearchPhaseExecutionException e) {
        assertThat(e.status().getStatus(), is(400));
        assertThat(e.toString(), containsString("Fielddata is not supported on field [" + FIELD + "] of type [completion]"));
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Example 22 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class RemoteRequestBuilders method initialSearchParams.

static Map<String, String> initialSearchParams(SearchRequest searchRequest, Version remoteVersion) {
    Map<String, String> params = new HashMap<>();
    if (searchRequest.scroll() != null) {
        params.put("scroll", searchRequest.scroll().keepAlive().toString());
    }
    params.put("size", Integer.toString(searchRequest.source().size()));
    if (searchRequest.source().version() == null || searchRequest.source().version() == true) {
        // false is the only value that makes it false. Null defaults to true....
        params.put("version", null);
    }
    if (searchRequest.source().sorts() != null) {
        boolean useScan = false;
        // Detect if we should use search_type=scan rather than a sort
        if (remoteVersion.before(Version.V_2_1_0)) {
            for (SortBuilder<?> sort : searchRequest.source().sorts()) {
                if (sort instanceof FieldSortBuilder) {
                    FieldSortBuilder f = (FieldSortBuilder) sort;
                    if (f.getFieldName().equals(FieldSortBuilder.DOC_FIELD_NAME)) {
                        useScan = true;
                        break;
                    }
                }
            }
        }
        if (useScan) {
            params.put("search_type", "scan");
        } else {
            StringBuilder sorts = new StringBuilder(sortToUri(searchRequest.source().sorts().get(0)));
            for (int i = 1; i < searchRequest.source().sorts().size(); i++) {
                sorts.append(',').append(sortToUri(searchRequest.source().sorts().get(i)));
            }
            params.put("sort", sorts.toString());
        }
    }
    if (remoteVersion.before(Version.V_2_0_0)) {
        // Versions before 2.0.0 need prompting to return interesting fields. Note that timestamp isn't available at all....
        searchRequest.source().storedField("_parent").storedField("_routing").storedField("_ttl");
    }
    if (searchRequest.source().storedFields() != null && false == searchRequest.source().storedFields().fieldNames().isEmpty()) {
        StringBuilder fields = new StringBuilder(searchRequest.source().storedFields().fieldNames().get(0));
        for (int i = 1; i < searchRequest.source().storedFields().fieldNames().size(); i++) {
            fields.append(',').append(searchRequest.source().storedFields().fieldNames().get(i));
        }
        String storedFieldsParamName = remoteVersion.before(Version.V_5_0_0_alpha4) ? "fields" : "stored_fields";
        params.put(storedFieldsParamName, fields.toString());
    }
    return params;
}
Also used : HashMap(java.util.HashMap) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Example 23 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testMatchesQueriesNestedInnerHits.

public void testMatchesQueriesNestedInnerHits() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", builder));
    ensureGreen();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = randomIntBetween(2, 35);
    requests.add(client().prepareIndex("test", "type1", "0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
    for (int i = 2; i < numDocs; i++) {
        requests.add(client().prepareIndex("test", "type1", String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    }
    indexRandom(true, requests);
    waitForRelocation(ClusterHealthStatus.GREEN);
    QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
    query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)), false);
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertAllSuccessful(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo((long) numDocs));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
    for (int i = 2; i < numDocs; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 24 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method query.

@Override
public List<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    SearchRequestBuilder srb = client.prepareSearch(indexName);
    srb.setTypes(query.getStore());
    srb.setQuery(QueryBuilders.matchAllQuery());
    srb.setPostFilter(getFilter(query.getCondition(), informations.get(query.getStore())));
    if (!query.getOrder().isEmpty()) {
        List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (int i = 0; i < orders.size(); i++) {
            IndexQuery.OrderEntry orderEntry = orders.get(i);
            FieldSortBuilder fsb = new FieldSortBuilder(orders.get(i).getKey()).order(orderEntry.getOrder() == Order.ASC ? SortOrder.ASC : SortOrder.DESC);
            if (useDeprecatedIgnoreUnmapped) {
                fsb.ignoreUnmapped(true);
            } else {
                Class<?> datatype = orderEntry.getDatatype();
                fsb.unmappedType(convertToEsDataType(datatype));
            }
            srb.addSort(fsb);
        }
    }
    srb.setFrom(0);
    if (query.hasLimit())
        srb.setSize(query.getLimit());
    else
        srb.setSize(maxResultsSize);
    srb.setNoFields();
    //srb.setExplain(true);
    SearchResponse response = srb.execute().actionGet();
    log.debug("Executed query [{}] in {} ms", query.getCondition(), response.getTookInMillis());
    SearchHits hits = response.getHits();
    if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
        log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
    List<String> result = new ArrayList<String>(hits.hits().length);
    for (SearchHit hit : hits) {
        result.add(hit.id());
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchHits(org.elasticsearch.search.SearchHits)

Example 25 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project fess by codelibs.

the class QueryHelper method createFieldSortBuilder.

protected FieldSortBuilder createFieldSortBuilder(final String field, final SortOrder order) {
    final String fieldName = SCORE_FIELD.equals(field) ? ES_SCORE_FIELD : field;
    final FieldSortBuilder builder = SortBuilders.fieldSort(fieldName);
    if (ES_SCORE_FIELD.equals(fieldName)) {
        builder.unmappedType("float");
    }
    return builder.order(order);
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Aggregations

FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)32 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 ArrayList (java.util.ArrayList)10 SearchHits (org.elasticsearch.search.SearchHits)7 SearchHit (org.elasticsearch.search.SearchHit)6 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)6 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 SortBuilder (org.elasticsearch.search.sort.SortBuilder)5 IOException (java.io.IOException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 ScoreSortBuilder (org.elasticsearch.search.sort.ScoreSortBuilder)4 HashMap (java.util.HashMap)3 SortOrder (org.elasticsearch.search.sort.SortOrder)3 ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)2 FieldType (org.apache.metron.indexing.dao.search.FieldType)2 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)2 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)2 SortField (org.apache.metron.indexing.dao.search.SortField)2 SortTerm (org.bedework.calfacade.filter.SortTerm)2