大数据学习[21]---java操作ES一例

一个常用的java操作ES的例子:

//GET info/_search
//{
//  "query": {
//    "nested": {
//      "path": "keywords02",
//      "score_mode": "max",
//      "query": {
//        "function_score": {
//          "query": {
//            "bool": {
//              "must": [
//                {
//                  "match": {
//                    "keywords02.keyword": {
//                      "query": "高血压",
//                      "analyzer": "xxxx_xxx_analyzer"
//                    }
//                  }
//                }
//              ],
//              "must_not": {
//                "terms": {
//                  "_id": [
//                    "63356",
//                    "5408",
//                    "24618"
//                  ]
//                }
//              }
//            }
//          },
//          "functions": [
//            {
//              "script_score": {
//                "script": "_score * doc['keywords02.weight'].value"
//              }
//            }
//          ]
//        }
//      }
//    }
//  }
//  ,
//  "collapse": {
//    "field": "keywords01.raw"
//  }
//}

package sparkml.es;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

@SuppressWarnings("unchecked")
public class demoEs {

    private static TransportClient client = null;
    static {
        // 设置集群名称
        Settings settings = Settings.builder().put("cluster.name", "***")
                .put("xpack.security.user", "elastic:changeme")// xpack用户名密码
                .build();
        // 创建client
        try {
            client = new PreBuiltXPackTransportClient(settings)
                    .addTransportAddress(
                            new InetSocketTransportAddress(InetAddress
                                    .getByName("***.***.***.***"), 9300))
                    .addTransportAddress(
                            new InetSocketTransportAddress(InetAddress
                                    .getByName("***.***.***.***"), 9300));

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static void getArticleInfoByTags(String tags, List filterList)
            throws Exception {
        //
        SearchRequestBuilder responsebuilder = client.prepareSearch("info")
                .setTypes("articles")
                .setSearchType(SearchType.QUERY_THEN_FETCH);

        // 1. pics过滤
        NestedQueryBuilder picsFilter = QueryBuilders.nestedQuery(
                "pics",
                QueryBuilders.boolQuery()
                        .must(QueryBuilders.matchQuery("pics.is_down", "1"))
                        .must(QueryBuilders.matchQuery("pics.is_qr", "0")),
                ScoreMode.Avg);

        // 2. 关键词权重计算过滤,过滤ID
        // ---------keyword查询
        NestedQueryBuilder kwIDsQuery = QueryBuilders.nestedQuery(
                "keywords02",
                QueryBuilders
                        .boolQuery()
                        .must(QueryBuilders.matchQuery("keywords02.keyword",
                                tags).analyzer("xxxx_xxx_analyzer"))
                        .mustNot(QueryBuilders.termsQuery("_id", filterList)),
                ScoreMode.Avg);

        // ---------权重分数函数
        FilterFunctionBuilder[] keyWeithFunctionBuilders = { new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                // 这里把_score * doc['keywords02.weight'].value修改成了随机化            
                ScoreFunctionBuilders.randomFunction(Math.round(Math.random() * 100))
        ) };

        // ---kw函数权重查询
        FunctionScoreQueryBuilder query = QueryBuilders.functionScoreQuery(
                kwIDsQuery, keyWeithFunctionBuilders);

        // 综合查询
        SearchResponse myresponse = responsebuilder.setQuery(query)
                .setPostFilter(picsFilter)
                .setCollapse(new CollapseBuilder("keywords01.raw")).setFrom(0)
                .setSize(5).get();
        System.out.println(myresponse.toString());
        SearchHits hits = myresponse.getHits();
        System.out.println(hits.totalHits);
        for (int i = 0; i < hits.getHits().length; i++) {
            String sourceAsString = hits.getHits()[i].getSourceAsString();
            System.out.println(sourceAsString);
        }
    }

    public static void main(String[] args) throws Exception {
        String tags = "高血压";
        String[] fls = { "63356", "5408", "24618" };
        List filterList = Arrays.asList(fls);

        demoEs.getArticleInfoByTags(tags, filterList);
    }
}

注意:ES中的查询与过滤,过滤完之后,对于score会有损失的。

你可能感兴趣的:(elasticsearch)