How i can search with multi fields use java api?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How i can search with multi fields use java api?

曹江华
i'm hope search with multi fields use java api.how i can do it?
    public static void search() {
        Client client = SearchClient.getSearchClient();
        try {
            //String keyword = "超级文件管理器";
            String keyword = "炫丽的白色极光";
            keyword = IKAnalyzer.parse(keyword);
            System.out.println(keyword);
            XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("name",keyword);
            //XContentQueryBuilder xContentQueryBulder =
QueryBuilders.termQuery("name",keyword);
            //XContentFilterBuilder.
            XContentQueryBuilder xContentQueryBulder2 =
QueryBuilders.fieldQuery("resourcetype","3");
            //JsonQueryBuilder jsonQueryBuilder =
JsonQueryBuilders.fieldQuery();
            //JsonQueryBuilder jsonQueryBuilder =
JsonQueryBuilders.termQuery("softname", keyword);
            //AtomicReference<SearchResponse> response = new
AtomicReference<SearchResponse>(client.search(searchRequest("resources").searchType(SearchType.DFS_QUERY_AND_FETCH).source(SearchSourceBuilder.searchSource().query(jsonQueryBuilder))).actionGet());
            //SearchResponse response =
client.search(searchRequest("resources").searchType(SearchType.DFS_QUERY_AND_FETCH).source(SearchSourceBuilder.searchSource().query(jsonQueryBuilder).from(0).size(5))).actionGet();
            SearchResponse response =
client.search(searchRequest("resources")
                    .searchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .source(searchSource()
                        .query(xContentQueryBulder)
                        //.queryParserName("{\"field\" : { \"name\" :
\"文件管理\" }}")
                        //.query(xContentQueryBulder2)
                        //.sort("flatid")
                        .from(0).size(100).explain(true)
                    )
            ).actionGet();
            //SearchHit[] hits = response.get().hits().hits();
            SearchHits theHits = response.hits();
            SearchHit[] hits = theHits.hits();
            for(SearchHit hit : hits) {
                String id = hit.id();
                System.out.println("id:" + id + ":" + "source:" +
hit.sourceAsString());
            }
            System.out.println("====================");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


in this code.
XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("name",keyword);
i'm hope search annother field together,example:
XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("restype","3");


Reply | Threaded
Open this post in threaded view
|

Re: How i can search with multi fields use java api?

kimchy
Administrator
When you want to search on multiple fields, you can wrap them in a boolean query (there is a builder for that). Note, the query builders are meant for chaining for simpler code usage.

Also note that if you just want to search for some terms / words that can exists in different fields, the _all field allows you to do that simply.

cheers,
shay.banon

2010/5/17 曹江华 <tujiao.com@gmail.com>
i'm hope search with multi fields use java api.how i can do it?
   public static void search() {
       Client client = SearchClient.getSearchClient();
       try {
           //String keyword = "超级文件管理器";
           String keyword = "炫丽的白色极光";
           keyword = IKAnalyzer.parse(keyword);
           System.out.println(keyword);
           XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("name",keyword);
           //XContentQueryBuilder xContentQueryBulder =
QueryBuilders.termQuery("name",keyword);
           //XContentFilterBuilder.
           XContentQueryBuilder xContentQueryBulder2 =
QueryBuilders.fieldQuery("resourcetype","3");
           //JsonQueryBuilder jsonQueryBuilder =
JsonQueryBuilders.fieldQuery();
           //JsonQueryBuilder jsonQueryBuilder =
JsonQueryBuilders.termQuery("softname", keyword);
           //AtomicReference<SearchResponse> response = new
AtomicReference<SearchResponse>(client.search(searchRequest("resources").searchType(SearchType.DFS_QUERY_AND_FETCH).source(SearchSourceBuilder.searchSource().query(jsonQueryBuilder))).actionGet());
           //SearchResponse response =
client.search(searchRequest("resources").searchType(SearchType.DFS_QUERY_AND_FETCH).source(SearchSourceBuilder.searchSource().query(jsonQueryBuilder).from(0).size(5))).actionGet();
           SearchResponse response =
client.search(searchRequest("resources")
                   .searchType(SearchType.DFS_QUERY_THEN_FETCH)
                   .source(searchSource()
                       .query(xContentQueryBulder)
                       //.queryParserName("{\"field\" : { \"name\" :
\"文件管理\" }}")
                       //.query(xContentQueryBulder2)
                       //.sort("flatid")
                       .from(0).size(100).explain(true)
                   )
           ).actionGet();
           //SearchHit[] hits = response.get().hits().hits();
           SearchHits theHits = response.hits();
           SearchHit[] hits = theHits.hits();
           for(SearchHit hit : hits) {
               String id = hit.id();
               System.out.println("id:" + id + ":" + "source:" +
hit.sourceAsString());
           }
           System.out.println("====================");
       } catch (Exception e) {
           e.printStackTrace();
       }
   }


in this code.
XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("name",keyword);
i'm hope search annother field together,example:
XContentQueryBuilder xContentQueryBulder =
QueryBuilders.fieldQuery("restype","3");