Finding multiple rows in all fields in Elasticsearch using the Java API

I have an elasticsearch index that has users with fields like .. "name": "kai" "age": "23" "location": "Delhi, India" "tag": ["search", "nosql" ] and etc.

I want to request multiple rows in all user fields (for example, ["nosql", "delhi"]). Is it possible to use the Java API?

Here is an example of the code I'm using. (But this is not relevant to the question) Its just to know the objects that I am using right now.

BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.matchAllQuery()); if(location!=null) { queryBuilder.must(QueryBuilders.matchQuery("location",location)); } BoolFilterBuilder filerBuilder=FilterBuilders.boolFilter(); for(String skill:skills){ filerBuilder.must(FilterBuilders.rangeFilter("tags."+skill).from(0)); } filerBuilder.must(FilterBuilders.queryFilter(queryBuilder)); if(age!=null) { filerBuilder.must(FilterBuilders.rangeFilter("age").from(age[0]).to(age[1])); } SearchResponse response = client.prepareSearch("skillbin") .setTypes("stackdump") .setSearchType(SearchType.QUERY_AND_FETCH) .setQuery(queryBuilder) .setPostFilter(filerBuilder) .addSort("reputation", SortOrder.DESC) .execute() .actionGet(); SearchHits hits=response.getHits(); 

Thanks in advance.:)

+5
source share
2 answers

In the Java API, you can request the "_all" field, which by default includes all fields in the document. An alternative way is to use MultiMatchQuery , which is more flexible and allows you to specify a list of fields to which you want to query.

Here is sample code for accessing the _all field, as well as for creating MultiMatchQuery using MultiMatchQueryBuilder and QueryBuilders . multiMatchQuery by query to search in all fields :

 String queryString = "nosql delhi"; String allField = "_all"; MultiMatchQueryBuilder mmqb = QueryBuilders.multiMatchQuery(queryString, allField); 
+7
source

If you use a standard analyzer, elasticsearch will tokenize every word by default using a space tokenizer.

You can use the query string query to search in this case, where you can add multiple fields for the search. Also the query string query by default. The operator is "OR". Therefore, if your search query is "nosql delhi" (as in your case), it is translated into "nosql OR delhi" and a search is performed in all the fields specified in the query. Thus, you can search for multiple terms in multiple fields.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1208108/


All Articles