コード例 #1
0
        private static BoolQueryDescriptor <DocumentDM> ExactQuery(DocumentDM doc)
        {
            var boolQuery = new BoolQueryDescriptor <DocumentDM>();

            //ex
            boolQuery.Must(mu => mu
                           .Match(m => !doc.Subject[0].Equals('?') ? m
                                  .Field(f => f.Subject)
                                  .Query(doc.Subject) : m
                                  ), mud => mud
                           .Match(m => !doc.Predicate[0].Equals('?') ? m
                                  .Field(f => f.Predicate)
                                  .Query(doc.Predicate) : m
                                  ),
                           mud => mud
                           .Match(m => !doc.Obj[0].Equals('?') ? m
                                  .Field(f => f.Obj)
                                  .Query(doc.Obj) : m
                                  )

                           );
            return(boolQuery);
        }
コード例 #2
0
        private DataTable ElasticSearch(DocumentDM doc)
        {
            DataTable table = new DataTable();

            ConnectionSettings   connectionSettings;
            ElasticClient        elasticClient;
            StaticConnectionPool connectionPool;

            var nodes = new Uri[]
            {
                new Uri("http://localhost:9200/"),
            };

            connectionPool     = new StaticConnectionPool(nodes);
            connectionSettings = new ConnectionSettings(connectionPool);
            elasticClient      = new ElasticClient(connectionSettings);
            //var searchResult = elasticClient.Search<DocumentDM>(s => s
            //                    .Size(500)
            //                    .Index("*")
            //                    .Query(q => q
            //                        .MultiMatch(m => m
            //                            //get fields that are not variables
            //                            .Fields(doc.GetTripleFields().Split(' ').ToList<string>().Select(x => new Field(x)).ToArray())
            //                            //.Fields(f=> f
            //                            //        .Field("subject")
            //                            //        .Field("predicate")
            //                            //        .Field("object"))
            //                            //get text that is not variables
            //                            .Query(doc.GetTripleText())
            //                        )
            //                    )
            //                );

            BoolQueryDescriptor <DocumentDM> boolQuery = ExactQuery(doc);

            var searchResult = elasticClient.Search <DocumentDM>(s => s
                                                                 .Size(500)
                                                                 .Index("*")
                                                                 .Query(q => q.Bool(b => boolQuery))
                                                                 );

            table.Columns.Add("subject", typeof(String));
            table.Columns.Add("predicate", typeof(String));
            table.Columns.Add("object", typeof(String));
            table.Columns.Add("score", typeof(String));
            table.Columns.Add("index", typeof(String));
            table.Columns.Add("invisible", typeof(String));
            for (int i = 0; i < searchResult.Documents.Count; i++)
            {
                var           subject   = searchResult.Documents.ElementAt(i).Subject;
                var           predicate = searchResult.Documents.ElementAt(i).Predicate;
                var           obj       = searchResult.Documents.ElementAt(i).Obj;
                var           score     = searchResult.HitsMetadata.Hits.ElementAt(i).Score;
                var           index     = searchResult.HitsMetadata.Hits.ElementAt(i).Index;
                StringBuilder sb        = new StringBuilder();
                sb.Append(subject);
                sb.Append(" ");
                sb.Append(predicate);
                sb.Append(" ");
                sb.Append(obj);
                sb.Append(" ");
                sb.Append(score);
                sb.Append(" ");
                sb.Append(index);
                var row = new object[] { subject, predicate, obj, score, index, sb.ToString() };
                table.Rows.Add(row);
            }

            return(table);
            //if (!doc.Subject[0].Equals('?'))
            //{
            //    var subjectResponse = elasticClient.Search<DocumentDM>(sd => sd
            //                        .Index("*")
            //                        .Size(1000)
            //                        .Query(q => q
            //                            .Match(m => m.Field("subject").Query(doc.Subject)
            //                            )));
            //}

            //if (!doc.Predicate[0].Equals('?'))
            //{
            //    var predicateResponse = elasticClient.Search<DocumentDM>(sd => sd
            //                    .Index("*")
            //                    .Size(1000)
            //                    .Query(q => q
            //                        .Match(m => m.Field("predicate").Query(doc.Predicate)
            //                        )));
            //}

            //if (!doc.Obj[0].Equals('?'))
            //{
            //    var objectResponse = elasticClient.Search<DocumentDM>(sd => sd
            //                    .Index("*")
            //                    .Size(1000)
            //                    .Query(q => q
            //                        .Match(m => m.Field("object").Query(doc.Obj)
            //                        )));
            //}
        }