Search in sources :

Example 6 with SearchQueryInputDataset

use of org.opensearch.ml.common.dataset.SearchQueryInputDataset in project ml-commons by opensearch-project.

the class MLInputDatasetHandler method parseSearchQueryInput.

/**
 * Create DataFrame based on given search query
 * @param mlInputDataset MLInputDataset
 * @param listener ActionListener
 */
public void parseSearchQueryInput(MLInputDataset mlInputDataset, ActionListener<DataFrame> listener) {
    if (!mlInputDataset.getInputDataType().equals(MLInputDataType.SEARCH_QUERY)) {
        throw new IllegalArgumentException("Input dataset is not SEARCH_QUERY type.");
    }
    SearchQueryInputDataset inputDataset = (SearchQueryInputDataset) mlInputDataset;
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(inputDataset.getSearchSourceBuilder());
    List<String> indicesList = inputDataset.getIndices();
    String[] indices = new String[indicesList.size()];
    indices = indicesList.toArray(indices);
    searchRequest.indices(indices);
    client.search(searchRequest, ActionListener.wrap(r -> {
        if (r == null || r.getHits() == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
            listener.onFailure(new IllegalArgumentException("No document found"));
            return;
        }
        SearchHits hits = r.getHits();
        List<Map<String, Object>> input = new ArrayList<>();
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            input.add(hit.getSourceAsMap());
        }
        DataFrame dataFrame = DataFrameBuilder.load(input);
        listener.onResponse(dataFrame);
        return;
    }, e -> {
        log.error("Failed to search" + e);
        listener.onFailure(e);
    }));
    return;
}
Also used : FieldDefaults(lombok.experimental.FieldDefaults) Client(org.opensearch.client.Client) SearchHit(org.opensearch.search.SearchHit) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) SearchHits(org.opensearch.search.SearchHits) ArrayList(java.util.ArrayList) List(java.util.List) AccessLevel(lombok.AccessLevel) Map(java.util.Map) Log4j2(lombok.extern.log4j.Log4j2) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) DataFrameBuilder(org.opensearch.ml.common.dataframe.DataFrameBuilder) MLInputDataset(org.opensearch.ml.common.dataset.MLInputDataset) SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) DataFrameInputDataset(org.opensearch.ml.common.dataset.DataFrameInputDataset) MLInputDataType(org.opensearch.ml.common.dataset.MLInputDataType) SearchRequest(org.opensearch.action.search.SearchRequest) SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) SearchHit(org.opensearch.search.SearchHit) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) ArrayList(java.util.ArrayList) List(java.util.List) SearchHits(org.opensearch.search.SearchHits)

Example 7 with SearchQueryInputDataset

use of org.opensearch.ml.common.dataset.SearchQueryInputDataset in project ml-commons by opensearch-project.

the class RestActionUtils method buildSearchQueryInput.

/**
 * Create SearchQueryInputDataset from a RestRequest
 *
 * @param request RestRequest
 * @param client node client
 * @return SearchQueryInputDataset with indices and search source
 * @throws IOException throw IOException when fail to parse search request
 */
public static SearchQueryInputDataset buildSearchQueryInput(RestRequest request, NodeClient client) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    IntConsumer setSize = size -> searchRequest.source().size(size);
    request.withContentOrSourceParamParserOrNull(parser -> parseSearchRequest(searchRequest, request, parser, client.getNamedWriteableRegistry(), setSize));
    return new SearchQueryInputDataset(Arrays.asList(searchRequest.indices()), searchRequest.source());
}
Also used : Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) Locale(java.util.Locale) RestRequest(org.opensearch.rest.RestRequest) IntConsumer(java.util.function.IntConsumer) SearchRequest(org.opensearch.action.search.SearchRequest) IOException(java.io.IOException) RestSearchAction.parseSearchRequest(org.opensearch.rest.action.search.RestSearchAction.parseSearchRequest) SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) Strings(org.opensearch.common.Strings) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) SearchRequest(org.opensearch.action.search.SearchRequest) RestSearchAction.parseSearchRequest(org.opensearch.rest.action.search.RestSearchAction.parseSearchRequest) SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) IntConsumer(java.util.function.IntConsumer)

Example 8 with SearchQueryInputDataset

use of org.opensearch.ml.common.dataset.SearchQueryInputDataset in project ml-commons by opensearch-project.

the class MLInputDatasetHandlerTests method testSearchQueryInputDatasetWithoutHits.

@SuppressWarnings("unchecked")
public void testSearchQueryInputDatasetWithoutHits() {
    searchResponse = mock(SearchResponse.class);
    SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1f);
    when(searchResponse.getHits()).thenReturn(hits);
    doAnswer(invocation -> {
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocation.getArguments()[1];
        listener.onResponse(searchResponse);
        return null;
    }).when(client).search(any(), any());
    SearchQueryInputDataset searchQueryInputDataset = SearchQueryInputDataset.builder().indices(Collections.singletonList("index1")).searchSourceBuilder(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())).build();
    mlInputDatasetHandler.parseSearchQueryInput(searchQueryInputDataset, listener);
    verify(listener, times(1)).onFailure(any());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) ActionListener(org.opensearch.action.ActionListener) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 9 with SearchQueryInputDataset

use of org.opensearch.ml.common.dataset.SearchQueryInputDataset in project ml-commons by opensearch-project.

the class MLInputDatasetHandlerTests method testDataFrameInputDatasetWrongType.

public void testDataFrameInputDatasetWrongType() {
    expectedEx.expect(IllegalArgumentException.class);
    expectedEx.expectMessage("Input dataset is not DATA_FRAME type.");
    SearchQueryInputDataset searchQueryInputDataset = SearchQueryInputDataset.builder().indices(Collections.singletonList("index1")).searchSourceBuilder(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())).build();
    mlInputDatasetHandler.parseDataFrameInput(searchQueryInputDataset);
}
Also used : SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 10 with SearchQueryInputDataset

use of org.opensearch.ml.common.dataset.SearchQueryInputDataset in project ml-commons by opensearch-project.

the class PredictionITTests method initTestingData.

@Before
public void initTestingData() throws ExecutionException, InterruptedException {
    generateMLTestingData();
    SearchSourceBuilder searchSourceBuilder = generateSearchSourceBuilder();
    MLInputDataset inputDataset = new SearchQueryInputDataset(Collections.singletonList(TESTING_INDEX_NAME), searchSourceBuilder);
    taskId = trainModel(inputDataset);
    waitModelAvailable(taskId);
}
Also used : SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) MLInputDataset(org.opensearch.ml.common.dataset.MLInputDataset) IntegTestUtils.generateSearchSourceBuilder(org.opensearch.ml.utils.IntegTestUtils.generateSearchSourceBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) Before(org.junit.Before)

Aggregations

SearchQueryInputDataset (org.opensearch.ml.common.dataset.SearchQueryInputDataset)11 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)8 MLInputDataset (org.opensearch.ml.common.dataset.MLInputDataset)5 IntegTestUtils.generateSearchSourceBuilder (org.opensearch.ml.utils.IntegTestUtils.generateSearchSourceBuilder)4 ActionListener (org.opensearch.action.ActionListener)3 SearchHits (org.opensearch.search.SearchHits)3 TotalHits (org.apache.lucene.search.TotalHits)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 DataFrame (org.opensearch.ml.common.dataframe.DataFrame)2 MLInput (org.opensearch.ml.common.parameter.MLInput)2 SearchHit (org.opensearch.search.SearchHit)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 IntConsumer (java.util.function.IntConsumer)1 AccessLevel (lombok.AccessLevel)1