if(count($priceRange)){
$facets = new \Elastica\Facet\Range('price');
$facets->setField('price');
foreach ($min as $key => $value) {
$facets->addRange(intval($min[$key])*100, intval($max[$key])*100);
}
$facets->setGlobal(false);
$query->addFacet($facets);
}
This doesn't work for me, I am trying to search on two ways.
Like this:
$client = $this->container->get('fos_elastica.client')
$searcher = new \Elastica\Search($client)
$esResultSet = $searcher->search($query , 500)
$arrayOfResults = $esResultSet->getResults()
$facets = $esResultSet->getFacets()
Or like this
$resultSet = $finder->find($query, 500)
Neither of this works. Why my facet range doesn't work?
Here is more code:
$boolQuery = new \Elastica\Query\Bool();
$fieldText = new \Elastica\Query\Text();
$fieldText->setFieldQuery('name', $keyword);
$fieldText->setFieldParam('name', 'analyzer', 'my_analyzer');
$boolQuery->addMust($fieldText);
$fieldText = new \Elastica\Query\Text();
$fieldText->setFieldQuery('description', $keyword);
$fieldText->setFieldParam('description', 'analyzer', 'my_analyzer');
$boolQuery->addShould($fieldText);
$fieldTerms = new \Elastica\Query\Terms();
$fieldTerms->setTerms('country_id', $countryArray);
$boolQuery->addMust($fieldTerms);
$fieldTerms = new \Elastica\Query\Terms();
$fieldTerms->setTerms('taxon_ids', $themeArray);
$boolQuery->addMust($fieldTerms);
$query->setSort(array("price" => array("order" => "asc")));
Related
I'm using ElarsticSearch 7.7 & NEST 7.7 and on my web page, I'm getting 9 search result documents per page. Even I'm showing the first 9 results on the page, I need to return some property values from all the results for side filtration on the web page.
Eg: if I'm searching "LapTop", my page will show 9 results on the first page. Also, I need to show all the "Manufactures" from all the search results. Not only manufacturers in the first-page result. Then customers can filter by manufacture not only display on the first page.
I have tried GlobalAggregation but it returns categories and manufactures only items in selected page.
public SearchResult Search(SearchType searchType, string searchQuery, int storeId, int pageNumber = 1, int pageSize = 12, IList<SearchFilter> requestFilter = null, decimal? priceFrom = 0, decimal? priceTo = 100000000, string sortBy = null, int totalCount = 0)
{
var queryContainer = new QueryContainer();
var sorts = new List<ISort>();
sorts.Add(new FieldSort { Field = "_score", Order = SortOrder.Descending });
switch (sortBy)
{
case "z-a":
sorts.Add(new FieldSort { Field = Field<ElasticIndexGroupProduct>(p => p.SortValue), Order = SortOrder.Descending });
break;
case "a-z":
sorts.Add(new FieldSort { Field = Field<ElasticIndexGroupProduct>(p => p.SortValue), Order = SortOrder.Ascending });
break;
}
var aggrigations = new AggregationDictionary
{
{"average_per_child", new
AverageAggregation("average_per_child",Field<ElasticIndexGroupProduct>(d => d.Price))},
{"max_per_child", new MaxAggregation("max_per_child",Field<ElasticIndexGroupProduct>(d => d.Price))},
{"min_per_child", new MinAggregation("min_per_child", Field<ElasticIndexGroupProduct>(d => d.Price))},
{
"globle_filter_aggrigation", new GlobalAggregation("globle_filter_aggrigation")
{
Aggregations =new AggregationDictionary
{
{"category_flow", new TermsAggregation("category_flow"){Field = Field<ElasticIndexGroupProduct>(p => p.CategoryFlow)} },
{"manufacturers", new TermsAggregation("manufacturers"){Field = Field<ElasticIndexGroupProduct>(p => p.Manufacturer)} }
}
}
}
};
var searchRequest = new SearchRequest<ElasticIndexGroupProduct>()
{
Profile = true,
From = (pageNumber - 1) * pageSize,
Size = pageSize,
Version = true,
Sort = sorts,
//Scroll = Time.MinusOne,
Aggregations = aggrigations
};
var multiMatch = new QueryStringQuery
{
Query = searchQuery,
Fields = GetSearchFields(searchType),
Boost = 1.1,
Name = "named_query",
DefaultOperator = Operator.Or,
Analyzer = "standard",
QuoteAnalyzer = "keyword",
AllowLeadingWildcard = true,
MaximumDeterminizedStates = 2,
Escape = true,
FuzzyPrefixLength = 2,
FuzzyMaxExpansions = 3,
FuzzyRewrite = MultiTermQueryRewrite.ConstantScore,
Rewrite = MultiTermQueryRewrite.ConstantScore,
Fuzziness = Fuzziness.Auto,
TieBreaker = 1,
AnalyzeWildcard = true,
MinimumShouldMatch = 2,
QuoteFieldSuffix = "'",
Lenient = true,
AutoGenerateSynonymsPhraseQuery = false
};
searchRequest.Query = new BoolQuery
{
Must = new QueryContainer[] { multiMatch },
Filter = new QueryContainer[] { queryContainer }
};
var searchResponse = _client.Search<ElasticIndexGroupProduct>(searchRequest);
var categoryFlowsGlobe = new List<string>();
var allAggregations = searchResponse.Aggregations.Global("globle_filter_aggrigation");
var categories = allAggregations.Terms("category_flow");
foreach (var aggItem in categories.Buckets)
{
if (!categoryFlowsGlobe.Any(x => x == aggItem.Key))
{
categoryFlowsGlobe.Add(aggItem.Key);
}
}
}
This is the exact use case for Post filter - to run a search request that returns hits and aggregations, then to apply filtering to the hits after aggregations have been calculated.
For Manufacturers, these can be retrieved with a terms aggregation in the search request - you can adjust the size on the aggregation if you need to return all manufacturers, otherwise you might decide to return only the top x.
Code:
public IEnumerable<InvoiceStringData> GetInvoiceByID(string orderID)
{
var orderId = Convert.ToInt32(orderID);
ResponseDetails jsonRes = new ResponseDetails();
List<string> campaignidByProdid = new List<string>();
Productlevelcharges prodlevel = new Productlevelcharges();
List<Productlevelcharges> lstprodlevel = new
List<Productlevelcharges>();
List<OrderVM> lstorvm = new List<OrderVM>();
List<ProductMaster> lstpmvm = new List<ProductMaster>();
List<OrderDetailsVM> lstodvm = new List<OrderDetailsVM>();
List<ProductVariant> lstpvvm = new List<ProductVariant>();
List<CampaignProductMapper> lstcmvm = new
List<CampaignProductMapper>();
List<CampaignVm> lstcm = new List<CampaignVm>();
List<InvoiceStringData> Bill = new List<InvoiceStringData>();
InvoiceStringData InvoiceStringData = new InvoiceStringData();
var orderChargId = _context.Orderlevelcharges.Where(a => a.OrderId == orderId).Select(a => a.OrderChargeId).FirstOrDefault();
var orderCharge = orderChargId.ToString();
var lst = CalculateUnitPrice(orderCharge, orderID);
var result = (from v in lst
group v by new
{
v.InvoiceDate,
v.InvoiceNo,
v.NetAmount,
v.TotalAmount,
v.SaveAmount
} into order
select new
{
invoiceDate = order.Key.InvoiceDate,
netAmount = order.Key.NetAmount,
totalAmount = order.Key.TotalAmount,
saveAmount = order.Key.SaveAmount,
// productMasterlst=order
_invoice = new
{
product = order.Select(o => new {
o.productName, o.UnitPrice, o.Quantity, o.UnitOfMeasure, o.hsnCode,
o.Weight, o.Tax1Sgst, o.Tax2Cgst, o.Tax3Igst })
}
}).ToList();
return result;
}
error:
CS0266 Cannot implicitly convert type
'System.Collections.Generic.List<> product> _invoice>>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) Payment.API D:\Consumer Billing\MRU 15-05-18 10.47\MRU Current\src\Services\Payment\Payment.API\BAL\Repository\InvoiceRepo.cs 131 Active
I have a method which has anonymous type and after it I add parameters to viewmodel.
public void ConvertirBono(List<Bono> ParamBonos,List<Empleado> ParamEmpleado)
{
Bonos = new List<BonoVM>();
var bonos2 = ParamBonos.Where(x => x.mdEstatusRegistro)
.Join(ParamEmpleado, x => x.EmpleadoID, y => y.ID, (x, y) => new
{
empleadoCompleto = y.nCodigoEmpleado.ToString() + y.vNombreEmpleado,
}).ToList();
foreach (var p in ParamBonos)
{
Bonos.Add(new BonoVM()
{
EmpleadoId = p.EmpleadoID,
vEmpleado = empleadoCompleto,
nBono1 = p.nBono1,
nBono2 = p.nBono2
});
}
}
What I want to do is to insert anonymous type empleadoCompleto when I add into ViewModel(foreach clause) but it always comes null. I try to declare that variable outside but it still comes null.
public void ConvertirBono(List<Bono> ParamBonos,List<Empleado> ParamEmpleado)
{
Bonos = new List<BonoVM>();
string empleadoCompleto = null;
var bonos2 = ParamBonos.Where(x => x.mdEstatusRegistro)
.Join(ParamEmpleado, x => x.EmpleadoID, y => y.ID, (x, y) => new
{
empleadoCompleto = y.nCodigoEmpleado.ToString() + y.vNombreEmpleado,
}).ToList();
foreach (var p in ParamBonos)
{
Bonos.Add(new BonoVM()
{
EmpleadoId = p.EmpleadoID,
vEmpleado = empleadoCompleto,
nBono1 = p.nBono1,
nBono2 = p.nBono2
});
}
}
What am I doing wrong?
From your code, it looks like all you are trying to do it merge the two list objects together and create a list of view models. There's no need to create anonymous types here at all, you can do it all with one line of code. This may not be quite right as your question is unclear, but it should at least get you on the right track.
public void ConvertirBono(List<Bono> ParamBonos,List<Empleado> ParamEmpleado)
{
Bonos = ParamBonos.Where(x => x.mdEstatusRegistro)
.Join(ParamEmpleado,
x => x.EmpleadoID,
y => y.ID,
(x, y) => new BonoVM
{
EmpleadoId = x.EmpleadoID,
vEmpleado = y.nCodigoEmpleado.ToString() + y.vNombreEmpleado,
nBono1 = x.nBono1,
nBono2 = x.nBono2
})
.ToList();
}
I have a linq query like below:
public IQueryable<vmEmp> GetEmp(int crewid)
{
var dt = new EmpEntities();
var e = from t in dt.tblEmp
where (t.CrewId == crewid)
select new vmEmp
{
Id = -1,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
};
return e;
}
I hope can make the Id auto decrease by 1 till end of the employee.
like first's Id is -1, second's is -2, third is -3 ...
How to do that here? Thanks a lot
If this was LINQ-to-objects, you could use this overload of Select:
var dt = new EmpEntities();
var e = dt.tblEmp
.Where(t => t.CrewId == crewid)
.Select((t,index) => new vmEmp
{
Id = -index - 1,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
});
But EF doesn't suport this, because the indexer can't be translated into SQL. So you need a work-around:
var dt = new EmpEntities();
var e = dt.tblEmp
.Where(t => t.CrewId == crewid)
.Select(t => new
{
Id = 0,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
}
.AsEnumerable() // Continue in memory
.Select((t,index) => new vmEmp
{
Id = -index - 1,
Crew = t.Crew,
Name = t.Name,
Address = t.Address
});
Side note: it's recommended to put dt in a using construct.
Use a counter variable and decrease it for every record while you project it to your custom POCO.
public IQueryable<vmEmp> GetEmp(int crewid)
{
int counter=0;
var dt = new EmpEntities();
//load the items to a list of anonymous type
var eList = from t in dt.tblEmp
where (t.CrewId == crewid)
.Select(s=>
new { Id = 0,
Crew = s.crewid,
Name = s.Name,
Address = s.Address
}).ToList();
var e=eList.Select(x=> new vmEmp
{
Id = --counter,
Crew = x.Crew,
Name = x.Name,
Address = x.Address
});
return e.AsQueryable();
}
i want Replace بازي to بازی
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Address.Replace("ی","ي").StartsWith( Address.Replace("ی","ي") ) )
select new
{
....
}
I think,You should write in select query
var List = (from darkhast in Tbl_Darkhast) )
select new Tbl_Darkhast
{
Address= darkhast.Address.Replace("ی","ي")
,Name = darkhast.Name
};
The distance and character displacement:
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Address.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک").StartsWith(Address.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک")))
select new
{
....strong text
}
result is =>
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Name_Moteghazi.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک").StartsWith(NameMoteghazi.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک")))
select new
{
Shomare_Darkhast = darkhast.Shomare_Darkhast,
Tarikhe_Darkhast = darkhast.Tarikhe_Darkhast,
Sharhe_NoeDarkhast = darkhast.TBL_NoeDarkhast.Sharhe_NoeDarkhast,
Mantaghe = darkhast.Mantaghe,
Hoze = darkhast.Hoze,
Block = darkhast.Block,
Melk = darkhast.Melk,
});
return (ObjectQuery)List;