コード例 #1
0
        /// <summary>
        /// 精确的terms查询,查询Keyword_type_col这一列精确匹配"TableStore"或者"SearchIndex"的数据
        /// TermsQuery可以使用多个Term同时查询
        /// </summary>
        /// <param name="otsClient"></param>
        public static void TermsQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start terms query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var query = new TermsQuery();

            query.FieldName = Keyword_type_col;
            query.Terms     = new List <ColumnValue>()
            {
                new ColumnValue("TableStore"), new ColumnValue("SearchIndex")
            };

            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                ReturnAll = true
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
コード例 #2
0
        /// <summary>
        /// 精确匹配词条列表
        /// </summary>
        /// <typeparam name="TProperty">属性类型</typeparam>
        /// <param name="expression">字段表达式</param>
        /// <param name="values">值</param>
        public EsQuery <TResult> Terms <TProperty>(Expression <Func <TResult, TProperty> > expression, IEnumerable <object> values)
        {
            var condition = new TermsQuery
            {
                Field = new Field(expression),
                Terms = values
            };

            return(And(condition));
        }
コード例 #3
0
        public static QueryContainer MakeTermsQuery(string[] queries, string field, double boost = 1)
        {
            QueryContainer termsQuery = new TermsQuery
            {
                Field = field,
                Terms = queries,
                Boost = boost
            };

            return(termsQuery);
        }
コード例 #4
0
 public static TermsQuery BuildTermsQuery(Aliyun.TableStore.DataModel.Search.Query.TermsQuery query)
 {
     TermsQuery.Builder builder = TermsQuery.CreateBuilder();
     builder.SetFieldName(query.FieldName);
     if (query.Terms == null)
     {
         throw new ArgumentException("terms is null");
     }
     foreach (var item in query.Terms)
     {
         builder.AddTerms(ByteString.CopyFrom(SearchVariantType.toVariant(item)));
     }
     return(builder.Build());
 }
コード例 #5
0
        public void Terms_query_must_transform_correclty_to_ES()
        {
            var query = new TermsQuery("type", new string[] { "text", "photo" });

            Assert.IsTrue(query.Field == "type");
            Assert.IsTrue(query.Values[0].ToString() == "text");
            Assert.IsTrue(query.Values[1].ToString() == "photo");

            string json = query.ToString();
            var    jo   = JsonObject.Parse(json);
            var    q    = jo.ToQuery();

            Assert.IsTrue(q.Type == QueryType.TermsQuery);
            query = q as TermsQuery;
            Assert.IsTrue(query.Field == "type");
            Assert.IsTrue(query.Values[0].ToString() == "text");
            Assert.IsTrue(query.Values[1].ToString() == "photo");
        }
コード例 #6
0
        public void Log_With_Custom_Routing()
        {
            string oldRoutingSource = null;

            QueryConfiguration(appender =>
            {
                if (!appender.IndexOperationParams.TryGetValue("_routing", out oldRoutingSource))
                {
                    oldRoutingSource = null;
                }
                appender.IndexOperationParams.AddParameter(new IndexOperationParam("_routing", "%{RoutingSource}"));
            });
            ThreadContext.Properties["RoutingSource"] = "ROUTING";
            _log.Info("loggingtest");

            Client.Refresh(TestIndex);
            var query = new TermsQuery
            {
                Field = "_routing",
                Terms = new[] { "ROUTING" }
            };
            var searchResults = Client.Search <JObject>(s => s.AllTypes().Query(descriptor => query));

            QueryConfiguration(appender =>
            {
                if (oldRoutingSource == null)
                {
                    appender.IndexOperationParams.Remove("_routing");
                }
                else
                {
                    appender.IndexOperationParams.AddParameter(new IndexOperationParam("_routing", oldRoutingSource));
                }
            });

            Assert.AreEqual(1, searchResults.Total);
        }
コード例 #7
0
        /// <summary>根据iiid数组搜索</summary>
        public virtual async Task <MetadataCollection> GetMetadatasAsync(SearchMetadatasRequest request)
        {
            var provider  = new SearchProvider <Metadata>();
            var termQuery = new TermsQuery {
                Field = "iiid.keyword", Terms = request.IIIds
            };
            var query = new BoolQuery()
            {
                Must = new List <QueryContainer> {
                    termQuery
                }
            };

            var fields = provider.BuildFields(request.Fields);

            var from = 0;
            var size = request.IIIds.Count;

            var searchresponse = await _esAccess.PagingQueryAsync(query, null, fields, null, from, size);

            var response = new MetadataCollection(searchresponse.Documents);

            return(await Task.FromResult(response));
        }
コード例 #8
0
        private void get_glassinterface_data_from_elastic()
        {
            try
            {
                //var node = new Uri("http://192.168.7.79:9200");//台中
                var node = new Uri("http://192.168.39.111:9200");//新竹

                var settings = new ConnectionSettings(node).DefaultIndex("default");

                var           client = new ElasticClient(settings);
                int           size   = 10000;
                SearchRequest sr     = new SearchRequest("recodevehiclecstinterface*");

                DateTime       start_datetime = dtp_start.Value.AddHours(-10);
                DateTime       end_datetime   = start_datetime.AddHours(10);
                DateRangeQuery dq             = new DateRangeQuery
                {
                    Field       = "@timestamp",
                    GreaterThan = start_datetime,
                    LessThan    = end_datetime,
                };

                TermsQuery tsq = new TermsQuery
                {
                    Field = new Field("VH_ID.keyword"),
                    Terms = new List <string> {
                        cmb_VH.Text
                    }
                };

                sr.From = 0;
                sr.Size = size;

                sr.Query = dq && tsq;
                //sr.Query = dq;
                sr.Source = new SourceFilter()
                {
                    Includes = new string[] { "@timestamp", "VALID", "AM_AVBL", "BUSY", "COMPT", "CONT", "CS_0", "CS_1", "HO_AVBL", "L_REQ", "READY", "TR_REQ", "U_REQ", "VA", "VS_0", "VS_1", "ES", "VH_ID", "PORT_ID" },
                    //Excludes = new string[] { "CMD_FINISH_TIME" }
                };
                var result = client.Search <recodevehiclecstinterface>(sr);

                List <recodevehiclecstinterface> resultList = result.Documents.ToList <recodevehiclecstinterface>();
                signalList.Clear();
                foreach (recodevehiclecstinterface record in resultList)
                {
                    Signal signal = new Signal();
                    if (!vehicleList.Contains(record.VH_ID))
                    {
                        vehicleList.Add(record.VH_ID);
                    }
                    signal.VH_ID       = record.VH_ID;
                    signal.PORT_ID     = record.PORT_ID;
                    signal.record_time = record.timestamp;
                    signal.L_REQ       = record.L_REQ;
                    signal.U_REQ       = record.U_REQ;
                    signal.READY       = record.READY;
                    signal.VA          = record.VA;
                    signal.VS_0        = record.VS_0;
                    signal.VS_1        = record.VS_1;
                    signal.VALID       = record.VALID;
                    signal.TR_REQ      = record.TR_REQ;
                    signal.BUSY        = record.BUSY;
                    signal.COMPT       = record.COMPT;
                    signal.AM_AVBL     = record.AM_AVBL;
                    signal.HO_AVBL     = record.HO_AVBL;
                    signal.ES          = record.ES;
                    signalList.Add(signal);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Exception");
            }
        }
コード例 #9
0
        public override QueryContainer GetQuery(QueryContainer query)
        {
            switch (ExpressOperator)
            {
            case ExpressOperator.Eq:
                query = new TermQuery
                {
                    Field = PropertyName,
                    Value = Value
                };
                break;

            case ExpressOperator.Gt:
                query = new TermRangeQuery
                {
                    Field       = PropertyName,
                    GreaterThan = Value.ToString()
                };
                break;

            case ExpressOperator.Ge:
                query = new TermRangeQuery
                {
                    Field = PropertyName,
                    GreaterThanOrEqualTo = Value.ToString()
                };
                break;

            case ExpressOperator.Lt:
                query = new TermRangeQuery
                {
                    Field    = PropertyName,
                    LessThan = Value.ToString()
                };
                break;

            case ExpressOperator.Le:
                query = new TermRangeQuery
                {
                    Field             = PropertyName,
                    LessThanOrEqualTo = Value.ToString()
                };
                break;

            case ExpressOperator.Like:
                query = new MatchPhraseQuery
                {
                    Field = PropertyName,
                    Query = Value.ToString()
                };
                break;

            case ExpressOperator.In:
                query = new TermsQuery
                {
                    Field = PropertyName,
                    Terms = (List <object>)Value
                };
                break;

            default:
                throw new ElasticsearchException("构建Elasticsearch查询谓词异常");
            }
            return(query);
        }
コード例 #10
0
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var resolver = ctx.GetMappingResolver();

            var fieldConditions = ctx.Source.SafeGetCollection <FieldCondition>(FieldConditionQueryExtensions.FieldConditionsKey);

            if (fieldConditions == null || fieldConditions.Count <= 0)
            {
                return(Task.CompletedTask);
            }

            foreach (var fieldValue in fieldConditions)
            {
                QueryBase query;
                if (fieldValue.Value == null && fieldValue.Operator == ComparisonOperator.Equals)
                {
                    fieldValue.Operator = ComparisonOperator.IsEmpty;
                }
                else if (fieldValue.Value == null && fieldValue.Operator == ComparisonOperator.NotEquals)
                {
                    fieldValue.Operator = ComparisonOperator.HasValue;
                }

                switch (fieldValue.Operator)
                {
                case ComparisonOperator.Equals:
                    if (fieldValue.Value is IEnumerable && !(fieldValue.Value is string))
                    {
                        query = new TermsQuery {
                            Field = resolver.GetResolvedField(fieldValue.Field), Terms = (IEnumerable <object>)fieldValue.Value
                        }
                    }
                    ;
                    else
                    {
                        query = new TermQuery {
                            Field = resolver.GetResolvedField(fieldValue.Field), Value = fieldValue.Value
                        }
                    };
                    ctx.Filter &= query;

                    break;

                case ComparisonOperator.NotEquals:
                    if (fieldValue.Value is IEnumerable && !(fieldValue.Value is string))
                    {
                        query = new TermsQuery {
                            Field = resolver.GetResolvedField(fieldValue.Field), Terms = (IEnumerable <object>)fieldValue.Value
                        }
                    }
                    ;
                    else
                    {
                        query = new TermQuery {
                            Field = resolver.GetResolvedField(fieldValue.Field), Value = fieldValue.Value
                        }
                    };

                    ctx.Filter &= new BoolQuery {
                        MustNot = new QueryContainer[] { query }
                    };
                    break;

                case ComparisonOperator.IsEmpty:
                    ctx.Filter &= new BoolQuery {
                        MustNot = new QueryContainer[] { new ExistsQuery {
                                                             Field = resolver.GetResolvedField(fieldValue.Field)
                                                         } }
                    };
                    break;

                case ComparisonOperator.HasValue:
                    ctx.Filter &= new ExistsQuery {
                        Field = resolver.GetResolvedField(fieldValue.Field)
                    };
                    break;
                }
            }

            return(Task.CompletedTask);
        }
コード例 #11
0
        /// <summary>
        /// Gets the elastic query for the given feeds.
        /// </summary>
        /// <param name="relevantFeeds">The relevant feeds to determine their query.</param>
        /// <param name="blackList">The blacklist to not violate.</param>
        /// <returns>The query for the feed.</returns>
        private static QueryContainer GetFeedQuery(IEnumerable <Feed> relevantFeeds, IEnumerable <string> blackList)
        {
            QueryContainer finalContainer = null;

            if (relevantFeeds == null || !relevantFeeds.Any())
            {
                throw new InvalidOperationException(nameof(relevantFeeds));
            }

            blackList = blackList?.Select(x => x.ToLowerInvariant()).ToList();
            foreach (var feed in relevantFeeds)
            {
                var       keywordList = feed.Filter.Keywords.Select(x => x.ToLowerInvariant()).ToList();
                QueryBase basicTermsOperator;
                if (blackList != null && blackList.Any())
                {
                    basicTermsOperator = !(new TermsQuery
                    {
                        Field = "ArticleTitle",
                        Terms = blackList
                    } ||
                                           new TermsQuery
                    {
                        Field = "ArticleLongText",
                        Terms = blackList
                    })
                                         &&
                                         (new TermsQuery
                    {
                        Field = "ArticleTitle",
                        Terms = keywordList
                    } ||
                                          new TermsQuery
                    {
                        Field = "ArticleLongText",
                        Terms = keywordList
                    });
                }
                else
                {
                    basicTermsOperator = new TermsQuery
                    {
                        Field = "ArticleTitle",
                        Terms = keywordList
                    } ||
                    new TermsQuery
                    {
                        Field = "ArticleLongText",
                        Terms = keywordList
                    };
                }

                foreach (var guid in feed.Sources.Select(x => x.Id))
                {
                    finalContainer |= new MatchPhraseQuery
                    {
                        Field = "IndexingSourceId",
                        Query = (guid.ToString())
                    } && basicTermsOperator;
                }
            }

            return(finalContainer);
        }
コード例 #12
0
        public void VisitMethod(QNode node)
        {
            var method = EnumResolver.ResolveNodeType(node.Type);

            switch (method)
            {
            case NodeType.OrderBy:
            case NodeType.OrderByDescending:
                var member = this.ContextField.Pop();
                var query  = this.ContextQuery.Pop();
                this.SortingRequest.Add(new KeyValuePair <Field, NodeType>(member, method));
                break;

            case NodeType.Where:
                break;

            case NodeType.Contains:
            {
                var value = string.Format("*{0}*", this.ContextValue.Pop());
                var term  = new QueryStringQuery {
                    DefaultField = this.ContextField.Pop(), Query = value
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.StartsWith:
            {
                var term = new PrefixQuery {
                    Field = this.ContextField.Pop(), Value = this.ContextValue.Pop()
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.EndsWith:
            {
                var value = string.Format("*{0}", this.ContextValue.Pop());
                var term  = new WildcardQuery {
                    Field = this.ContextField.Pop(), Value = value
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.In:
            {
                var value = this.ContextValue.Pop() as List <string>;
                var terms = new TermsQuery {
                    Field = this.ContextField.Pop(), Terms = value
                };
                this.ContextQuery.Push(terms);
            }
            break;

            case NodeType.NotIn:
            {
                var value = this.ContextValue.Pop() as List <string>;
                var terms = !new TermsQuery {
                    Field = this.ContextField.Pop(), Terms = value
                };
                this.ContextQuery.Push(terms);
            }
            break;

            default:
                throw new NotImplementedException(method.ToString());
            }
        }
コード例 #13
0
        public QueryContainer GetQuery()
        {
            QueryContainer query;

            switch (Operator)
            {
            case Operator.Eq:
                query = new TermQuery
                {
                    Field = Infer.Field(PropertyInfo),
                    Value = Value
                };
                break;

            case Operator.Like:
                query = new MatchPhraseQuery
                {
                    Field = Infer.Field(PropertyInfo),
                    Query = Value.ToString()
                };
                break;

            case Operator.Gt:
                query = new TermRangeQuery
                {
                    Field       = Infer.Field(PropertyInfo),
                    GreaterThan = Value.ToString()
                };
                break;

            case Operator.Gte:
                query = new TermRangeQuery
                {
                    Field = Infer.Field(PropertyInfo),
                    GreaterThanOrEqualTo = Value.ToString()
                };
                break;

            case Operator.Lt:
                query = new TermRangeQuery
                {
                    Field    = Infer.Field(PropertyInfo),
                    LessThan = Value.ToString()
                };
                break;

            case Operator.Lte:
                query = new TermRangeQuery
                {
                    Field             = Infer.Field(PropertyInfo),
                    LessThanOrEqualTo = Value.ToString()
                };
                break;

            case Operator.In:
                query = new TermsQuery
                {
                    Field = PropertyName,
                    Terms = (List <object>)Value
                };
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(query);
        }
コード例 #14
0
        public override SearchRequest <DemoEntity> TrunConditionToSql <DemoEntity>(BaseDemoCondition condition)
        {
            var mustClauses = new List <QueryContainer>();

            if (condition != null)
            {
                if (!string.IsNullOrEmpty(condition.MoNO))
                {
                    var termQuery = new TermQuery {
                        Field = "moNo", Value = condition.MoNO
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.RoNO))
                {
                    var termQuery = new TermQuery {
                        Field = "roNo", Value = condition.RoNO
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.BelongDate))
                {
                    var termQuery = new TermQuery {
                        Field = "belongDate", Value = condition.BelongDate
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.SiliconWaferSupplierCode))
                {
                    var termQuery = new TermQuery {
                        Field = "siliconWaferSupplierCode", Value = condition.SiliconWaferSupplierCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.CashmereLineCode))
                {
                    var termQuery = new TermQuery {
                        Field = "cashmereLineCode", Value = condition.CashmereLineCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.CashmereLineCodes))
                {
                    var arr_lineCode = condition.CashmereLineCodes.Split(',');
                    var termQuery    = new TermsQuery {
                        Field = "cashmereLineCode", Terms = arr_lineCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (!string.IsNullOrEmpty(condition.WorkGroupCode))
                {
                    var termQuery = new TermQuery {
                        Field = "workGroupCode", Value = condition.WorkGroupCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (condition.IsSingleSideType.HasValue)
                {
                    var termQuery = new TermQuery {
                        Field = "isSingleSideType", Value = condition.IsSingleSideType.Value
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }

                if (!string.IsNullOrEmpty(condition.ClassShiftCode))
                {
                    var termQuery = new TermQuery {
                        Field = "classShiftCode", Value = condition.ClassShiftCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }

                if (!string.IsNullOrEmpty(condition.OrderCode))
                {
                    var termQuery = new TermQuery {
                        Field = "orderInfo.orderCode", Value = condition.OrderCode
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
                if (condition.StartTime.HasValue && condition.EndTime.HasValue)
                {
                    //µ±Ìì23:59:59.999
                    var endTime = DateMath.Anchored(condition.EndTime.Value).RoundTo(DateMathTimeUnit.Day);

                    var dateQuery = new DateRangeQuery {
                        GreaterThanOrEqualTo = condition.StartTime.Value, LessThanOrEqualTo = endTime, Field = "createTime", TimeZone = "+08:00"
                    };

                    mustClauses.Add(dateQuery);
                }
                if (condition.DocumentType.HasValue)
                {
                    var termQuery = new TermQuery {
                        Field = "documentType", Value = condition.DocumentType.Value
                    };
                    var query = new ConstantScoreQuery()
                    {
                        Filter = termQuery
                    };
                    mustClauses.Add(query);
                }
            }
            var searchRequest = new SearchRequest <DemoEntity>()
            {
                Query = new BoolQuery {
                    Must = mustClauses
                }
            };

            return(searchRequest);
        }